update-alternatives.c 66 KB

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