update-alternatives.c 60 KB

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