update-alternatives.c 59 KB

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