update-alternatives.c 66 KB

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