update-alternatives.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  1. /*
  2. * update-alternatives
  3. *
  4. * Copyright © 1995 Ian Jackson <ian@davenant.greenend.org.uk>
  5. * Copyright © 2000-2002 Wichert Akkerman <wakkerma@debian.org>
  6. * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
  8. * Copyright © 2009-2010 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <stdarg.h>
  30. #include <stdbool.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <dirent.h>
  36. #include <time.h>
  37. #include <setjmp.h>
  38. #include <assert.h>
  39. #include <locale.h>
  40. #include <ctype.h>
  41. #include <limits.h>
  42. #include <dpkg/macros.h>
  43. #include <dpkg/i18n.h>
  44. /* Global variables: */
  45. #define PROGNAME "update-alternatives"
  46. static const char *altdir = SYSCONFDIR "/alternatives";
  47. static const char *admdir;
  48. static const char *prog_path = "update-alternatives";
  49. /* Action to perform */
  50. static const char *action = NULL;
  51. static const char *log_file = LOGDIR "/alternatives.log";
  52. /* Skip alternatives properly configured in auto mode (for --config) */
  53. static int opt_skip_auto = 0;
  54. static int opt_verbose = 0;
  55. static int opt_force = 0;
  56. #define ALT_TMP_EXT ".dpkg-tmp"
  57. /*
  58. * Functions.
  59. */
  60. static void
  61. version(void)
  62. {
  63. printf(_("Debian %s version %s.\n"), PROGNAME, VERSION);
  64. printf("\n");
  65. printf(_(
  66. "This is free software; see the GNU General Public License version 2 or\n"
  67. "later for copying conditions. There is NO warranty.\n"));
  68. }
  69. static void
  70. usage(void)
  71. {
  72. printf(_(
  73. "Usage: %s [<option> ...] <command>\n"
  74. "\n"), PROGNAME);
  75. printf(_(
  76. "Commands:\n"
  77. " --install <link> <name> <path> <priority>\n"
  78. " [--slave <link> <name> <path>] ...\n"
  79. " add a group of alternatives to the system.\n"
  80. " --remove <name> <path> remove <path> from the <name> group alternative.\n"
  81. " --remove-all <name> remove <name> group from the alternatives system.\n"
  82. " --auto <name> switch the master link <name> to automatic mode.\n"
  83. " --display <name> display information about the <name> group.\n"
  84. " --query <name> machine parseable version of --display <name>.\n"
  85. " --list <name> display all targets of the <name> group.\n"
  86. " --get-selections list master alternative names and their status.\n"
  87. " --set-selections read alternative status from standard input.\n"
  88. " --config <name> show alternatives for the <name> group and ask the\n"
  89. " user to select which one to use.\n"
  90. " --set <name> <path> set <path> as alternative for <name>.\n"
  91. " --all call --config on all alternatives.\n"
  92. "\n"));
  93. printf(_(
  94. "<link> is the symlink pointing to %s/<name>.\n"
  95. " (e.g. /usr/bin/pager)\n"
  96. "<name> is the master name for this link group.\n"
  97. " (e.g. pager)\n"
  98. "<path> is the location of one of the alternative target files.\n"
  99. " (e.g. /usr/bin/less)\n"
  100. "<priority> is an integer; options with higher numbers have higher priority in\n"
  101. " automatic mode.\n"
  102. "\n"), altdir);
  103. printf(_(
  104. "Options:\n"
  105. " --altdir <directory> change the alternatives directory.\n"
  106. " --admindir <directory> change the administrative directory.\n"
  107. " --log <file> change the log file.\n"
  108. " --force allow replacing files with alternative links.\n"
  109. " --skip-auto skip prompt for alternatives correctly configured\n"
  110. " in automatic mode (relevant for --config only)\n"
  111. " --verbose verbose operation, more output.\n"
  112. " --quiet quiet operation, minimal output.\n"
  113. " --help show this help message.\n"
  114. " --version show the version.\n"
  115. ));
  116. }
  117. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1)
  118. error(char const *fmt, ...)
  119. {
  120. va_list args;
  121. fprintf(stderr, "%s: %s: ", PROGNAME, _("error"));
  122. va_start(args, fmt);
  123. vfprintf(stderr, fmt, args);
  124. va_end(args);
  125. fprintf(stderr, "\n");
  126. exit(2);
  127. }
  128. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1)
  129. syserr(char const *fmt, ...)
  130. {
  131. va_list args;
  132. fprintf(stderr, "%s: %s: ", PROGNAME, _("error"));
  133. va_start(args, fmt);
  134. vfprintf(stderr, fmt, args);
  135. va_end(args);
  136. fprintf(stderr, ": %s\n", strerror(errno));
  137. exit(2);
  138. }
  139. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(1)
  140. badusage(char const *fmt, ...)
  141. {
  142. va_list args;
  143. fprintf(stderr, "%s: ", PROGNAME);
  144. va_start(args, fmt);
  145. vfprintf(stderr, fmt, args);
  146. va_end(args);
  147. fprintf(stderr, "\n\n");
  148. fprintf(stderr, _("Use '%s --help' for program usage information."),
  149. PROGNAME);
  150. fprintf(stderr, "\n");
  151. exit(2);
  152. }
  153. static void DPKG_ATTR_PRINTF(1)
  154. warning(char const *fmt, ...)
  155. {
  156. va_list args;
  157. if (opt_verbose < 0)
  158. return;
  159. fprintf(stderr, "%s: %s: ", PROGNAME, _("warning"));
  160. va_start(args, fmt);
  161. vfprintf(stderr, fmt, args);
  162. va_end(args);
  163. fprintf(stderr, "\n");
  164. }
  165. static void DPKG_ATTR_PRINTF(1)
  166. debug(char const *fmt, ...)
  167. {
  168. #if 0
  169. va_list args;
  170. fprintf(stderr, "DEBUG: ");
  171. va_start(args, fmt);
  172. vfprintf(stderr, fmt, args);
  173. va_end(args);
  174. fprintf(stderr, "\n");
  175. #endif
  176. }
  177. static void DPKG_ATTR_PRINTF(1)
  178. verbose(char const *fmt, ...)
  179. {
  180. va_list args;
  181. if (opt_verbose < 1)
  182. return;
  183. printf("%s: ", PROGNAME);
  184. va_start(args, fmt);
  185. vprintf(fmt, args);
  186. va_end(args);
  187. printf("\n");
  188. }
  189. static void DPKG_ATTR_PRINTF(1)
  190. info(char const *fmt, ...)
  191. {
  192. va_list args;
  193. if (opt_verbose < 0)
  194. return;
  195. printf("%s: ", PROGNAME);
  196. va_start(args, fmt);
  197. vprintf(fmt, args);
  198. va_end(args);
  199. printf("\n");
  200. }
  201. static void DPKG_ATTR_PRINTF(1)
  202. pr(char const *fmt, ...)
  203. {
  204. va_list args;
  205. va_start(args, fmt);
  206. vprintf(fmt, args);
  207. va_end(args);
  208. printf("\n");
  209. }
  210. static void *
  211. xmalloc(size_t size)
  212. {
  213. void *ptr;
  214. ptr = malloc(size);
  215. if (!ptr)
  216. error(_("malloc failed (%zu bytes)"), size);
  217. return ptr;
  218. }
  219. static char *
  220. xstrdup(const char *str)
  221. {
  222. char *new_str;
  223. if (!str)
  224. return NULL;
  225. new_str = strdup(str);
  226. if (!new_str)
  227. error(_("failed to allocate memory"));
  228. return new_str;
  229. }
  230. static char *
  231. areadlink(const char *linkname)
  232. {
  233. struct stat st;
  234. char *buf;
  235. ssize_t size;
  236. /* Allocate required memory to store the value of the symlink */
  237. if (lstat(linkname, &st))
  238. return NULL;
  239. if (!S_ISLNK(st.st_mode)) {
  240. errno = EINVAL;
  241. return NULL;
  242. }
  243. buf = xmalloc(st.st_size + 1);
  244. /* Read it and terminate the string properly */
  245. size = readlink(linkname, buf, st.st_size);
  246. if (size == -1) {
  247. int saved_errno = errno;
  248. free(buf);
  249. errno = saved_errno;
  250. return NULL;
  251. }
  252. buf[size] = '\0';
  253. return buf;
  254. }
  255. static char *
  256. xreadlink(const char *linkname)
  257. {
  258. char *buf;
  259. buf = areadlink(linkname);
  260. if (buf == NULL)
  261. syserr(_("unable to read link '%.255s'"), linkname);
  262. return buf;
  263. }
  264. static int DPKG_ATTR_VPRINTF(2)
  265. xvasprintf(char **strp, const char *fmt, va_list args)
  266. {
  267. int ret;
  268. ret = vasprintf(strp, fmt, args);
  269. if (ret < 0)
  270. error(_("failed to allocate memory"));
  271. return ret;
  272. }
  273. static int DPKG_ATTR_PRINTF(2)
  274. xasprintf(char **strp, const char *fmt, ...)
  275. {
  276. va_list args;
  277. int ret;
  278. va_start(args, fmt);
  279. ret = xvasprintf(strp, fmt, args);
  280. va_end(args);
  281. return ret;
  282. }
  283. static void
  284. set_action(const char *new_action)
  285. {
  286. if (action)
  287. badusage(_("two commands specified: --%s and --%s"),
  288. action, new_action);
  289. action = new_action;
  290. }
  291. static const char *
  292. admindir_init(void)
  293. {
  294. const char *basedir, *dpkg_basedir;
  295. char *admindir;
  296. dpkg_basedir = getenv("DPKG_ADMINDIR");
  297. if (dpkg_basedir)
  298. basedir = dpkg_basedir;
  299. else
  300. basedir = ADMINDIR;
  301. xasprintf(&admindir, "%s/%s", basedir, "alternatives");
  302. return admindir;
  303. }
  304. static FILE *fh_log = NULL;
  305. static void DPKG_ATTR_PRINTF(1)
  306. log_msg(const char *fmt, ...)
  307. {
  308. va_list args;
  309. if (fh_log == NULL) {
  310. fh_log = fopen(log_file, "a");
  311. if (fh_log == NULL && errno != EACCES)
  312. syserr(_("cannot append to '%s'"), log_file);
  313. }
  314. if (fh_log) {
  315. char timestamp[64];
  316. time_t now;
  317. time(&now);
  318. strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S",
  319. localtime(&now));
  320. fprintf(fh_log, "%s %s: ", PROGNAME, timestamp);
  321. va_start(args, fmt);
  322. vfprintf(fh_log, fmt, args);
  323. va_end(args);
  324. fprintf(fh_log, "\n");
  325. }
  326. }
  327. static int
  328. spawn(const char *prog, const char *args[])
  329. {
  330. pid_t pid, dead_pid;
  331. int status;
  332. pid = fork();
  333. if (pid == -1)
  334. error(_("fork failed"));
  335. if (pid == 0) {
  336. execvp(prog, (char *const *)args);
  337. syserr(_("unable to execute %s (%s)"), prog, prog);
  338. }
  339. while ((dead_pid = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
  340. if (dead_pid != pid)
  341. error(_("wait for subprocess %s failed"), prog);
  342. return status;
  343. }
  344. static bool
  345. rename_mv(const char *src, const char *dst)
  346. {
  347. const char *args[] = { "mv", src, dst, NULL };
  348. int rc;
  349. if (rename(src, dst) == 0)
  350. return true;
  351. if (errno == ENOENT)
  352. return false;
  353. rc = spawn("mv", args);
  354. if (WIFEXITED(rc) && WEXITSTATUS(rc) == 0)
  355. return true;
  356. return false;
  357. }
  358. static void
  359. checked_symlink(const char *filename, const char *linkname)
  360. {
  361. if (symlink(filename, linkname))
  362. syserr(_("error creating symbolic link '%.255s'"), linkname);
  363. }
  364. static void
  365. checked_mv(const char *src, const char *dst)
  366. {
  367. if (!rename_mv(src, dst))
  368. syserr(_("unable to install '%.250s' as '%.250s'"), src, dst);
  369. }
  370. static void
  371. checked_rm(const char *f)
  372. {
  373. if (!unlink(f))
  374. return;
  375. if (errno != ENOENT)
  376. syserr(_("unable to remove '%s'"), f);
  377. }
  378. static void DPKG_ATTR_PRINTF(1)
  379. checked_rm_args(const char *fmt, ...)
  380. {
  381. va_list args;
  382. char *path;
  383. va_start(args, fmt);
  384. xvasprintf(&path, fmt, args);
  385. va_end(args);
  386. checked_rm(path);
  387. free(path);
  388. }
  389. /*
  390. * OBJECTS
  391. */
  392. struct fileset {
  393. struct fileset *next;
  394. char *master_file;
  395. int priority;
  396. struct slave_file {
  397. struct slave_file *next;
  398. char *name;
  399. char *file;
  400. } *slaves;
  401. };
  402. static struct fileset *
  403. fileset_new(const char *master_file, int prio)
  404. {
  405. struct fileset *fs;
  406. fs = xmalloc(sizeof(*fs));
  407. fs->next = NULL;
  408. fs->master_file = xstrdup(master_file);
  409. fs->priority = prio;
  410. fs->slaves = NULL;
  411. return fs;
  412. }
  413. static void
  414. fileset_free(struct fileset *fs)
  415. {
  416. struct slave_file *slave, *next;
  417. free(fs->master_file);
  418. for (slave = fs->slaves; slave; slave = next) {
  419. next = slave->next;
  420. free(slave->name);
  421. free(slave->file);
  422. free(slave);
  423. }
  424. free(fs);
  425. }
  426. /* name and file must be allocated with malloc */
  427. static void
  428. fileset_add_slave(struct fileset *fs, char *name, char *file)
  429. {
  430. struct slave_file *sl, *cur, *prev = NULL;
  431. /* Replace existing first */
  432. for (cur = fs->slaves; cur; cur = cur->next) {
  433. if (strcmp(cur->name, name) == 0) {
  434. free(cur->file);
  435. cur->file = xstrdup(file);
  436. return;
  437. }
  438. prev = cur;
  439. }
  440. /* Otherwise add new at the end */
  441. sl = xmalloc(sizeof(*sl));
  442. sl->next = NULL;
  443. sl->name = name;
  444. sl->file = file;
  445. if (prev)
  446. prev->next = sl;
  447. else
  448. fs->slaves = sl;
  449. }
  450. static const char *
  451. fileset_get_slave(struct fileset *fs, const char *name)
  452. {
  453. struct slave_file *slave;
  454. for (slave = fs->slaves; slave; slave = slave->next) {
  455. if (strcmp(slave->name, name) == 0)
  456. return slave->file;
  457. }
  458. return NULL;
  459. }
  460. static bool
  461. fileset_has_slave(struct fileset *fs, const char *name)
  462. {
  463. const char *file = fileset_get_slave(fs, name);
  464. if (file == NULL)
  465. return false;
  466. return file[0] != '\0';
  467. }
  468. static bool
  469. fileset_can_install_slave(struct fileset *fs, const char *slave_name)
  470. {
  471. struct stat st;
  472. /* Decide whether the slave alternative must be setup */
  473. if (fileset_has_slave(fs, slave_name)) {
  474. const char *slave = fileset_get_slave(fs, slave_name);
  475. errno = 0;
  476. if (stat(slave, &st) == -1 && errno != ENOENT)
  477. syserr(_("cannot stat file '%s'"), slave);
  478. if (errno == 0)
  479. return true;
  480. }
  481. return false;
  482. }
  483. struct slave_link {
  484. struct slave_link *next;
  485. char *name;
  486. char *link;
  487. bool updated;
  488. };
  489. struct commit_operation {
  490. struct commit_operation *next;
  491. enum opcode {
  492. OPCODE_NOP,
  493. OPCODE_RM,
  494. OPCODE_MV,
  495. } opcode;
  496. char *arg_a;
  497. char *arg_b;
  498. };
  499. enum alternative_update_reason {
  500. ALT_UPDATE_NO,
  501. ALT_UPDATE_SLAVE_CHANGED,
  502. ALT_UPDATE_LINK_BROKEN,
  503. };
  504. struct alternative {
  505. char *master_name;
  506. char *master_link;
  507. char *current;
  508. enum alternative_status {
  509. ALT_ST_UNKNOWN,
  510. ALT_ST_AUTO,
  511. ALT_ST_MANUAL,
  512. } status;
  513. struct slave_link *slaves;
  514. struct fileset *choices;
  515. struct commit_operation *commit_ops;
  516. int ref_count;
  517. bool modified;
  518. bool known_current;
  519. };
  520. static void
  521. slave_link_free(struct slave_link *slave)
  522. {
  523. free(slave->name);
  524. free(slave->link);
  525. free(slave);
  526. }
  527. static void
  528. commit_operation_free(struct commit_operation *commit_op)
  529. {
  530. free(commit_op->arg_a);
  531. free(commit_op->arg_b);
  532. free(commit_op);
  533. }
  534. static struct alternative *
  535. alternative_new(const char *name)
  536. {
  537. struct alternative *alt;
  538. alt = xmalloc(sizeof(*alt));
  539. alt->master_name = xstrdup(name);
  540. alt->master_link = NULL;
  541. alt->current = NULL;
  542. alt->status = ALT_ST_UNKNOWN;
  543. alt->slaves = NULL;
  544. alt->choices = NULL;
  545. alt->commit_ops = NULL;
  546. alt->modified = false;
  547. alt->known_current = false;
  548. alt->ref_count = 1;
  549. return alt;
  550. }
  551. static inline void
  552. alternative_ref(struct alternative *a)
  553. {
  554. a->ref_count++;
  555. }
  556. static inline bool
  557. alternative_unref(struct alternative *a)
  558. {
  559. return --a->ref_count == 0;
  560. }
  561. static void
  562. alternative_choices_free(struct alternative *a)
  563. {
  564. struct fileset *fs;
  565. if (a->choices)
  566. a->modified = true;
  567. while (a->choices) {
  568. fs = a->choices;
  569. a->choices = fs->next;
  570. fileset_free(fs);
  571. }
  572. }
  573. static void
  574. alternative_commit_operations_free(struct alternative *a)
  575. {
  576. struct commit_operation *op;
  577. while (a->commit_ops) {
  578. op = a->commit_ops;
  579. a->commit_ops = op->next;
  580. commit_operation_free(op);
  581. }
  582. }
  583. static void
  584. alternative_reset(struct alternative *alt)
  585. {
  586. struct slave_link *slave;
  587. free(alt->current);
  588. alt->current = NULL;
  589. free(alt->master_link);
  590. alt->master_link = NULL;
  591. while (alt->slaves) {
  592. slave = alt->slaves;
  593. alt->slaves = slave->next;
  594. slave_link_free(slave);
  595. }
  596. alternative_choices_free(alt);
  597. alternative_commit_operations_free(alt);
  598. alt->modified = false;
  599. alt->known_current = false;
  600. }
  601. static void
  602. alternative_free(struct alternative *alt)
  603. {
  604. if (!alternative_unref(alt))
  605. return;
  606. alternative_reset(alt);
  607. free(alt->master_name);
  608. free(alt);
  609. }
  610. static int
  611. alternative_choices_count(struct alternative *alt)
  612. {
  613. struct fileset *fs;
  614. int count = 0;
  615. for (fs = alt->choices; fs; fs = fs->next)
  616. count++;
  617. return count;
  618. }
  619. static int
  620. alternative_slaves_count(struct alternative *alt)
  621. {
  622. struct slave_link *sl;
  623. int count = 0;
  624. for (sl = alt->slaves; sl; sl = sl->next)
  625. count++;
  626. return count;
  627. }
  628. static int
  629. compare_fileset(const void *va, const void *vb)
  630. {
  631. const struct fileset *a = *(const struct fileset **)va;
  632. const struct fileset *b = *(const struct fileset **)vb;
  633. assert(a && a->master_file);
  634. assert(b && b->master_file);
  635. return strcmp(a->master_file, b->master_file);
  636. }
  637. static int
  638. compare_slave_link(const void *va, const void *vb)
  639. {
  640. const struct slave_link *a = *(const struct slave_link **)va;
  641. const struct slave_link *b = *(const struct slave_link **)vb;
  642. assert(a && a->name);
  643. assert(b && b->name);
  644. return strcmp(a->name, b->name);
  645. }
  646. static void
  647. alternative_sort_choices(struct alternative *a)
  648. {
  649. int count, i;
  650. struct fileset **table, *fs;
  651. count = alternative_choices_count(a);
  652. if (count < 2) /* Nothing to sort */
  653. return;
  654. /* Store objects in a table instead of a linked list */
  655. table = xmalloc(sizeof(fs) * count);
  656. for (fs = a->choices, i = 0; fs; fs = fs->next) {
  657. assert(fs->master_file);
  658. table[i++] = fs;
  659. }
  660. qsort(table, count, sizeof(fs), compare_fileset);
  661. /* Rewrite the linked list from the sorted table */
  662. a->choices = fs = table[0];
  663. table[count - 1]->next = NULL;
  664. for (i = 1; i < count; fs = fs->next, i++)
  665. fs->next = table[i];
  666. free(table);
  667. }
  668. static void
  669. alternative_sort_slaves(struct alternative *a)
  670. {
  671. int count, i;
  672. struct slave_link **table, *sl;
  673. count = alternative_slaves_count(a);
  674. if (count < 2) /* Nothing to sort */
  675. return;
  676. /* Store objects in a table instead of a linked list */
  677. table = xmalloc(sizeof(sl) * count);
  678. for (sl = a->slaves, i = 0; sl; sl = sl->next, i++) {
  679. table[i] = sl;
  680. }
  681. qsort(table, count, sizeof(sl), compare_slave_link);
  682. /* Rewrite the linked list from the sorted table */
  683. a->slaves = sl = table[0];
  684. table[count - 1]->next = NULL;
  685. for (i = 1; i < count; sl = sl->next, i++)
  686. sl->next = table[i];
  687. free(table);
  688. }
  689. static struct fileset *
  690. alternative_get_fileset(struct alternative *a, const char *file)
  691. {
  692. struct fileset *fs;
  693. for (fs = a->choices; fs; fs = fs->next)
  694. if (strcmp(fs->master_file, file) == 0)
  695. return fs;
  696. return NULL;
  697. }
  698. static struct slave_link *
  699. alternative_get_slave(struct alternative *a, const char *name)
  700. {
  701. struct slave_link *sl;
  702. for (sl = a->slaves; sl; sl = sl->next)
  703. if (strcmp(sl->name, name) == 0)
  704. return sl;
  705. return NULL;
  706. }
  707. static bool
  708. alternative_has_slave(struct alternative *a, const char *name)
  709. {
  710. return alternative_get_slave(a, name) != NULL;
  711. }
  712. static bool
  713. alternative_has_choice(struct alternative *a, const char *file)
  714. {
  715. return alternative_get_fileset(a, file) != NULL;
  716. }
  717. static void
  718. alternative_add_choice(struct alternative *a, struct fileset *fs)
  719. {
  720. struct fileset *cur, *prev = NULL;
  721. /* Replace if already existing */
  722. for (cur = a->choices; cur; cur = cur->next) {
  723. if (strcmp(cur->master_file, fs->master_file) == 0) {
  724. fs->next = cur->next;
  725. fileset_free(cur);
  726. if (prev)
  727. prev->next = fs;
  728. else
  729. a->choices = fs;
  730. /* XXX: Be smarter in detecting change? */
  731. a->modified = true;
  732. return;
  733. }
  734. prev = cur;
  735. }
  736. /* Otherwise add at the end */
  737. if (prev == NULL)
  738. a->choices = fs;
  739. else
  740. prev->next = fs;
  741. fs->next = NULL;
  742. a->modified = true;
  743. }
  744. /* slave_name and slave_link must be allocated with malloc */
  745. static struct slave_link *
  746. alternative_add_slave(struct alternative *a, char *slave_name,
  747. char *slave_link)
  748. {
  749. struct slave_link *sl, *new;
  750. /* Replace if already existing */
  751. for (sl = a->slaves; sl; sl = sl->next) {
  752. if (strcmp(sl->name, slave_name) == 0) {
  753. free(sl->link);
  754. sl->link = xstrdup(slave_link);
  755. return sl;
  756. }
  757. if (sl->next == NULL)
  758. break;
  759. }
  760. /* Otherwise create new and add at the end */
  761. new = xmalloc(sizeof(*new));
  762. new->name = slave_name;
  763. new->link = slave_link;
  764. new->updated = false;
  765. new->next = NULL;
  766. if (sl)
  767. sl->next = new;
  768. else
  769. a->slaves = new;
  770. return new;
  771. }
  772. static void
  773. alternative_copy_slave(struct alternative *a, struct slave_link *sl)
  774. {
  775. struct slave_link *sl_new;
  776. sl_new = alternative_add_slave(a, xstrdup(sl->name), xstrdup(sl->link));
  777. sl_new->updated = sl->updated;
  778. }
  779. static const char *
  780. alternative_status_string(enum alternative_status status)
  781. {
  782. return (status == ALT_ST_AUTO) ? "auto" : "manual";
  783. }
  784. static const char *
  785. alternative_status_describe(enum alternative_status status)
  786. {
  787. return (status == ALT_ST_AUTO) ? _("auto mode") : _("manual mode");
  788. }
  789. static void
  790. alternative_set_status(struct alternative *a, enum alternative_status status)
  791. {
  792. if (a->status == ALT_ST_UNKNOWN || status != a->status)
  793. a->modified = true;
  794. if (a->status != ALT_ST_UNKNOWN && status != a->status)
  795. log_msg("status of link group %s set to %s", a->master_link,
  796. alternative_status_string(status));
  797. a->status = status;
  798. }
  799. /* link must be allocated with malloc */
  800. static void
  801. alternative_set_link(struct alternative *a, char *linkname)
  802. {
  803. if (a->master_link == NULL || strcmp(linkname, a->master_link) != 0)
  804. a->modified = true;
  805. free(a->master_link);
  806. a->master_link = linkname;
  807. }
  808. static bool
  809. alternative_remove_choice(struct alternative *a, const char *file)
  810. {
  811. struct fileset *fs, *fs_prev;
  812. fs_prev = NULL;
  813. for (fs = a->choices; fs; fs = fs->next) {
  814. if (strcmp(fs->master_file, file) != 0) {
  815. fs_prev = fs;
  816. continue;
  817. }
  818. if (fs_prev)
  819. fs_prev->next = fs->next;
  820. else
  821. a->choices = fs->next;
  822. fileset_free(fs);
  823. a->modified = true;
  824. return true;
  825. }
  826. return false;
  827. }
  828. /*
  829. * Alternatives Database Load/Store functions.
  830. */
  831. enum altdb_flags {
  832. ALTDB_LAX_PARSER = 1 << 0,
  833. ALTDB_WARN_PARSER = 1 << 1,
  834. };
  835. struct altdb_context {
  836. FILE *fh;
  837. char *filename;
  838. enum altdb_flags flags;
  839. bool modified;
  840. void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  841. (*bad_format)(struct altdb_context *, const char *format, ...);
  842. jmp_buf on_error;
  843. };
  844. static int
  845. altdb_filter_namelist(const struct dirent *entry)
  846. {
  847. if (strcmp(entry->d_name, ".") == 0 ||
  848. strcmp(entry->d_name, "..") == 0 ||
  849. (strlen(entry->d_name) > strlen(ALT_TMP_EXT) &&
  850. strcmp(entry->d_name + strlen(entry->d_name) -
  851. strlen(ALT_TMP_EXT), ALT_TMP_EXT) == 0))
  852. return 0;
  853. return 1;
  854. }
  855. static int
  856. altdb_get_namelist(struct dirent ***table)
  857. {
  858. int count;
  859. count = scandir(admdir, table, altdb_filter_namelist, alphasort);
  860. if (count < 0)
  861. syserr(_("cannot scan directory '%.255s'"), admdir);
  862. return count;
  863. }
  864. static void
  865. altdb_free_namelist(struct dirent **table, int n)
  866. {
  867. while (n--)
  868. free(table[n]);
  869. free(table);
  870. }
  871. static char *
  872. altdb_get_line(struct altdb_context *ctx, const char *name)
  873. {
  874. char *buf, *line;
  875. size_t len, bufsz, i;
  876. bufsz = 1024;
  877. buf = xmalloc(bufsz);
  878. for (i = 0; true; i += strlen(line)) {
  879. errno = 0;
  880. line = fgets(buf + i, bufsz - i, ctx->fh);
  881. if (line) {
  882. if (strlen(buf) < bufsz - 1 || buf[bufsz - 2] == '\n')
  883. break;
  884. /* Need more space */
  885. bufsz *= 2;
  886. buf = realloc(buf, bufsz);
  887. if (!buf)
  888. error(_("failed to allocate memory"));
  889. continue;
  890. }
  891. if (feof(ctx->fh))
  892. ctx->bad_format(ctx, _("unexpected end of file while trying "
  893. "to read %s"), name);
  894. ctx->bad_format(ctx, _("while reading %s: %s"),
  895. name, strerror(errno));
  896. }
  897. len = strlen(buf);
  898. if (len == 0 || buf[len - 1] != '\n') {
  899. ctx->bad_format(ctx, _("line not terminated while trying "
  900. "to read %s"), name);
  901. }
  902. line[len - 1] = '\0';
  903. return buf;
  904. }
  905. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  906. altdb_parse_error(struct altdb_context *ctx, const char *format, ...)
  907. {
  908. char *msg;
  909. va_list args;
  910. va_start(args, format);
  911. xvasprintf(&msg, format, args);
  912. va_end(args);
  913. error(_("%s corrupt: %s"), ctx->filename, msg);
  914. }
  915. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  916. altdb_parse_stop(struct altdb_context *ctx, const char *format, ...)
  917. {
  918. longjmp(ctx->on_error, 1);
  919. }
  920. static void
  921. altdb_print_line(struct altdb_context *ctx, const char *line)
  922. {
  923. if (strchr(line, '\n') != NULL)
  924. error(_("newlines prohibited in update-alternatives files (%s)"),
  925. line);
  926. if (fprintf(ctx->fh, "%s\n", line) < (int) strlen(line) + 1)
  927. syserr(_("unable to write file '%s'"), ctx->filename);
  928. }
  929. static bool
  930. alternative_parse_slave(struct alternative *a, struct altdb_context *ctx)
  931. {
  932. char *name, *linkname;
  933. struct slave_link *sl;
  934. name = altdb_get_line(ctx, _("slave name"));
  935. if (!strlen(name)) { /* End of list */
  936. free(name);
  937. return false;
  938. }
  939. if (alternative_has_slave(a, name)) {
  940. sl = alternative_get_slave(a, name);
  941. free(name);
  942. ctx->bad_format(ctx, _("duplicate slave name %s"), sl->name);
  943. }
  944. linkname = altdb_get_line(ctx, _("slave link"));
  945. if (strcmp(linkname, a->master_link) == 0) {
  946. free(linkname);
  947. free(name);
  948. ctx->bad_format(ctx, _("slave link same as main link %s"),
  949. a->master_link);
  950. }
  951. for (sl = a->slaves; sl; sl = sl->next) {
  952. if (strcmp(linkname, sl->link) == 0) {
  953. free(linkname);
  954. free(name);
  955. ctx->bad_format(ctx, _("duplicate slave link %s"),
  956. sl->link);
  957. }
  958. }
  959. alternative_add_slave(a, name, linkname);
  960. return true;
  961. }
  962. static bool
  963. alternative_parse_fileset(struct alternative *a, struct altdb_context *ctx)
  964. {
  965. struct fileset *fs;
  966. struct slave_link *sl;
  967. struct stat st;
  968. char *master_file;
  969. master_file = altdb_get_line(ctx, _("master file"));
  970. if (!strlen(master_file)) { /* End of list */
  971. free(master_file);
  972. return false;
  973. }
  974. fs = alternative_get_fileset(a, master_file);
  975. if (fs)
  976. ctx->bad_format(ctx, _("duplicate path %s"), master_file);
  977. if (stat(master_file, &st)) {
  978. char *junk;
  979. if (errno != ENOENT)
  980. syserr(_("cannot stat file '%s'"), master_file);
  981. /* File not found - remove. */
  982. if (ctx->flags & ALTDB_WARN_PARSER)
  983. warning(_("alternative %s (part of link group %s) "
  984. "doesn't exist; removing from list of "
  985. "alternatives"), master_file, a->master_name);
  986. junk = altdb_get_line(ctx, _("priority"));
  987. free(junk);
  988. for (sl = a->slaves; sl; sl = sl->next) {
  989. junk = altdb_get_line(ctx, _("slave file"));
  990. free(junk);
  991. }
  992. ctx->modified = true;
  993. } else {
  994. char *prio_str, *prio_end;
  995. long prio;
  996. prio_str = altdb_get_line(ctx, _("priority"));
  997. errno = 0;
  998. prio = strtol(prio_str, &prio_end, 10);
  999. /* XXX: Leak master_file/prio_str on non-fatal error */
  1000. if (prio_str == prio_end || *prio_end != '\0')
  1001. ctx->bad_format(ctx, _("priority of %s: %s"),
  1002. master_file, prio_str);
  1003. if (prio < INT_MIN || prio > INT_MAX || errno == ERANGE)
  1004. ctx->bad_format(ctx,
  1005. _("priority of %s is out of range: %s"),
  1006. master_file, prio_str);
  1007. free(prio_str);
  1008. fs = fileset_new(master_file, prio);
  1009. for (sl = a->slaves; sl; sl = sl->next) {
  1010. fileset_add_slave(fs, xstrdup(sl->name),
  1011. altdb_get_line(ctx, _("slave file")));
  1012. }
  1013. alternative_add_choice(a, fs);
  1014. }
  1015. free(master_file);
  1016. return true;
  1017. }
  1018. static bool
  1019. alternative_load(struct alternative *a, enum altdb_flags flags)
  1020. {
  1021. struct altdb_context ctx;
  1022. struct stat st;
  1023. char *fn, *status;
  1024. /* Initialize parse context */
  1025. if (setjmp(ctx.on_error)) {
  1026. if (ctx.fh)
  1027. fclose(ctx.fh);
  1028. free(ctx.filename);
  1029. alternative_reset(a);
  1030. return false;
  1031. }
  1032. ctx.modified = false;
  1033. ctx.flags = flags;
  1034. if (flags & ALTDB_LAX_PARSER)
  1035. ctx.bad_format = altdb_parse_stop;
  1036. else
  1037. ctx.bad_format = altdb_parse_error;
  1038. xasprintf(&fn, "%s/%s", admdir, a->master_name);
  1039. ctx.filename = fn;
  1040. /* Open the alternative file. */
  1041. ctx.fh = fopen(ctx.filename, "r");
  1042. if (ctx.fh == NULL) {
  1043. if (errno == ENOENT)
  1044. return false;
  1045. syserr(_("unable to open file '%s'"), ctx.filename);
  1046. }
  1047. /* Verify the alternative is not empty. */
  1048. if (fstat(fileno(ctx.fh), &st) == -1)
  1049. syserr(_("cannot stat file '%s'"), ctx.filename);
  1050. if (st.st_size == 0)
  1051. return false;
  1052. /* Start parsing mandatory attributes (link+status) of the alternative */
  1053. alternative_reset(a);
  1054. status = altdb_get_line(&ctx, _("status"));
  1055. if (strcmp(status, "auto") != 0 && strcmp(status, "manual") != 0)
  1056. ctx.bad_format(&ctx, _("invalid status"));
  1057. alternative_set_status(a, (strcmp(status, "auto") == 0) ?
  1058. ALT_ST_AUTO : ALT_ST_MANUAL);
  1059. free(status);
  1060. alternative_set_link(a, altdb_get_line(&ctx, _("master link")));
  1061. /* Parse the description of the slaves links of the alternative */
  1062. while (alternative_parse_slave(a, &ctx));
  1063. /* Parse the available choices in the alternative */
  1064. while (alternative_parse_fileset(a, &ctx)) ;
  1065. /* Close database file */
  1066. if (fclose(ctx.fh))
  1067. syserr(_("unable to close file '%s'"), ctx.filename);
  1068. free(ctx.filename);
  1069. /* Initialize the modified field which has been erroneously changed
  1070. * by the various alternative_(add|set)_* calls:
  1071. * false unless a choice has been auto-cleaned */
  1072. a->modified = ctx.modified;
  1073. return true;
  1074. }
  1075. static void
  1076. alternative_save(struct alternative *a)
  1077. {
  1078. struct altdb_context ctx;
  1079. struct slave_link *sl, *sl_prev;
  1080. struct fileset *fs;
  1081. char *filenew, *file;
  1082. /* Cleanup unused slaves before writing admin file. */
  1083. sl_prev = NULL;
  1084. for (sl = a->slaves; sl; sl_prev = sl, sl = sl->next) {
  1085. bool has_slave = false;
  1086. for (fs = a->choices; fs; fs = fs->next) {
  1087. if (fileset_has_slave(fs, sl->name)) {
  1088. has_slave = true;
  1089. break;
  1090. }
  1091. }
  1092. if (!has_slave) {
  1093. struct slave_link *sl_rm;
  1094. verbose(_("discarding obsolete slave link %s (%s)"),
  1095. sl->name, sl->link);
  1096. if (sl_prev)
  1097. sl_prev->next = sl->next;
  1098. else
  1099. a->slaves = sl->next;
  1100. sl_rm = sl;
  1101. sl = sl_prev ? sl_prev : a->slaves;
  1102. slave_link_free(sl_rm);
  1103. if (!sl)
  1104. break; /* No other slave left. */
  1105. }
  1106. }
  1107. /* Sort entries */
  1108. alternative_sort_slaves(a);
  1109. alternative_sort_choices(a);
  1110. /* Write admin file. */
  1111. xasprintf(&file, "%s/%s", admdir, a->master_name);
  1112. xasprintf(&filenew, "%s" ALT_TMP_EXT, file);
  1113. ctx.filename = filenew;
  1114. ctx.fh = fopen(ctx.filename, "w");
  1115. if (ctx.fh == NULL)
  1116. syserr(_("unable to create file '%s'"), ctx.filename);
  1117. altdb_print_line(&ctx, alternative_status_string(a->status));
  1118. altdb_print_line(&ctx, a->master_link);
  1119. for (sl = a->slaves; sl; sl = sl->next) {
  1120. altdb_print_line(&ctx, sl->name);
  1121. altdb_print_line(&ctx, sl->link);
  1122. }
  1123. altdb_print_line(&ctx, "");
  1124. for (fs = a->choices; fs; fs = fs->next) {
  1125. char *prio;
  1126. altdb_print_line(&ctx, fs->master_file);
  1127. xasprintf(&prio, "%d", fs->priority);
  1128. altdb_print_line(&ctx, prio);
  1129. free(prio);
  1130. for (sl = a->slaves; sl; sl = sl->next) {
  1131. if (fileset_has_slave(fs, sl->name))
  1132. altdb_print_line(&ctx,
  1133. fileset_get_slave(fs, sl->name));
  1134. else
  1135. altdb_print_line(&ctx, "");
  1136. }
  1137. }
  1138. altdb_print_line(&ctx, "");
  1139. /* Close database file */
  1140. if (fflush(ctx.fh))
  1141. syserr(_("unable to flush file '%s'"), ctx.filename);
  1142. if (fsync(fileno(ctx.fh)))
  1143. syserr(_("unable to sync file '%s'"), ctx.filename);
  1144. if (fclose(ctx.fh))
  1145. syserr(_("unable to close file '%s'"), ctx.filename);
  1146. /* Put in place atomically. */
  1147. checked_mv(filenew, file);
  1148. free(filenew);
  1149. free(file);
  1150. }
  1151. static const char *
  1152. alternative_set_current(struct alternative *a, char *new_choice)
  1153. {
  1154. a->known_current = true;
  1155. a->current = new_choice;
  1156. return new_choice;
  1157. }
  1158. static const char *
  1159. alternative_get_current(struct alternative *a)
  1160. {
  1161. struct stat st;
  1162. char *curlink;
  1163. char *file;
  1164. if (a->known_current)
  1165. return a->current;
  1166. xasprintf(&curlink, "%s/%s", altdir, a->master_name);
  1167. if (lstat(curlink, &st)) {
  1168. if (errno == ENOENT) {
  1169. free(curlink);
  1170. return alternative_set_current(a, NULL);
  1171. }
  1172. syserr(_("cannot stat file '%s'"), curlink);
  1173. }
  1174. file = xreadlink(curlink);
  1175. free(curlink);
  1176. return alternative_set_current(a, file);
  1177. }
  1178. static struct fileset *
  1179. alternative_get_best(struct alternative *a)
  1180. {
  1181. struct fileset *fs, *best;
  1182. const char *current;
  1183. current = alternative_get_current(a);
  1184. if (current)
  1185. best = alternative_get_fileset(a, current);
  1186. else
  1187. best = NULL;
  1188. if (best == NULL)
  1189. best = a->choices;
  1190. for (fs = a->choices; fs; fs = fs->next)
  1191. if (fs->priority > best->priority)
  1192. best = fs;
  1193. return best;
  1194. }
  1195. static void
  1196. alternative_display_query(struct alternative *a)
  1197. {
  1198. struct fileset *best, *fs;
  1199. struct slave_link *sl;
  1200. const char *current;
  1201. pr("Name: %s", a->master_name);
  1202. pr("Link: %s", a->master_link);
  1203. if (alternative_slaves_count(a) > 0) {
  1204. pr("Slaves:");
  1205. for (sl = a->slaves; sl; sl = sl->next)
  1206. pr(" %s %s", sl->name, sl->link);
  1207. }
  1208. pr("Status: %s", alternative_status_string(a->status));
  1209. best = alternative_get_best(a);
  1210. if (best)
  1211. pr("Best: %s", best->master_file);
  1212. current = alternative_get_current(a);
  1213. pr("Value: %s", current ? current : "none");
  1214. for (fs = a->choices; fs; fs = fs->next) {
  1215. printf("\n");
  1216. pr("Alternative: %s", fs->master_file);
  1217. pr("Priority: %d", fs->priority);
  1218. if (alternative_slaves_count(a) == 0)
  1219. continue;
  1220. pr("Slaves:");
  1221. for (sl = a->slaves; sl; sl = sl->next) {
  1222. if (fileset_has_slave(fs, sl->name))
  1223. pr(" %s %s", sl->name,
  1224. fileset_get_slave(fs, sl->name));
  1225. }
  1226. }
  1227. }
  1228. static void
  1229. alternative_display_user(struct alternative *a)
  1230. {
  1231. const char *current;
  1232. struct fileset *fs;
  1233. struct slave_link *sl;
  1234. pr("%s - %s", a->master_name, alternative_status_describe(a->status));
  1235. fs = alternative_get_best(a);
  1236. if (fs)
  1237. pr(_(" link best version is %s"), fs->master_file);
  1238. else
  1239. pr(_(" link best version not available"));
  1240. current = alternative_get_current(a);
  1241. if (current) {
  1242. pr(_(" link currently points to %s"), current);
  1243. } else {
  1244. pr(_(" link currently absent"));
  1245. }
  1246. pr(_(" link %s is %s"), a->master_name, a->master_link);
  1247. for (sl = a->slaves; sl; sl = sl->next)
  1248. pr(_(" slave %s is %s"), sl->name, sl->link);
  1249. for (fs = a->choices; fs; fs = fs->next) {
  1250. pr(_("%s - priority %d"), fs->master_file, fs->priority);
  1251. for (sl = a->slaves; sl; sl = sl->next) {
  1252. if (fileset_has_slave(fs, sl->name))
  1253. pr(_(" slave %s: %s"), sl->name,
  1254. fileset_get_slave(fs, sl->name));
  1255. }
  1256. }
  1257. }
  1258. static void
  1259. alternative_display_list(struct alternative *a)
  1260. {
  1261. struct fileset *fs;
  1262. for (fs = a->choices; fs; fs = fs->next)
  1263. pr("%s", fs->master_file);
  1264. }
  1265. static void
  1266. alternative_print_choice(struct alternative *a, enum alternative_status status,
  1267. struct fileset *fs, int idx, int len)
  1268. {
  1269. const char *current = alternative_get_current(a);
  1270. int mark;
  1271. if (a->status == status &&
  1272. current && strcmp(current, fs->master_file) == 0)
  1273. mark = '*';
  1274. else
  1275. mark = ' ';
  1276. pr("%c %-12d %-*s % -10d %s", mark, idx, len,
  1277. fs->master_file, fs->priority, alternative_status_describe(status));
  1278. }
  1279. static const char *
  1280. alternative_select_choice(struct alternative *a)
  1281. {
  1282. const char *current;
  1283. char *ret, selection[_POSIX_PATH_MAX];
  1284. struct fileset *best, *fs;
  1285. int n_choices;
  1286. int len, idx;
  1287. n_choices = alternative_choices_count(a);
  1288. current = alternative_get_current(a);
  1289. best = alternative_get_best(a);
  1290. assert(best);
  1291. len = 15;
  1292. for (fs = a->choices; fs; fs = fs->next)
  1293. len = max(len, (int)strlen(fs->master_file) + 1);
  1294. for (;;) {
  1295. pr(P_("There is %d choice for the alternative %s (providing %s).",
  1296. "There are %d choices for the alternative %s (providing %s).",
  1297. n_choices), n_choices, a->master_name, a->master_link);
  1298. printf("\n");
  1299. pr(" %-12.12s %-*.*s %-10.10s %s", _("Selection"), len, len,
  1300. _("Path"), _("Priority"), _("Status"));
  1301. pr("------------------------------------------------------------");
  1302. idx = 0;
  1303. alternative_print_choice(a, ALT_ST_AUTO, best, idx++, len);
  1304. for (fs = a->choices; fs; fs = fs->next, idx++)
  1305. alternative_print_choice(a, ALT_ST_MANUAL, fs, idx, len);
  1306. printf("\n");
  1307. printf(_("Press <enter> to keep the current choice[*], "
  1308. "or type selection number: "));
  1309. ret = fgets(selection, sizeof(selection), stdin);
  1310. if (ret == NULL || strlen(selection) == 0) {
  1311. return NULL;
  1312. }
  1313. selection[strlen(selection) - 1] = '\0';
  1314. if (strlen(selection) == 0)
  1315. return xstrdup(current);
  1316. errno = 0;
  1317. idx = strtol(selection, &ret, 10);
  1318. if (idx < 0 || errno != 0)
  1319. continue;
  1320. if (*ret == '\0') {
  1321. /* Look up by index */
  1322. if (idx == 0) {
  1323. alternative_set_status(a, ALT_ST_AUTO);
  1324. return xstrdup(best->master_file);
  1325. }
  1326. idx--;
  1327. for (fs = a->choices; idx && fs; idx--)
  1328. fs = fs->next;
  1329. if (fs) {
  1330. alternative_set_status(a, ALT_ST_MANUAL);
  1331. return xstrdup(fs->master_file);
  1332. }
  1333. } else {
  1334. /* Look up by name */
  1335. fs = alternative_get_fileset(a, selection);
  1336. if (fs) {
  1337. alternative_set_status(a, ALT_ST_MANUAL);
  1338. return xstrdup(selection);
  1339. }
  1340. }
  1341. }
  1342. }
  1343. static const char *
  1344. alternative_config(struct alternative *a, const char *current_choice)
  1345. {
  1346. const char *new_choice = NULL;
  1347. if (alternative_choices_count(a) == 0) {
  1348. pr(_("There is no program which provides %s."),
  1349. a->master_name);
  1350. pr(_("Nothing to configure."));
  1351. } else if (opt_skip_auto && a->status == ALT_ST_AUTO) {
  1352. alternative_display_user(a);
  1353. } else if (alternative_choices_count(a) == 1 &&
  1354. a->status == ALT_ST_AUTO &&
  1355. current_choice != NULL) {
  1356. pr(_("There is only one alternative in link group %s (providing %s): %s"),
  1357. a->master_name, a->master_link, current_choice);
  1358. pr(_("Nothing to configure."));
  1359. } else {
  1360. new_choice = alternative_select_choice(a);
  1361. }
  1362. return new_choice;
  1363. }
  1364. static void
  1365. alternative_add_commit_op(struct alternative *a, enum opcode opcode,
  1366. const char *arg_a, const char *arg_b)
  1367. {
  1368. struct commit_operation *op, *cur;
  1369. op = xmalloc(sizeof(*op));
  1370. op->opcode = opcode;
  1371. op->arg_a = xstrdup(arg_a);
  1372. op->arg_b = xstrdup(arg_b);
  1373. op->next = NULL;
  1374. /* Add at the end */
  1375. cur = a->commit_ops;
  1376. while (cur && cur->next)
  1377. cur = cur->next;
  1378. if (cur)
  1379. cur->next = op;
  1380. else
  1381. a->commit_ops = op;
  1382. }
  1383. static void
  1384. alternative_commit(struct alternative *a)
  1385. {
  1386. struct commit_operation *op;
  1387. for (op = a->commit_ops; op; op = op->next) {
  1388. switch (op->opcode) {
  1389. case OPCODE_NOP:
  1390. break;
  1391. case OPCODE_RM:
  1392. checked_rm(op->arg_a);
  1393. break;
  1394. case OPCODE_MV:
  1395. checked_mv(op->arg_a, op->arg_b);
  1396. break;
  1397. }
  1398. }
  1399. alternative_commit_operations_free(a);
  1400. }
  1401. enum alternative_path_status {
  1402. ALT_PATH_SYMLINK,
  1403. ALT_PATH_MISSING,
  1404. ALT_PATH_OTHER,
  1405. };
  1406. static enum alternative_path_status
  1407. alternative_path_classify(const char *linkname)
  1408. {
  1409. struct stat st;
  1410. errno = 0;
  1411. if (lstat(linkname, &st) == -1) {
  1412. if (errno != ENOENT)
  1413. syserr(_("cannot stat file '%s'"), linkname);
  1414. return ALT_PATH_MISSING;
  1415. } else if (S_ISLNK(st.st_mode)) {
  1416. return ALT_PATH_SYMLINK;
  1417. } else {
  1418. return ALT_PATH_OTHER;
  1419. }
  1420. }
  1421. static bool
  1422. alternative_path_can_remove(const char *linkname)
  1423. {
  1424. if (opt_force)
  1425. return true;
  1426. if (alternative_path_classify(linkname) == ALT_PATH_OTHER)
  1427. return false;
  1428. else
  1429. return true;
  1430. }
  1431. static bool
  1432. alternative_path_needs_update(const char *linkname, const char *filename)
  1433. {
  1434. char *linktarget;
  1435. bool update;
  1436. if (opt_force)
  1437. return true;
  1438. switch (alternative_path_classify(linkname)) {
  1439. case ALT_PATH_SYMLINK:
  1440. linktarget = xreadlink(linkname);
  1441. if (strcmp(linktarget, filename) == 0)
  1442. update = false;
  1443. else
  1444. update = true;
  1445. free(linktarget);
  1446. return update;
  1447. case ALT_PATH_OTHER:
  1448. warning(_("not replacing %s with a link"), linkname);
  1449. return false;
  1450. case ALT_PATH_MISSING:
  1451. default:
  1452. return true;
  1453. }
  1454. }
  1455. static void
  1456. alternative_prepare_install_single(struct alternative *a, const char *name,
  1457. const char *linkname, const char *file)
  1458. {
  1459. char *fntmp, *fn;
  1460. /* Create link in /etc/alternatives. */
  1461. xasprintf(&fntmp, "%s/%s" ALT_TMP_EXT, altdir, name);
  1462. xasprintf(&fn, "%s/%s", altdir, name);
  1463. checked_rm(fntmp);
  1464. checked_symlink(file, fntmp);
  1465. alternative_add_commit_op(a, OPCODE_MV, fntmp, fn);
  1466. free(fntmp);
  1467. if (alternative_path_needs_update(linkname, fn)) {
  1468. /* Create alternative link. */
  1469. xasprintf(&fntmp, "%s" ALT_TMP_EXT, linkname);
  1470. checked_rm(fntmp);
  1471. checked_symlink(fn, fntmp);
  1472. alternative_add_commit_op(a, OPCODE_MV, fntmp, linkname);
  1473. free(fntmp);
  1474. }
  1475. free(fn);
  1476. }
  1477. static void
  1478. alternative_prepare_install(struct alternative *a, const char *choice)
  1479. {
  1480. struct slave_link *sl;
  1481. struct fileset *fs;
  1482. fs = alternative_get_fileset(a, choice);
  1483. if (fs == NULL)
  1484. error(_("can't install unknown choice %s"), choice);
  1485. /* Take care of master alternative */
  1486. alternative_prepare_install_single(a, a->master_name, a->master_link,
  1487. choice);
  1488. /* Take care of slaves alternatives */
  1489. for (sl = a->slaves; sl; sl = sl->next) {
  1490. char *fn;
  1491. if (fileset_can_install_slave(fs, sl->name)) {
  1492. alternative_prepare_install_single(a, sl->name,
  1493. sl->link, fileset_get_slave(fs, sl->name));
  1494. continue;
  1495. }
  1496. /* Slave can't be installed */
  1497. if (fileset_has_slave(fs, sl->name))
  1498. warning(_("skip creation of %s because associated "
  1499. "file %s (of link group %s) doesn't exist"),
  1500. sl->link, fileset_get_slave(fs, sl->name),
  1501. a->master_name);
  1502. /* Drop unused slave. */
  1503. xasprintf(&fn, "%s/%s", altdir, sl->name);
  1504. if (alternative_path_can_remove(sl->link))
  1505. alternative_add_commit_op(a, OPCODE_RM, sl->link, NULL);
  1506. else
  1507. warning(_("not removing %s since it's not a symlink"),
  1508. sl->link);
  1509. alternative_add_commit_op(a, OPCODE_RM, fn, NULL);
  1510. free(fn);
  1511. }
  1512. }
  1513. static void
  1514. alternative_remove_files(struct alternative *a)
  1515. {
  1516. struct slave_link *sl;
  1517. checked_rm_args("%s" ALT_TMP_EXT, a->master_link);
  1518. if (alternative_path_can_remove(a->master_link))
  1519. checked_rm(a->master_link);
  1520. checked_rm_args("%s/%s" ALT_TMP_EXT, altdir, a->master_name);
  1521. checked_rm_args("%s/%s", altdir, a->master_name);
  1522. for (sl = a->slaves; sl; sl = sl->next) {
  1523. checked_rm_args("%s" ALT_TMP_EXT, sl->link);
  1524. if (alternative_path_can_remove(sl->link))
  1525. checked_rm(sl->link);
  1526. checked_rm_args("%s/%s" ALT_TMP_EXT, altdir, sl->name);
  1527. checked_rm_args("%s/%s", altdir, sl->name);
  1528. }
  1529. /* Drop admin file */
  1530. checked_rm_args("%s/%s", admdir, a->master_name);
  1531. }
  1532. static const char *
  1533. alternative_remove(struct alternative *a, const char *current_choice,
  1534. const char *path)
  1535. {
  1536. const char *new_choice = NULL;
  1537. if (alternative_has_choice(a, path))
  1538. alternative_remove_choice(a, path);
  1539. else
  1540. verbose(_("alternative %s for %s not registered; not removing"),
  1541. path, a->master_name);
  1542. if (current_choice && strcmp(current_choice, path) == 0) {
  1543. struct fileset *best;
  1544. /* Current choice is removed. */
  1545. if (a->status == ALT_ST_MANUAL) {
  1546. /* And it was manual, switch to auto. */
  1547. info(_("removing manually selected alternative "
  1548. "- switching %s to auto mode"),
  1549. a->master_name);
  1550. alternative_set_status(a, ALT_ST_AUTO);
  1551. }
  1552. best = alternative_get_best(a);
  1553. if (best)
  1554. new_choice = best->master_file;
  1555. }
  1556. return new_choice;
  1557. }
  1558. static bool
  1559. alternative_has_broken_slave(struct slave_link *sl, struct fileset *fs)
  1560. {
  1561. if (fileset_can_install_slave(fs, sl->name)) {
  1562. char *wanted;
  1563. char *sl_altlnk, *sl_current;
  1564. /* Verify link -> /etc/alternatives/foo */
  1565. sl_altlnk = areadlink(sl->link);
  1566. if (!sl_altlnk)
  1567. return true;
  1568. xasprintf(&wanted, "%s/%s", altdir, sl->name);
  1569. if (strcmp(sl_altlnk, wanted) != 0) {
  1570. free(wanted);
  1571. free(sl_altlnk);
  1572. return true;
  1573. }
  1574. free(sl_altlnk);
  1575. /* Verify /etc/alternatives/foo -> file */
  1576. sl_current = areadlink(wanted);
  1577. free(wanted);
  1578. if (!sl_current)
  1579. return true;
  1580. if (strcmp(sl_current, fileset_get_slave(fs, sl->name)) != 0) {
  1581. free(sl_current);
  1582. return true;
  1583. }
  1584. free(sl_current);
  1585. } else {
  1586. char *sl_altlnk;
  1587. /* Slave link must not exist. */
  1588. if (alternative_path_classify(sl->link) != ALT_PATH_MISSING)
  1589. return true;
  1590. xasprintf(&sl_altlnk, "%s/%s", altdir, sl->name);
  1591. if (alternative_path_classify(sl_altlnk) != ALT_PATH_MISSING) {
  1592. free(sl_altlnk);
  1593. return true;
  1594. }
  1595. free(sl_altlnk);
  1596. }
  1597. return false;
  1598. }
  1599. static enum alternative_update_reason
  1600. alternative_needs_update(struct alternative *a)
  1601. {
  1602. enum alternative_update_reason reason = ALT_UPDATE_NO;
  1603. const char *current;
  1604. char *altlnk, *wanted;
  1605. struct fileset *fs;
  1606. struct slave_link *sl;
  1607. /* Check master link */
  1608. altlnk = areadlink(a->master_link);
  1609. if (!altlnk)
  1610. return ALT_UPDATE_LINK_BROKEN;
  1611. xasprintf(&wanted, "%s/%s", altdir, a->master_name);
  1612. if (strcmp(altlnk, wanted) != 0) {
  1613. free(wanted);
  1614. free(altlnk);
  1615. return ALT_UPDATE_LINK_BROKEN;
  1616. }
  1617. free(wanted);
  1618. free(altlnk);
  1619. /* Stop if we have an unmanaged alternative */
  1620. current = alternative_get_current(a);
  1621. if (current == NULL)
  1622. return ALT_UPDATE_LINK_BROKEN;
  1623. fs = alternative_get_fileset(a, current);
  1624. /* Stop if we do not have the choice. */
  1625. if (fs == NULL)
  1626. return ALT_UPDATE_NO;
  1627. /* Check slaves */
  1628. for (sl = a->slaves; sl; sl = sl->next) {
  1629. if (alternative_has_broken_slave(sl, fs)) {
  1630. if (sl->updated)
  1631. reason = ALT_UPDATE_SLAVE_CHANGED;
  1632. else
  1633. return ALT_UPDATE_LINK_BROKEN;
  1634. }
  1635. }
  1636. return reason;
  1637. }
  1638. struct alternative_map {
  1639. struct alternative_map *next;
  1640. const char *key;
  1641. struct alternative *item;
  1642. };
  1643. static struct alternative_map *
  1644. alternative_map_new(const char *key, struct alternative *a)
  1645. {
  1646. struct alternative_map *am;
  1647. am = xmalloc(sizeof(*am));
  1648. am->next = NULL;
  1649. am->key = key;
  1650. am->item = a;
  1651. return am;
  1652. }
  1653. static struct alternative *
  1654. alternative_map_find(struct alternative_map *am, const char *key)
  1655. {
  1656. for (; am; am = am->next)
  1657. if (am->key && strcmp(am->key, key) == 0)
  1658. return am->item;
  1659. return NULL;
  1660. }
  1661. static void
  1662. alternative_map_add(struct alternative_map *am, const char *key,
  1663. struct alternative *a)
  1664. {
  1665. if (am->key == NULL) {
  1666. am->key = key;
  1667. am->item = a;
  1668. } else {
  1669. struct alternative_map *new = alternative_map_new(key, a);
  1670. while (am->next)
  1671. am = am->next;
  1672. am->next = new;
  1673. }
  1674. }
  1675. static void
  1676. alternative_map_load_names(struct alternative_map *alt_map_obj)
  1677. {
  1678. struct dirent **table;
  1679. int i, count;
  1680. count = altdb_get_namelist(&table);
  1681. for (i = 0; i < count; i++) {
  1682. struct alternative *a_new = alternative_new(table[i]->d_name);
  1683. if (!alternative_load(a_new, ALTDB_LAX_PARSER)) {
  1684. alternative_free(a_new);
  1685. continue;
  1686. }
  1687. alternative_map_add(alt_map_obj, a_new->master_name, a_new);
  1688. }
  1689. altdb_free_namelist(table, count);
  1690. }
  1691. static void
  1692. alternative_map_load_tree(struct alternative_map *alt_map_links,
  1693. struct alternative_map *alt_map_parent)
  1694. {
  1695. struct dirent **table;
  1696. int i, count;
  1697. count = altdb_get_namelist(&table);
  1698. for (i = 0; i < count; i++) {
  1699. struct slave_link *sl;
  1700. struct alternative *a_new = alternative_new(table[i]->d_name);
  1701. if (!alternative_load(a_new, ALTDB_LAX_PARSER)) {
  1702. alternative_free(a_new);
  1703. continue;
  1704. }
  1705. alternative_map_add(alt_map_links, a_new->master_link, a_new);
  1706. alternative_ref(a_new);
  1707. alternative_map_add(alt_map_parent, a_new->master_name, a_new);
  1708. for (sl = a_new->slaves; sl; sl = sl->next) {
  1709. alternative_ref(a_new);
  1710. alternative_map_add(alt_map_links, sl->link, a_new);
  1711. alternative_ref(a_new);
  1712. alternative_map_add(alt_map_parent, sl->name, a_new);
  1713. }
  1714. }
  1715. altdb_free_namelist(table, count);
  1716. }
  1717. static void
  1718. alternative_map_free(struct alternative_map *am)
  1719. {
  1720. struct alternative_map *am_next;
  1721. while (am) {
  1722. am_next = am->next;
  1723. if (am->item)
  1724. alternative_free(am->item);
  1725. free(am);
  1726. am = am_next;
  1727. }
  1728. }
  1729. static const char *
  1730. alternative_set_manual(struct alternative *a, const char *path)
  1731. {
  1732. const char *new_choice = NULL;
  1733. if (alternative_has_choice(a, path))
  1734. new_choice = path;
  1735. else
  1736. error(_("alternative %s for %s not registered; "
  1737. "not setting"), path, a->master_name);
  1738. alternative_set_status(a, ALT_ST_MANUAL);
  1739. return new_choice;
  1740. }
  1741. static const char *
  1742. alternative_set_auto(struct alternative *a)
  1743. {
  1744. const char *new_choice = NULL;
  1745. alternative_set_status(a, ALT_ST_AUTO);
  1746. if (alternative_choices_count(a) == 0)
  1747. pr(_("There is no program which provides %s."),
  1748. a->master_name);
  1749. else
  1750. new_choice = alternative_get_best(a)->master_file;
  1751. return new_choice;
  1752. }
  1753. static const char *
  1754. get_argv_string(int argc, char **argv)
  1755. {
  1756. static char string[2048];
  1757. size_t cur_len;
  1758. int i;
  1759. string[0] = '\0';
  1760. cur_len = 0;
  1761. for (i = 1; i < argc; i++) {
  1762. size_t arg_len = strlen(argv[i]);
  1763. if (cur_len + arg_len + 2 > sizeof(string))
  1764. break;
  1765. if (cur_len) {
  1766. strcpy(string + cur_len, " ");
  1767. cur_len++;
  1768. }
  1769. strcpy(string + cur_len, argv[i]);
  1770. cur_len += arg_len;
  1771. }
  1772. return string;
  1773. }
  1774. static void
  1775. alternative_select_mode(struct alternative *a, const char *current_choice)
  1776. {
  1777. if (current_choice) {
  1778. /* Detect manually modified alternative, switch to manual. */
  1779. if (!alternative_has_choice(a, current_choice)) {
  1780. struct stat st;
  1781. errno = 0;
  1782. if (stat(current_choice, &st) == -1 && errno != ENOENT)
  1783. syserr(_("cannot stat file '%s'"), current_choice);
  1784. if (errno == ENOENT) {
  1785. warning(_("%s/%s is dangling; it will be updated "
  1786. "with best choice"), altdir, a->master_name);
  1787. alternative_set_status(a, ALT_ST_AUTO);
  1788. } else if (a->status != ALT_ST_MANUAL) {
  1789. warning(_("%s/%s has been changed (manually or by "
  1790. "a script); switching to manual "
  1791. "updates only"), altdir, a->master_name);
  1792. alternative_set_status(a, ALT_ST_MANUAL);
  1793. }
  1794. }
  1795. } else {
  1796. /* Lack of alternative link => automatic mode. */
  1797. verbose(_("setting up automatic selection of %s"),
  1798. a->master_name);
  1799. alternative_set_status(a, ALT_ST_AUTO);
  1800. }
  1801. }
  1802. static void
  1803. alternative_evolve_slave(struct alternative *a, const char *cur_choice,
  1804. struct slave_link *sl, struct fileset *fs)
  1805. {
  1806. struct stat st;
  1807. char *new_file = NULL;
  1808. const char *old, *new;
  1809. old = alternative_get_slave(a, sl->name)->link;
  1810. new = sl->link;
  1811. if (cur_choice && strcmp(cur_choice, fs->master_file) == 0) {
  1812. new_file = xstrdup(fileset_get_slave(fs, sl->name));
  1813. } else {
  1814. char *lnk;
  1815. xasprintf(&lnk, "%s/%s", altdir, sl->name);
  1816. new_file = areadlink(lnk);
  1817. free(lnk);
  1818. }
  1819. if (strcmp(old, new) != 0 &&
  1820. alternative_path_classify(old) == ALT_PATH_SYMLINK) {
  1821. bool rename_link = false;
  1822. if (new_file) {
  1823. errno = 0;
  1824. if (stat(new_file, &st) == -1 && errno != ENOENT)
  1825. syserr(_("cannot stat file '%s'"),
  1826. new_file);
  1827. rename_link = (errno == 0);
  1828. }
  1829. if (rename_link) {
  1830. info(_("renaming %s slave link from %s to %s"),
  1831. sl->name, old, new);
  1832. checked_mv(old, new);
  1833. } else {
  1834. checked_rm(old);
  1835. }
  1836. sl->updated = true;
  1837. }
  1838. free(new_file);
  1839. }
  1840. static void
  1841. alternative_evolve(struct alternative *a, struct alternative *b,
  1842. const char *cur_choice, struct fileset *fs)
  1843. {
  1844. struct slave_link *sl;
  1845. bool is_link;
  1846. is_link = alternative_path_classify(a->master_link) == ALT_PATH_SYMLINK;
  1847. if (is_link && strcmp(a->master_link, b->master_link) != 0) {
  1848. info(_("renaming %s link from %s to %s"), b->master_name,
  1849. a->master_link, b->master_link);
  1850. checked_mv(a->master_link, b->master_link);
  1851. }
  1852. alternative_set_link(a, xstrdup(b->master_link));
  1853. /* Check if new slaves have been added, or existing
  1854. * ones renamed. */
  1855. for (sl = b->slaves; sl; sl = sl->next) {
  1856. if (alternative_has_slave(a, sl->name))
  1857. alternative_evolve_slave(a, cur_choice, sl, fs);
  1858. else
  1859. sl->updated = true;
  1860. alternative_copy_slave(a, sl);
  1861. }
  1862. }
  1863. static void
  1864. alternative_update(struct alternative *a,
  1865. const char *current_choice, const char *new_choice)
  1866. {
  1867. enum alternative_update_reason reason;
  1868. /* No choice left, remove everything. */
  1869. if (!alternative_choices_count(a)) {
  1870. log_msg("link group %s fully removed", a->master_name);
  1871. alternative_remove_files(a);
  1872. return;
  1873. }
  1874. /* New choice wanted. */
  1875. if (new_choice &&
  1876. (!current_choice || strcmp(new_choice, current_choice) != 0)) {
  1877. log_msg("link group %s updated to point to %s", a->master_name,
  1878. new_choice);
  1879. if (a->status == ALT_ST_AUTO)
  1880. info(_("using %s to provide %s (%s) in auto mode"),
  1881. new_choice, a->master_link, a->master_name);
  1882. else
  1883. info(_("using %s to provide %s (%s) in manual mode"),
  1884. new_choice, a->master_link, a->master_name);
  1885. debug("prepare_install(%s)", new_choice);
  1886. alternative_prepare_install(a, new_choice);
  1887. } else if ((reason = alternative_needs_update(a))) {
  1888. if (reason == ALT_UPDATE_SLAVE_CHANGED) {
  1889. log_msg("link group %s updated with changed slaves",
  1890. a->master_name);
  1891. info(_("updating alternative %s "
  1892. "because link group %s has changed slave links"),
  1893. current_choice, a->master_name);
  1894. } else {
  1895. log_msg("auto-repair link group %s", a->master_name);
  1896. warning(_("forcing reinstallation of alternative %s "
  1897. "because link group %s is broken"),
  1898. current_choice, a->master_name);
  1899. }
  1900. if (current_choice && !alternative_has_choice(a, current_choice)) {
  1901. struct fileset *best = alternative_get_best(a);
  1902. warning(_("current alternative %s is unknown, "
  1903. "switching to %s for link group %s"),
  1904. current_choice, best->master_file,
  1905. a->master_name);
  1906. current_choice = best->master_file;
  1907. alternative_set_status(a, ALT_ST_AUTO);
  1908. }
  1909. if (current_choice)
  1910. alternative_prepare_install(a, current_choice);
  1911. }
  1912. /* Save administrative file if needed. */
  1913. if (a->modified) {
  1914. debug("%s is modified and will be saved", a->master_name);
  1915. alternative_save(a);
  1916. }
  1917. /* Replace all symlinks in one pass. */
  1918. alternative_commit(a);
  1919. }
  1920. static void
  1921. alternative_config_all(void)
  1922. {
  1923. struct alternative_map *alt_map_obj;
  1924. struct alternative_map *am;
  1925. alt_map_obj = alternative_map_new(NULL, NULL);
  1926. alternative_map_load_names(alt_map_obj);
  1927. for (am = alt_map_obj; am && am->item; am = am->next) {
  1928. const char *current_choice;
  1929. const char *new_choice;
  1930. current_choice = alternative_get_current(am->item);
  1931. alternative_select_mode(am->item, current_choice);
  1932. new_choice = alternative_config(am->item, current_choice);
  1933. alternative_update(am->item, current_choice, new_choice);
  1934. free(new_choice);
  1935. }
  1936. alternative_map_free(alt_map_obj);
  1937. }
  1938. static void
  1939. alternative_get_selections(void)
  1940. {
  1941. struct alternative_map *alt_map_obj;
  1942. struct alternative_map *am;
  1943. alt_map_obj = alternative_map_new(NULL, NULL);
  1944. alternative_map_load_names(alt_map_obj);
  1945. for (am = alt_map_obj; am && am->item; am = am->next) {
  1946. const char *current;
  1947. current = alternative_get_current(am->item);
  1948. printf("%-30s %-8s %s\n", am->key,
  1949. alternative_status_string(am->item->status),
  1950. current ? current : "");
  1951. }
  1952. alternative_map_free(alt_map_obj);
  1953. }
  1954. static void
  1955. alternative_set_selection(struct alternative_map *all, const char *name,
  1956. const char *status, const char *choice)
  1957. {
  1958. struct alternative *a;
  1959. debug("set_selection(%s, %s, %s)", name, status, choice);
  1960. a = alternative_map_find(all, name);
  1961. if (a) {
  1962. const char *new_choice = NULL;
  1963. if (strcmp(status, "auto") == 0) {
  1964. new_choice = alternative_set_auto(a);
  1965. } else if (alternative_has_choice(a, choice)) {
  1966. new_choice = alternative_set_manual(a, choice);
  1967. } else {
  1968. pr(_("Alternative %s unchanged because choice "
  1969. "%s is not available."), name, choice);
  1970. }
  1971. if (new_choice) {
  1972. const char *current_choice;
  1973. current_choice = alternative_get_current(a);
  1974. alternative_select_mode(a, current_choice);
  1975. alternative_update(a, current_choice, new_choice);
  1976. }
  1977. } else {
  1978. pr(_("Skip unknown alternative %s."), name);
  1979. }
  1980. }
  1981. static void
  1982. alternative_set_selections(FILE *input, const char *desc)
  1983. {
  1984. struct alternative_map *alt_map_obj;
  1985. alt_map_obj = alternative_map_new(NULL, NULL);
  1986. alternative_map_load_names(alt_map_obj);
  1987. for (;;) {
  1988. char line[1024], *res, *name, *status, *choice;
  1989. size_t len, i;
  1990. errno = 0;
  1991. /* Can't use scanf("%s %s %s") because choice can
  1992. * contain a space */
  1993. res = fgets(line, sizeof(line), input);
  1994. if (res == NULL && errno) {
  1995. syserr(_("read error in %.250s"), desc);
  1996. } else if (res == NULL) {
  1997. break;
  1998. }
  1999. len = strlen(line);
  2000. if (len == 0 || line[len - 1] != '\n') {
  2001. error(_("line too long or not terminated while "
  2002. "trying to read %s"), desc);
  2003. }
  2004. line[len - 1] = '\0';
  2005. len--;
  2006. /* Delimit name string in line */
  2007. i = 0;
  2008. name = line;
  2009. while (i < len && !isblank(line[i]))
  2010. i++;
  2011. if (i >= len) {
  2012. printf("[%s %s] ", PROGNAME, "--set-selections");
  2013. pr(_("Skip invalid line: %s"), line);
  2014. continue;
  2015. }
  2016. line[i++] = '\0';
  2017. while (i < len && isblank(line[i]))
  2018. i++;
  2019. /* Delimit status string in line */
  2020. status = line + i;
  2021. while (i < len && !isblank(line[i]))
  2022. i++;
  2023. if (i >= len) {
  2024. printf("[%s %s] ", PROGNAME, "--set-selections");
  2025. pr(_("Skip invalid line: %s"), line);
  2026. continue;
  2027. }
  2028. line[i++] = '\0';
  2029. while (i < len && isblank(line[i]))
  2030. i++;
  2031. /* Delimit choice string in the line */
  2032. if (i >= len) {
  2033. printf("[%s %s] ", PROGNAME, "--set-selections");
  2034. pr(_("Skip invalid line: %s"), line);
  2035. continue;
  2036. }
  2037. choice = line + i;
  2038. printf("[%s %s] ", PROGNAME, "--set-selections");
  2039. alternative_set_selection(alt_map_obj, name, status, choice);
  2040. }
  2041. alternative_map_free(alt_map_obj);
  2042. }
  2043. static void
  2044. alternative_check_name(const char *name)
  2045. {
  2046. if (strpbrk(name, "/ \t"))
  2047. error(_("alternative name (%s) must not contain '/' "
  2048. "and spaces"), name);
  2049. }
  2050. static void
  2051. alternative_check_link(const char *linkname)
  2052. {
  2053. if (linkname[0] != '/')
  2054. error(_("alternative link is not absolute as it should be: %s"),
  2055. linkname);
  2056. }
  2057. static void
  2058. alternative_check_path(const char *file)
  2059. {
  2060. if (!file || file[0] != '/')
  2061. error(_("alternative path is not absolute as it should be: %s"),
  2062. file);
  2063. }
  2064. /**
  2065. * Check the alternative installation arguments.
  2066. *
  2067. * That the caller doesn't mix links between alternatives, doesn't mix
  2068. * alternatives between slave/master, and that the various parameters
  2069. * are fine.
  2070. */
  2071. static void
  2072. alternative_check_install_args(struct alternative *inst_alt,
  2073. struct fileset *fileset)
  2074. {
  2075. struct alternative_map *alt_map_links, *alt_map_parent;
  2076. struct alternative *found;
  2077. struct slave_link *sl;
  2078. struct stat st;
  2079. alternative_check_name(inst_alt->master_name);
  2080. alternative_check_link(inst_alt->master_link);
  2081. alternative_check_path(fileset->master_file);
  2082. /* Load information about all alternatives to check for mistakes. */
  2083. alt_map_links = alternative_map_new(NULL, NULL);
  2084. alt_map_parent = alternative_map_new(NULL, NULL);
  2085. alternative_map_load_tree(alt_map_links, alt_map_parent);
  2086. found = alternative_map_find(alt_map_parent, inst_alt->master_name);
  2087. if (found && strcmp(found->master_name, inst_alt->master_name) != 0) {
  2088. error(_("alternative %s can't be master: it is a slave of %s"),
  2089. inst_alt->master_name, found->master_name);
  2090. }
  2091. found = alternative_map_find(alt_map_links, inst_alt->master_link);
  2092. if (found && strcmp(found->master_name, inst_alt->master_name) != 0) {
  2093. found = alternative_map_find(alt_map_parent,
  2094. found->master_name);
  2095. error(_("alternative link %s is already managed by %s"),
  2096. inst_alt->master_link, found->master_name);
  2097. }
  2098. if (stat(fileset->master_file, &st) == -1) {
  2099. if (errno == ENOENT)
  2100. error(_("alternative path %s doesn't exist"),
  2101. fileset->master_file);
  2102. else
  2103. syserr(_("cannot stat file '%s'"), fileset->master_file);
  2104. }
  2105. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  2106. const char *file = fileset_get_slave(fileset, sl->name);
  2107. alternative_check_name(sl->name);
  2108. alternative_check_link(sl->link);
  2109. alternative_check_path(file);
  2110. found = alternative_map_find(alt_map_parent, sl->name);
  2111. if (found &&
  2112. strcmp(found->master_name, inst_alt->master_name) != 0) {
  2113. if (strcmp(found->master_name, sl->name) == 0)
  2114. error(_("alternative %s can't be slave of %s: "
  2115. "it is a master alternative"),
  2116. sl->name, inst_alt->master_name);
  2117. else
  2118. error(_("alternative %s can't be slave of %s: "
  2119. "it is a slave of %s"),
  2120. sl->name, inst_alt->master_name,
  2121. found->master_name);
  2122. }
  2123. found = alternative_map_find(alt_map_links, sl->link);
  2124. if (found &&
  2125. strcmp(found->master_name, inst_alt->master_name) != 0) {
  2126. error(_("alternative link %s is already "
  2127. "managed by %s"), sl->link,
  2128. found->master_name);
  2129. }
  2130. if (found) {
  2131. struct slave_link *sl2;
  2132. for (sl2 = found->slaves; sl2; sl2 = sl2->next)
  2133. if (strcmp(sl2->link, sl->link) == 0)
  2134. break;
  2135. if (sl2 && strcmp(sl2->name, sl->name) != 0)
  2136. error(_("alternative link %s is already "
  2137. "managed by %s (slave of %s)"),
  2138. sl->link, sl2->name,
  2139. found->master_name);
  2140. }
  2141. }
  2142. alternative_map_free(alt_map_links);
  2143. alternative_map_free(alt_map_parent);
  2144. }
  2145. /*
  2146. * Main program
  2147. */
  2148. #define MISSING_ARGS(nb) (argc < i + nb + 1)
  2149. int
  2150. main(int argc, char **argv)
  2151. {
  2152. /* Alternative worked on. */
  2153. struct alternative *a = NULL;
  2154. /* Alternative to install. */
  2155. struct alternative *inst_alt = NULL;
  2156. /* Set of files to install in the alternative. */
  2157. struct fileset *fileset = NULL;
  2158. /* Path of alternative we are offering. */
  2159. char *path = NULL;
  2160. const char *current_choice = NULL;
  2161. const char *new_choice = NULL;
  2162. int i = 0;
  2163. setlocale(LC_ALL, "");
  2164. bindtextdomain(PACKAGE, LOCALEDIR);
  2165. textdomain(PACKAGE);
  2166. admdir = admindir_init();
  2167. if (setvbuf(stdout, NULL, _IONBF, 0))
  2168. syserr("setvbuf failed");
  2169. prog_path = argv[0];
  2170. for (i = 1; i < argc; i++) {
  2171. if (strstr(argv[i], "--") != argv[i]) {
  2172. error(_("unknown argument '%s'"), argv[i]);
  2173. } else if (strcmp("--help", argv[i]) == 0) {
  2174. usage();
  2175. exit(0);
  2176. } else if (strcmp("--version", argv[i]) == 0) {
  2177. version();
  2178. exit(0);
  2179. } else if (strcmp("--verbose", argv[i]) == 0) {
  2180. opt_verbose++;
  2181. } else if (strcmp("--quiet", argv[i]) == 0) {
  2182. opt_verbose--;
  2183. } else if (strcmp("--install", argv[i]) == 0) {
  2184. char *prio_str, *prio_end;
  2185. long prio;
  2186. set_action("install");
  2187. if (MISSING_ARGS(4))
  2188. badusage(_("--install needs <link> <name> "
  2189. "<path> <priority>"));
  2190. prio_str = argv[i + 4];
  2191. if (strcmp(argv[i+1], argv[i+3]) == 0)
  2192. badusage(_("<link> and <path> can't be the same"));
  2193. errno = 0;
  2194. prio = strtol(prio_str, &prio_end, 10);
  2195. if (prio_str == prio_end || *prio_end != '\0')
  2196. badusage(_("priority must be an integer"));
  2197. if (prio < INT_MIN || prio > INT_MAX || errno == ERANGE)
  2198. badusage(_("priority is out of range"));
  2199. a = alternative_new(argv[i + 2]);
  2200. inst_alt = alternative_new(argv[i + 2]);
  2201. alternative_set_status(inst_alt, ALT_ST_AUTO);
  2202. alternative_set_link(inst_alt, xstrdup(argv[i + 1]));
  2203. fileset = fileset_new(argv[i + 3], prio);
  2204. i += 4;
  2205. } else if (strcmp("--remove", argv[i]) == 0 ||
  2206. strcmp("--set", argv[i]) == 0) {
  2207. set_action(argv[i] + 2);
  2208. if (MISSING_ARGS(2))
  2209. badusage(_("--%s needs <name> <path>"), argv[i] + 2);
  2210. a = alternative_new(argv[i + 1]);
  2211. path = xstrdup(argv[i + 2]);
  2212. alternative_check_name(a->master_name);
  2213. alternative_check_path(path);
  2214. i += 2;
  2215. } else if (strcmp("--display", argv[i]) == 0 ||
  2216. strcmp("--query", argv[i]) == 0 ||
  2217. strcmp("--auto", argv[i]) == 0 ||
  2218. strcmp("--config", argv[i]) == 0 ||
  2219. strcmp("--list", argv[i]) == 0 ||
  2220. strcmp("--remove-all", argv[i]) == 0) {
  2221. set_action(argv[i] + 2);
  2222. if (MISSING_ARGS(1))
  2223. badusage(_("--%s needs <name>"), argv[i] + 2);
  2224. a = alternative_new(argv[i + 1]);
  2225. alternative_check_name(a->master_name);
  2226. i++;
  2227. } else if (strcmp("--all", argv[i]) == 0 ||
  2228. strcmp("--get-selections", argv[i]) == 0 ||
  2229. strcmp("--set-selections", argv[i]) == 0) {
  2230. set_action(argv[i] + 2);
  2231. } else if (strcmp("--slave", argv[i]) == 0) {
  2232. char *slink, *sname, *spath;
  2233. struct slave_link *sl;
  2234. if (action == NULL ||
  2235. (action && strcmp(action, "install") != 0))
  2236. badusage(_("--slave only allowed with --install"));
  2237. if (MISSING_ARGS(3))
  2238. badusage(_("--slave needs <link> <name> <path>"));
  2239. slink = xstrdup(argv[i + 1]);
  2240. sname = xstrdup(argv[i + 2]);
  2241. spath = xstrdup(argv[i + 3]);
  2242. if (strcmp(slink, spath) == 0)
  2243. badusage(_("<link> and <path> can't be the same"));
  2244. if (strcmp(inst_alt->master_name, sname) == 0)
  2245. badusage(_("name %s is both primary and slave"),
  2246. sname);
  2247. if (strcmp(slink, inst_alt->master_link) == 0)
  2248. badusage(_("link %s is both primary and slave"),
  2249. slink);
  2250. if (alternative_has_slave(inst_alt, sname))
  2251. badusage(_("duplicate slave name %s"), sname);
  2252. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  2253. const char *linkname = sl->link;
  2254. if (linkname == NULL)
  2255. linkname = "";
  2256. if (strcmp(linkname, slink) == 0)
  2257. badusage(_("duplicate slave link %s"),
  2258. slink);
  2259. }
  2260. alternative_add_slave(inst_alt, sname, slink);
  2261. fileset_add_slave(fileset, xstrdup(sname), spath);
  2262. i+= 3;
  2263. } else if (strcmp("--log", argv[i]) == 0) {
  2264. if (MISSING_ARGS(1))
  2265. badusage(_("--%s needs a <file> argument"), "log");
  2266. log_file = argv[i + 1];
  2267. i++;
  2268. } else if (strcmp("--altdir", argv[i]) == 0) {
  2269. if (MISSING_ARGS(1))
  2270. badusage(_("--%s needs a <directory> argument"), "log");
  2271. altdir = argv[i + 1];
  2272. i++;
  2273. } else if (strcmp("--admindir", argv[i]) == 0) {
  2274. if (MISSING_ARGS(1))
  2275. badusage(_("--%s needs a <directory> argument"), "log");
  2276. admdir = argv[i + 1];
  2277. i++;
  2278. } else if (strcmp("--skip-auto", argv[i]) == 0) {
  2279. opt_skip_auto = 1;
  2280. } else if (strcmp("--force", argv[i]) == 0) {
  2281. opt_force = 1;
  2282. } else {
  2283. badusage(_("unknown option '%s'"), argv[i]);
  2284. }
  2285. }
  2286. if (!action)
  2287. badusage(_("need --display, --query, --list, --get-selections, "
  2288. "--config, --set, --set-selections, --install, "
  2289. "--remove, --all, --remove-all or --auto"));
  2290. if (strcmp(action, "install") == 0)
  2291. alternative_check_install_args(inst_alt, fileset);
  2292. if (strcmp(action, "display") == 0 ||
  2293. strcmp(action, "query") == 0 ||
  2294. strcmp(action, "list") == 0 ||
  2295. strcmp(action, "set") == 0 ||
  2296. strcmp(action, "auto") == 0 ||
  2297. strcmp(action, "config") == 0 ||
  2298. strcmp(action, "remove-all") == 0) {
  2299. /* Load the alternative info, stop on failure. */
  2300. if (!alternative_load(a, ALTDB_WARN_PARSER))
  2301. error(_("no alternatives for %s"), a->master_name);
  2302. } else if (strcmp(action, "remove") == 0) {
  2303. /* FIXME: Be consistent for now with the case when we
  2304. * try to remove a non-existing path from an existing
  2305. * link group file. */
  2306. if (!alternative_load(a, ALTDB_WARN_PARSER)) {
  2307. verbose(_("no alternatives for %s"), a->master_name);
  2308. exit(0);
  2309. }
  2310. } else if (strcmp(action, "install") == 0) {
  2311. /* Load the alternative info, ignore failures. */
  2312. alternative_load(a, ALTDB_WARN_PARSER);
  2313. }
  2314. /* Handle actions. */
  2315. if (strcmp(action, "all") == 0) {
  2316. log_msg("run with %s", get_argv_string(argc, argv));
  2317. alternative_config_all();
  2318. exit(0);
  2319. } else if (strcmp(action, "get-selections") == 0) {
  2320. alternative_get_selections();
  2321. exit(0);
  2322. } else if (strcmp(action, "set-selections") == 0) {
  2323. log_msg("run with %s", get_argv_string(argc, argv));
  2324. alternative_set_selections(stdin, _("<standard input>"));
  2325. exit(0);
  2326. } else if (strcmp(action, "display") == 0) {
  2327. alternative_display_user(a);
  2328. exit(0);
  2329. } else if (strcmp(action, "query") == 0) {
  2330. alternative_display_query(a);
  2331. exit(0);
  2332. } else if (strcmp(action, "list") == 0) {
  2333. alternative_display_list(a);
  2334. exit(0);
  2335. }
  2336. /* Actions below might modify the system. */
  2337. log_msg("run with %s", get_argv_string(argc, argv));
  2338. current_choice = alternative_get_current(a);
  2339. alternative_select_mode(a, current_choice);
  2340. if (strcmp(action, "set") == 0) {
  2341. new_choice = alternative_set_manual(a, path);
  2342. } else if (strcmp(action, "auto") == 0) {
  2343. new_choice = alternative_set_auto(a);
  2344. } else if (strcmp(action, "config") == 0) {
  2345. new_choice = alternative_config(a, current_choice);
  2346. } else if (strcmp(action, "remove") == 0) {
  2347. new_choice = alternative_remove(a, current_choice, path);
  2348. } else if (strcmp(action, "remove-all") == 0) {
  2349. alternative_choices_free(a);
  2350. } else if (strcmp(action, "install") == 0) {
  2351. if (a->master_link) {
  2352. /* Alternative already exists, check if anything got
  2353. * updated. */
  2354. alternative_evolve(a, inst_alt, current_choice, fileset);
  2355. } else {
  2356. /* Alternative doesn't exist, create from parameters. */
  2357. alternative_free(a);
  2358. a = inst_alt;
  2359. }
  2360. alternative_add_choice(a, fileset);
  2361. if (a->status == ALT_ST_AUTO) {
  2362. new_choice = alternative_get_best(a)->master_file;
  2363. } else {
  2364. verbose(_("automatic updates of %s/%s are disabled; "
  2365. "leaving it alone"), altdir, a->master_name);
  2366. verbose(_("to return to automatic updates use "
  2367. "'%s --auto %s'"), PROGNAME, a->master_name);
  2368. }
  2369. }
  2370. alternative_update(a, current_choice, new_choice);
  2371. return 0;
  2372. }