update-alternatives.c 59 KB

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