update-alternatives.c 66 KB

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