update-alternatives.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445
  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. if (!sl)
  1041. break; /* no other slave left */
  1042. }
  1043. }
  1044. /* Sort entries */
  1045. alternative_sort_slaves(a);
  1046. alternative_sort_choices(a);
  1047. /* Write admin file. */
  1048. ctx.filename = xstrdup(file);
  1049. ctx.fh = fopen(file, "w");
  1050. if (ctx.fh == NULL)
  1051. error(_("cannot write %s: %s"), file, strerror(errno));
  1052. altdb_print_line(&ctx, alternative_status_string(a->status));
  1053. altdb_print_line(&ctx, a->master_link);
  1054. for (sl = a->slaves; sl; sl = sl->next) {
  1055. altdb_print_line(&ctx, sl->name);
  1056. altdb_print_line(&ctx, sl->link);
  1057. }
  1058. altdb_print_line(&ctx, "");
  1059. for (fs = a->choices; fs; fs = fs->next) {
  1060. char *prio;
  1061. altdb_print_line(&ctx, fs->master_file);
  1062. xasprintf(&prio, "%d", fs->priority);
  1063. altdb_print_line(&ctx, prio);
  1064. free(prio);
  1065. for (sl = a->slaves; sl; sl = sl->next) {
  1066. if (fileset_has_slave(fs, sl->name))
  1067. altdb_print_line(&ctx,
  1068. fileset_get_slave(fs, sl->name));
  1069. else
  1070. altdb_print_line(&ctx, "");
  1071. }
  1072. }
  1073. altdb_print_line(&ctx, "");
  1074. /* Close database file */
  1075. if (fclose(ctx.fh))
  1076. error(_("unable to close %s: %s"), ctx.filename, strerror(errno));
  1077. }
  1078. static struct fileset *
  1079. alternative_get_best(struct alternative *a)
  1080. {
  1081. struct fileset *fs, *best;
  1082. for (best = fs = a->choices; fs; fs = fs->next)
  1083. if (fs->priority > best->priority)
  1084. best = fs;
  1085. return best;
  1086. }
  1087. static bool
  1088. alternative_has_current_link(struct alternative *a)
  1089. {
  1090. struct stat st;
  1091. char *curlink;
  1092. xasprintf(&curlink, "%s/%s", altdir, a->master_name);
  1093. if (lstat(curlink, &st)) {
  1094. if (errno == ENOENT) {
  1095. free(curlink);
  1096. return false;
  1097. }
  1098. error(_("cannot stat %s: %s"), curlink, strerror(errno));
  1099. } else {
  1100. free(curlink);
  1101. return true;
  1102. }
  1103. }
  1104. static char *
  1105. alternative_get_current(struct alternative *a)
  1106. {
  1107. char *curlink, *file;
  1108. if (!alternative_has_current_link(a))
  1109. return NULL;
  1110. xasprintf(&curlink, "%s/%s", altdir, a->master_name);
  1111. file = xreadlink(curlink, true);
  1112. free(curlink);
  1113. return file;
  1114. }
  1115. static void
  1116. alternative_display_query(struct alternative *a)
  1117. {
  1118. struct fileset *best, *fs;
  1119. struct slave_link *sl;
  1120. char *current;
  1121. pr("Link: %s", a->master_name);
  1122. pr("Status: %s", alternative_status_string(a->status));
  1123. best = alternative_get_best(a);
  1124. if (best)
  1125. pr("Best: %s", best->master_file);
  1126. current = alternative_get_current(a);
  1127. if (current) {
  1128. pr("Value: %s", current);
  1129. free(current);
  1130. } else {
  1131. pr("Value: none");
  1132. }
  1133. for (fs = a->choices; fs; fs = fs->next) {
  1134. printf("\n");
  1135. pr("Alternative: %s", fs->master_file);
  1136. pr("Priority: %d", fs->priority);
  1137. if (alternative_slaves_count(a) == 0)
  1138. continue;
  1139. pr("Slaves:");
  1140. for (sl = a->slaves; sl; sl = sl->next) {
  1141. if (fileset_has_slave(fs, sl->name))
  1142. pr(" %s %s", sl->name,
  1143. fileset_get_slave(fs, sl->name));
  1144. }
  1145. }
  1146. }
  1147. static void
  1148. alternative_display_user(struct alternative *a)
  1149. {
  1150. char *current;
  1151. struct fileset *fs;
  1152. struct slave_link *sl;
  1153. pr("%s - %s", a->master_name,
  1154. (a->status == ALT_ST_AUTO) ? _("auto mode") : _("manual mode"));
  1155. current = alternative_get_current(a);
  1156. if (current) {
  1157. pr(_(" link currently points to %s"), current);
  1158. free(current);
  1159. } else {
  1160. pr(_(" link currently absent"));
  1161. }
  1162. for (fs = a->choices; fs; fs = fs->next) {
  1163. pr(_("%s - priority %d"), fs->master_file, fs->priority);
  1164. for (sl = a->slaves; sl; sl = sl->next) {
  1165. if (fileset_has_slave(fs, sl->name))
  1166. pr(_(" slave %s: %s"), sl->name,
  1167. fileset_get_slave(fs, sl->name));
  1168. }
  1169. }
  1170. fs = alternative_get_best(a);
  1171. if (fs)
  1172. pr(_("Current 'best' version is '%s'."), fs->master_file);
  1173. else
  1174. pr(_("No versions available."));
  1175. }
  1176. static void
  1177. alternative_display_list(struct alternative *a)
  1178. {
  1179. struct fileset *fs;
  1180. for (fs = a->choices; fs; fs = fs->next)
  1181. pr(fs->master_file);
  1182. }
  1183. static const char *
  1184. alternative_select_choice(struct alternative *a)
  1185. {
  1186. char *current, *ret, selection[_POSIX_PATH_MAX];
  1187. struct fileset *best, *fs;
  1188. int len, idx;
  1189. current = alternative_get_current(a);
  1190. best = alternative_get_best(a);
  1191. assert(best);
  1192. for (;;) {
  1193. const char *mark;
  1194. pr(_("There are %d choices for the alternative %s (providing "
  1195. "%s)."), alternative_choices_count(a), a->master_name,
  1196. a->master_link);
  1197. printf("\n");
  1198. len = 15;
  1199. for (fs = a->choices; fs; fs = fs->next)
  1200. len = max(len, (int)strlen(fs->master_file) + 1);
  1201. pr(" %-12.12s %-*.*s %-10.10s %s", _("Selection"), len, len,
  1202. _("Path"), _("Priority"), _("Status"));
  1203. pr("------------------------------------------------------------");
  1204. if (a->status == ALT_ST_AUTO && current &&
  1205. strcmp(current, best->master_file) == 0)
  1206. mark = "*";
  1207. else
  1208. mark = " ";
  1209. pr("%s %-12d %-*s % -10d %s", mark, 0, len, best->master_file,
  1210. best->priority, _("auto mode"));
  1211. idx = 1;
  1212. for (fs = a->choices; fs; fs = fs->next) {
  1213. if (a->status == ALT_ST_MANUAL && current &&
  1214. strcmp(current, fs->master_file) == 0)
  1215. mark = "*";
  1216. else
  1217. mark = " ";
  1218. pr("%s %-12d %-*s % -10d %s", mark, idx, len,
  1219. fs->master_file, fs->priority, _("manual mode"));
  1220. idx++;
  1221. }
  1222. printf("\n");
  1223. printf(_("Press enter to keep the current choice[*], "
  1224. "or type selection number: "));
  1225. ret = fgets(selection, sizeof(selection), stdin);
  1226. if (ret == NULL || strlen(selection) == 0) {
  1227. free(current);
  1228. return NULL;
  1229. }
  1230. selection[strlen(selection) - 1] = '\0';
  1231. if (strlen(selection) == 0)
  1232. return current;
  1233. idx = strtol(selection, &ret, 10);
  1234. if (*ret == '\0') {
  1235. /* Look up by index */
  1236. if (idx == 0) {
  1237. alternative_set_status(a, ALT_ST_AUTO);
  1238. free(current);
  1239. return xstrdup(best->master_file);
  1240. }
  1241. idx--;
  1242. for (fs = a->choices; idx && fs; idx--)
  1243. fs = fs->next;
  1244. if (fs) {
  1245. alternative_set_status(a, ALT_ST_MANUAL);
  1246. free(current);
  1247. return xstrdup(fs->master_file);
  1248. }
  1249. } else {
  1250. /* Look up by name */
  1251. for (fs = a->choices; fs; fs = fs->next) {
  1252. if (strcmp(fs->master_file, selection) == 0) {
  1253. alternative_set_status(a, ALT_ST_MANUAL);
  1254. free(current);
  1255. return xstrdup(selection);
  1256. }
  1257. }
  1258. }
  1259. }
  1260. free(current);
  1261. return NULL;
  1262. }
  1263. static void
  1264. alternative_add_commit_op(struct alternative *a, enum opcode opcode,
  1265. const char *arg_a, const char *arg_b)
  1266. {
  1267. struct commit_operation *op, *cur;
  1268. op = xmalloc(sizeof(*op));
  1269. op->opcode = opcode;
  1270. op->arg_a = xstrdup(arg_a);
  1271. op->arg_b = xstrdup(arg_b);
  1272. op->next = NULL;
  1273. /* Add at the end */
  1274. cur = a->commit_ops;
  1275. while (cur && cur->next)
  1276. cur = cur->next;
  1277. if (cur)
  1278. cur->next = op;
  1279. else
  1280. a->commit_ops = op;
  1281. }
  1282. static void
  1283. alternative_commit(struct alternative *a)
  1284. {
  1285. struct commit_operation *op;
  1286. for (op = a->commit_ops; op; op = op->next) {
  1287. switch (op->opcode) {
  1288. case opcode_nop:
  1289. break;
  1290. case opcode_rm:
  1291. checked_rm(op->arg_a);
  1292. break;
  1293. case opcode_mv:
  1294. checked_mv(op->arg_a, op->arg_b);
  1295. break;
  1296. }
  1297. }
  1298. while (a->commit_ops) {
  1299. op = a->commit_ops;
  1300. a->commit_ops = op->next;
  1301. commit_operation_free(op);
  1302. }
  1303. }
  1304. static void
  1305. alternative_prepare_install_single(struct alternative *a, const char *name,
  1306. const char *linkname, const char *file)
  1307. {
  1308. char *fntmp, *fn;
  1309. struct stat st;
  1310. bool create_link;
  1311. /* Create link in /etc/alternatives. */
  1312. xasprintf(&fntmp, "%s/%s" DPKG_TMP_EXT, altdir, name);
  1313. xasprintf(&fn, "%s/%s", altdir, name);
  1314. checked_rm(fntmp);
  1315. checked_symlink(file, fntmp);
  1316. alternative_add_commit_op(a, opcode_mv, fntmp, fn);
  1317. free(fntmp);
  1318. errno = 0;
  1319. if (lstat(linkname, &st) == -1) {
  1320. if (errno != ENOENT)
  1321. error(_("cannot stat %s: %s"), linkname,
  1322. strerror(errno));
  1323. create_link = true;
  1324. } else {
  1325. create_link = S_ISLNK(st.st_mode);
  1326. }
  1327. if (create_link || opt_force) {
  1328. /* Create alternative link. */
  1329. xasprintf(&fntmp, "%s" DPKG_TMP_EXT, linkname);
  1330. checked_rm(fntmp);
  1331. checked_symlink(fn, fntmp);
  1332. alternative_add_commit_op(a, opcode_mv, fntmp, linkname);
  1333. free(fntmp);
  1334. } else {
  1335. warning(_("not replacing %s with a link."), linkname);
  1336. }
  1337. free(fn);
  1338. }
  1339. static void
  1340. alternative_prepare_install(struct alternative *a, const char *choice)
  1341. {
  1342. struct slave_link *sl;
  1343. struct fileset *fs;
  1344. fs = alternative_get_fileset(a, choice);
  1345. if (fs == NULL)
  1346. error(_("can't install unknown choice %s"), choice);
  1347. /* Take care of master alternative */
  1348. alternative_prepare_install_single(a, a->master_name, a->master_link,
  1349. choice);
  1350. /* Take care of slaves alternatives */
  1351. for (sl = a->slaves; sl; sl = sl->next) {
  1352. char *fn;
  1353. if (fileset_can_install_slave(fs, sl->name)) {
  1354. alternative_prepare_install_single(a, sl->name,
  1355. sl->link, fileset_get_slave(fs, sl->name));
  1356. continue;
  1357. }
  1358. /* Slave can't be installed */
  1359. if (fileset_has_slave(fs, sl->name))
  1360. warning(_("skip creation of %s because associated "
  1361. "file %s (of link group %s) doesn't exist."),
  1362. sl->link, fileset_get_slave(fs, sl->name),
  1363. a->master_name);
  1364. /* Drop unused slave. */
  1365. xasprintf(&fn, "%s/%s", altdir, sl->name);
  1366. alternative_add_commit_op(a, opcode_rm, sl->link, NULL);
  1367. alternative_add_commit_op(a, opcode_rm, fn, NULL);
  1368. free(fn);
  1369. }
  1370. }
  1371. static void
  1372. alternative_remove(struct alternative *a)
  1373. {
  1374. char *fn;
  1375. struct stat st;
  1376. struct slave_link *sl;
  1377. xasprintf(&fn, "%s" DPKG_TMP_EXT, a->master_link);
  1378. checked_rm(fn);
  1379. free(fn);
  1380. if (lstat(a->master_link, &st) == 0 && S_ISLNK(st.st_mode))
  1381. checked_rm(a->master_link);
  1382. xasprintf(&fn, "%s/%s" DPKG_TMP_EXT, altdir, a->master_name);
  1383. checked_rm(fn);
  1384. free(fn);
  1385. xasprintf(&fn, "%s/%s", altdir, a->master_name);
  1386. checked_rm(fn);
  1387. free(fn);
  1388. for (sl = a->slaves; sl; sl = sl->next) {
  1389. xasprintf(&fn, "%s" DPKG_TMP_EXT, sl->link);
  1390. checked_rm(fn);
  1391. free(fn);
  1392. if (lstat(sl->link, &st) == 0 && S_ISLNK(st.st_mode))
  1393. checked_rm(sl->link);
  1394. xasprintf(&fn, "%s/%s" DPKG_TMP_EXT, altdir, sl->name);
  1395. checked_rm(fn);
  1396. free(fn);
  1397. xasprintf(&fn, "%s/%s", altdir, sl->name);
  1398. checked_rm(fn);
  1399. free(fn);
  1400. }
  1401. /* Drop admin file */
  1402. xasprintf(&fn, "%s/%s", admdir, a->master_name);
  1403. checked_rm(fn);
  1404. free(fn);
  1405. }
  1406. static bool
  1407. alternative_is_broken(struct alternative *a)
  1408. {
  1409. char *altlnk, *wanted, *current;
  1410. struct fileset *fs;
  1411. struct slave_link *sl;
  1412. struct stat st;
  1413. if (!alternative_has_current_link(a))
  1414. return true;
  1415. /* Check master link */
  1416. altlnk = xreadlink(a->master_link, false);
  1417. if (!altlnk)
  1418. return true;
  1419. xasprintf(&wanted, "%s/%s", altdir, a->master_name);
  1420. if (strcmp(altlnk, wanted) != 0) {
  1421. free(wanted);
  1422. free(altlnk);
  1423. return true;
  1424. }
  1425. free(wanted);
  1426. free(altlnk);
  1427. /* Stop if we have an unmanaged alternative */
  1428. current = alternative_get_current(a);
  1429. if (!alternative_has_choice(a, current)) {
  1430. free(current);
  1431. return false;
  1432. }
  1433. fs = alternative_get_fileset(a, current);
  1434. free(current);
  1435. /* Check slaves */
  1436. for (sl = a->slaves; sl; sl = sl->next) {
  1437. if (fileset_can_install_slave(fs, sl->name)) {
  1438. char *sl_altlnk, *sl_current;
  1439. /* Verify link -> /etc/alternatives/foo */
  1440. sl_altlnk = xreadlink(sl->link, false);
  1441. if (!sl_altlnk)
  1442. return true;
  1443. xasprintf(&wanted, "%s/%s", altdir, sl->name);
  1444. if (strcmp(sl_altlnk, wanted) != 0) {
  1445. free(wanted);
  1446. free(sl_altlnk);
  1447. return true;
  1448. }
  1449. free(sl_altlnk);
  1450. /* Verify /etc/alternatives/foo -> file */
  1451. sl_current = xreadlink(wanted, false);
  1452. free(wanted);
  1453. if (!sl_current)
  1454. return true;
  1455. if (strcmp(sl_current, fileset_get_slave(fs, sl->name)) != 0) {
  1456. free(sl_current);
  1457. return true;
  1458. }
  1459. free(sl_current);
  1460. } else {
  1461. char *sl_altlnk;
  1462. /* Slave link must not exist. */
  1463. if (lstat(sl->link, &st) == 0)
  1464. return true;
  1465. xasprintf(&sl_altlnk, "%s/%s", altdir, sl->name);
  1466. if (lstat(sl_altlnk, &st) == 0) {
  1467. free(sl_altlnk);
  1468. return true;
  1469. }
  1470. free(sl_altlnk);
  1471. }
  1472. }
  1473. return false;
  1474. }
  1475. struct alternative_map {
  1476. struct alternative_map *next;
  1477. const char *key;
  1478. struct alternative *item;
  1479. };
  1480. static struct alternative_map *
  1481. alternative_map_new(const char *key, struct alternative *a)
  1482. {
  1483. struct alternative_map *am;
  1484. am = xmalloc(sizeof(*am));
  1485. am->next = NULL;
  1486. am->key = key;
  1487. am->item = a;
  1488. return am;
  1489. }
  1490. static struct alternative *
  1491. alternative_map_find(struct alternative_map *am, const char *key)
  1492. {
  1493. for (; am; am = am->next)
  1494. if (am->key && strcmp(am->key, key) == 0)
  1495. return am->item;
  1496. return NULL;
  1497. }
  1498. static void
  1499. alternative_map_add(struct alternative_map *am, const char *key, struct alternative *a)
  1500. {
  1501. if (am->key == NULL) {
  1502. am->key = key;
  1503. am->item = a;
  1504. } else {
  1505. struct alternative_map *new = alternative_map_new(key, a);
  1506. while(am->next)
  1507. am = am->next;
  1508. am->next = new;
  1509. }
  1510. }
  1511. static const char *
  1512. get_argv_string(int argc, char **argv)
  1513. {
  1514. static char string[2048];
  1515. size_t cur_len;
  1516. int i;
  1517. string[0] = '\0';
  1518. cur_len = 0;
  1519. for (i = 1; i < argc; i++) {
  1520. size_t arg_len = strlen(argv[i]);
  1521. if (cur_len + arg_len + 2 > sizeof(string))
  1522. break;
  1523. if (cur_len) {
  1524. strcpy(string + cur_len, " ");
  1525. cur_len++;
  1526. }
  1527. strcpy(string + cur_len, argv[i]);
  1528. cur_len += arg_len;
  1529. }
  1530. return string;
  1531. }
  1532. static void
  1533. alternative_set_selection(struct alternative_map *all, const char *name,
  1534. const char *status, const char *choice)
  1535. {
  1536. struct alternative *a;
  1537. debug("set_selection(%s, %s, %s)", name, status, choice);
  1538. if ((a = alternative_map_find(all, name))) {
  1539. char *cmd;
  1540. if (strcmp(status, "auto") == 0) {
  1541. xasprintf(&cmd, PROGNAME " --auto %s", name);
  1542. pr(_("Call %s."), cmd);
  1543. free(cmd);
  1544. subcall(prog_path, "--auto", name, NULL);
  1545. } else if (alternative_has_choice(a, choice)) {
  1546. xasprintf(&cmd, PROGNAME " --set %s %s",
  1547. name, choice);
  1548. pr(_("Call %s."), cmd);
  1549. free(cmd);
  1550. subcall(prog_path, "--set", name, choice, NULL);
  1551. } else {
  1552. pr(_("Alternative %s unchanged because choice "
  1553. "%s is not available."), name, choice);
  1554. }
  1555. } else {
  1556. pr(_("Skip unknown alternative %s."), name);
  1557. }
  1558. }
  1559. static void
  1560. alternative_set_selections(struct alternative_map *all, FILE* input, const char *desc)
  1561. {
  1562. const char *prefix = "[" PROGNAME "--set-selections] ";
  1563. for (;;) {
  1564. char line[1024], *res, *name, *status, *choice;
  1565. size_t len, i;
  1566. errno = 0;
  1567. /* Can't use scanf("%s %s %s") because choice can
  1568. * contain a space */
  1569. name = status = choice = NULL;
  1570. res = fgets(line, sizeof(line), input);
  1571. if (res == NULL && errno) {
  1572. error(_("while reading %s: %s"), desc, strerror(errno));
  1573. } else if (res == NULL) {
  1574. break;
  1575. }
  1576. len = strlen(line);
  1577. if (len == 0 || line[len - 1] != '\n') {
  1578. error(_("line too long or not terminated while "
  1579. "trying to read %s"), desc);
  1580. }
  1581. line[len - 1] = '\0';
  1582. len--;
  1583. /* Delimit name string in line */
  1584. i = 0;
  1585. name = line;
  1586. while (i < len && !isblank(line[i]))
  1587. i++;
  1588. if (i >= len) {
  1589. printf("%s", prefix);
  1590. pr(_("Skip invalid line: %s"), line);
  1591. continue;
  1592. }
  1593. line[i++] = '\0';
  1594. while (i < len && isblank(line[i]))
  1595. i++;
  1596. /* Delimit status string in line */
  1597. status = line + i;
  1598. while (i < len && !isblank(line[i]))
  1599. i++;
  1600. if (i >= len) {
  1601. printf("%s", prefix);
  1602. pr(_("Skip invalid line: %s"), line);
  1603. continue;
  1604. }
  1605. line[i++] = '\0';
  1606. while (i < len && isblank(line[i]))
  1607. i++;
  1608. /* Delimit choice string in the line */
  1609. if (i >= len) {
  1610. printf("%s", prefix);
  1611. pr(_("Skip invalid line: %s"), line);
  1612. continue;
  1613. }
  1614. choice = line + i;
  1615. printf("%s", prefix);
  1616. alternative_set_selection(all, name, status, choice);
  1617. }
  1618. }
  1619. static void
  1620. alternative_evolve(struct alternative *a, struct alternative *b,
  1621. const char *cur_choice, struct fileset *fs)
  1622. {
  1623. struct slave_link *sl;
  1624. struct stat st;
  1625. bool is_link = (lstat(a->master_link, &st) == 0 && S_ISLNK(st.st_mode));
  1626. if (is_link && strcmp(a->master_link, b->master_link) != 0) {
  1627. info(_("renaming %s link from %s to %s."), b->master_name,
  1628. a->master_link, b->master_link);
  1629. checked_mv(a->master_link, b->master_link);
  1630. }
  1631. alternative_set_link(a, xstrdup(b->master_link));
  1632. /* Check if new slaves have been added, or existing
  1633. * ones renamed. */
  1634. for (sl = b->slaves; sl; sl = sl->next) {
  1635. char *new_file = NULL;
  1636. const char *old, *new;
  1637. if (!alternative_has_slave(a, sl->name)) {
  1638. alternative_add_slave(a, xstrdup(sl->name),
  1639. xstrdup(sl->link));
  1640. continue;
  1641. }
  1642. old = alternative_get_slave(a, sl->name)->link;
  1643. new = sl->link;
  1644. if (cur_choice && strcmp(cur_choice, fs->master_file) == 0) {
  1645. new_file = xstrdup(fileset_get_slave(fs, sl->name));
  1646. } else {
  1647. char *lnk;
  1648. xasprintf(&lnk, "%s/%s", altdir, sl->name);
  1649. new_file = xreadlink(lnk, false);
  1650. free(lnk);
  1651. }
  1652. if (strcmp(old, new) != 0 && lstat(old, &st) == 0 &&
  1653. S_ISLNK(st.st_mode)) {
  1654. if (stat(new_file, &st) == 0) {
  1655. info(_("renaming %s slave link from %s to %s."),
  1656. sl->name, old, new);
  1657. checked_mv(old, new);
  1658. } else {
  1659. checked_rm(old);
  1660. }
  1661. }
  1662. free(new_file);
  1663. alternative_add_slave(a, xstrdup(sl->name), xstrdup(sl->link));
  1664. }
  1665. }
  1666. /*
  1667. * Main program
  1668. */
  1669. #define MISSING_ARGS(nb) (argc < i + nb + 1)
  1670. int
  1671. main(int argc, char **argv)
  1672. {
  1673. /* Alternative worked on. */
  1674. struct alternative *a = NULL;
  1675. /* Alternative to install. */
  1676. struct alternative *inst_alt = NULL;
  1677. /* Set of files to install in the alternative. */
  1678. struct fileset *fileset = NULL;
  1679. /* Path of alternative we are offering. */
  1680. char *path = NULL, *current_choice = NULL;
  1681. /* Alternatives maps for checks */
  1682. struct alternative_map *alt_map_obj, *alt_map_links, *alt_map_parent;
  1683. struct dirent **table;
  1684. const char *new_choice = NULL;
  1685. int i = 0, count;
  1686. setlocale(LC_ALL, "");
  1687. bindtextdomain("dpkg", LOCALEDIR);
  1688. textdomain("dpkg");
  1689. if (setvbuf(stdout, NULL, _IONBF, 0))
  1690. error("setvbuf failed: %s", strerror(errno));
  1691. prog_path = argv[0];
  1692. for (i = 1; i < argc; i++) {
  1693. if (strstr(argv[i], "--") != argv[i]) {
  1694. error(_("unknown argument `%s'"), argv[i]);
  1695. } else if (strcmp("--help", argv[i]) == 0) {
  1696. usage();
  1697. exit(0);
  1698. } else if (strcmp("--version", argv[i]) == 0) {
  1699. version();
  1700. exit(0);
  1701. } else if (strcmp("--verbose", argv[i]) == 0) {
  1702. opt_verbose++;
  1703. PUSH_OPT(argv[i]);
  1704. } else if (strcmp("--quiet", argv[i]) == 0) {
  1705. opt_verbose--;
  1706. PUSH_OPT(argv[i]);
  1707. } else if (strcmp("--install", argv[i]) == 0) {
  1708. long priority;
  1709. char *endptr;
  1710. set_action("install");
  1711. if (MISSING_ARGS(4))
  1712. badusage(_("--install needs <link> <name> "
  1713. "<path> <priority>"));
  1714. if (strcmp(argv[i+1], argv[i+3]) == 0)
  1715. badusage(_("<link> and <path> can't be the same"));
  1716. priority = strtol(argv[i+4], &endptr, 10);
  1717. if (*endptr != '\0')
  1718. badusage(_("priority must be an integer"));
  1719. a = alternative_new(argv[i + 2]);
  1720. inst_alt = alternative_new(argv[i + 2]);
  1721. alternative_set_status(inst_alt, ALT_ST_AUTO);
  1722. alternative_set_link(inst_alt, xstrdup(argv[i + 1]));
  1723. fileset = fileset_new(argv[i + 3], priority);
  1724. i += 4;
  1725. } else if (strcmp("--remove", argv[i]) == 0 ||
  1726. strcmp("--set", argv[i]) == 0) {
  1727. set_action(argv[i] + 2);
  1728. if (MISSING_ARGS(2))
  1729. badusage(_("--%s needs <name> <path>"), argv[i] + 2);
  1730. a = alternative_new(argv[i + 1]);
  1731. path = xstrdup(argv[i + 2]);
  1732. i += 2;
  1733. } else if (strcmp("--display", argv[i]) == 0 ||
  1734. strcmp("--query", argv[i]) == 0 ||
  1735. strcmp("--auto", argv[i]) == 0 ||
  1736. strcmp("--config", argv[i]) == 0 ||
  1737. strcmp("--list", argv[i]) == 0 ||
  1738. strcmp("--remove-all", argv[i]) == 0) {
  1739. set_action(argv[i] + 2);
  1740. if (MISSING_ARGS(1))
  1741. badusage(_("--%s needs <name>"), argv[i] + 2);
  1742. a = alternative_new(argv[i + 1]);
  1743. i++;
  1744. } else if (strcmp("--all", argv[i]) == 0 ||
  1745. strcmp("--get-selections", argv[i]) == 0 ||
  1746. strcmp("--set-selections", argv[i]) == 0) {
  1747. set_action(argv[i] + 2);
  1748. } else if (strcmp("--slave", argv[i]) == 0) {
  1749. char *slink, *sname, *spath;
  1750. struct slave_link *sl;
  1751. if (action && strcmp(action, "install") != 0)
  1752. badusage(_("--slave only allowed with --install"));
  1753. if (MISSING_ARGS(3))
  1754. badusage(_("--slave needs <link> <name> <path>"));
  1755. slink = xstrdup(argv[i + 1]);
  1756. sname = xstrdup(argv[i + 2]);
  1757. spath = xstrdup(argv[i + 3]);
  1758. if (strcmp(slink, spath) == 0)
  1759. badusage(_("<link> and <path> can't be the same"));
  1760. if (strcmp(inst_alt->master_name, sname) == 0)
  1761. badusage(_("name %s is both primary and slave"),
  1762. sname);
  1763. if (strcmp(slink, inst_alt->master_link) == 0)
  1764. badusage(_("link %s is both primary and slave"),
  1765. slink);
  1766. if (alternative_has_slave(inst_alt, sname))
  1767. badusage(_("slave name %s duplicated"), sname);
  1768. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  1769. const char *linkname = sl->link;
  1770. if (linkname == NULL)
  1771. linkname = "";
  1772. if (strcmp(linkname, slink) == 0)
  1773. badusage(_("slave link %s duplicated"),
  1774. slink);
  1775. }
  1776. alternative_add_slave(inst_alt, sname, slink);
  1777. fileset_add_slave(fileset, xstrdup(sname), spath);
  1778. i+= 3;
  1779. } else if (strcmp("--log", argv[i]) == 0) {
  1780. if (MISSING_ARGS(1))
  1781. badusage(_("--%s needs a <file> argument"), "log");
  1782. PUSH_OPT(argv[i]);
  1783. PUSH_OPT(argv[i + 1]);
  1784. log_file = argv[i + 1];
  1785. i++;
  1786. } else if (strcmp("--altdir", argv[i]) == 0) {
  1787. if (MISSING_ARGS(1))
  1788. badusage(_("--%s needs a <directory> argument"), "log");
  1789. PUSH_OPT(argv[i]);
  1790. PUSH_OPT(argv[i + 1]);
  1791. altdir = argv[i + 1];
  1792. i++;
  1793. } else if (strcmp("--admindir", argv[i]) == 0) {
  1794. if (MISSING_ARGS(1))
  1795. badusage(_("--%s needs a <directory> argument"), "log");
  1796. PUSH_OPT(argv[i]);
  1797. PUSH_OPT(argv[i + 1]);
  1798. admdir = argv[i + 1];
  1799. i++;
  1800. } else if (strcmp("--skip-auto", argv[i]) == 0) {
  1801. opt_skip_auto = 1;
  1802. PUSH_OPT(argv[i]);
  1803. } else if (strcmp("--force", argv[i]) == 0) {
  1804. opt_force = 1;
  1805. PUSH_OPT(argv[i]);
  1806. } else {
  1807. badusage(_("unknown option `%s'"), argv[i]);
  1808. }
  1809. }
  1810. if (!action)
  1811. badusage(_("need --display, --query, --list, --get-selections, "
  1812. "--config, --set, --set-selections, --install, "
  1813. "--remove, --all, --remove-all or --auto"));
  1814. /* Load infos about all alternatives to be able to check for mistakes. */
  1815. alt_map_obj = alternative_map_new(NULL, NULL);
  1816. alt_map_links = alternative_map_new(NULL, NULL);
  1817. alt_map_parent = alternative_map_new(NULL, NULL);
  1818. count = get_all_alternatives(&table);
  1819. for (i = 0; i < count; i++) {
  1820. struct slave_link *sl;
  1821. struct alternative *a_new = alternative_new(table[i]->d_name);
  1822. if (!alternative_load(a_new, true)) {
  1823. alternative_free(a_new);
  1824. free(table[i]);
  1825. continue;
  1826. }
  1827. alternative_map_add(alt_map_obj, a_new->master_name, a_new);
  1828. alternative_map_add(alt_map_links, a_new->master_link, a_new);
  1829. alternative_map_add(alt_map_parent, a_new->master_name, a_new);
  1830. for (sl = a_new->slaves; sl; sl = sl->next) {
  1831. alternative_map_add(alt_map_links, sl->link, a_new);
  1832. alternative_map_add(alt_map_parent, sl->name, a_new);
  1833. }
  1834. free(table[i]);
  1835. }
  1836. /* Check that caller don't mix links between alternatives and don't mix
  1837. * alternatives between slave/master, and that the various parameters
  1838. * are fine. */
  1839. if (strcmp(action, "install") == 0) {
  1840. struct alternative *found;
  1841. struct stat st;
  1842. struct slave_link *sl;
  1843. found = alternative_map_find(alt_map_parent,
  1844. inst_alt->master_name);
  1845. if (found && strcmp(found->master_name,
  1846. inst_alt->master_name) != 0) {
  1847. char *msg;
  1848. xasprintf(&msg, _("it is a slave of %s"),
  1849. found->master_name);
  1850. error(_("alternative %s can't be master: %s"),
  1851. inst_alt->master_name, msg);
  1852. }
  1853. found = alternative_map_find(alt_map_links,
  1854. inst_alt->master_link);
  1855. if (found && strcmp(found->master_name,
  1856. inst_alt->master_name) != 0) {
  1857. found = alternative_map_find(alt_map_parent,
  1858. found->master_name);
  1859. error(_("alternative link %s is already managed by %s."),
  1860. inst_alt->master_link, found->master_name);
  1861. }
  1862. if (inst_alt->master_link[0] != '/')
  1863. error(_("alternative link is not absolute as it "
  1864. "should be: %s"), inst_alt->master_link);
  1865. if (fileset->master_file[0] != '/')
  1866. error(_("alternative path is not absolute as it "
  1867. "should be: %s"), fileset->master_file);
  1868. if (stat(fileset->master_file, &st) == -1 && errno == ENOENT)
  1869. error(_("alternative path %s doesn't exist."),
  1870. fileset->master_file);
  1871. if (strpbrk(inst_alt->master_name, "/ \t"))
  1872. error(_("alternative name (%s) must not contain '/' "
  1873. "and spaces."), inst_alt->master_name);
  1874. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  1875. const char *file = fileset_get_slave(fileset, sl->name);
  1876. found = alternative_map_find(alt_map_parent, sl->name);
  1877. if (found && strcmp(found->master_name,
  1878. inst_alt->master_name) != 0) {
  1879. char *msg;
  1880. if (strcmp(found->master_name, sl->name) == 0)
  1881. xasprintf(&msg, "%s",
  1882. _("it is a master alternative."));
  1883. else
  1884. xasprintf(&msg, _("it is a slave of %s"),
  1885. found->master_name);
  1886. error(_("alternative %s can't be slave of "
  1887. "%s: %s"), sl->name,
  1888. inst_alt->master_name, msg);
  1889. }
  1890. found = alternative_map_find(alt_map_links, sl->link);
  1891. if (found && strcmp(found->master_name,
  1892. inst_alt->master_name) != 0) {
  1893. error(_("alternative link %s is already "
  1894. "managed by %s."), sl->link,
  1895. found->master_name);
  1896. }
  1897. if (sl->link[0] != '/')
  1898. error(_("alternative link is not absolute as "
  1899. "it should be: %s"), sl->link);
  1900. if (!file || file[0] != '/')
  1901. error(_("alternative path is not absolute as "
  1902. "it should be: %s"), file);
  1903. if (strpbrk(sl->name, "/ \t"))
  1904. error(_("alternative name (%s) must not contain '/' "
  1905. "and spaces."), sl->name);
  1906. }
  1907. }
  1908. /* Handle actions. */
  1909. if (strcmp(action, "all") == 0) {
  1910. config_all();
  1911. exit(0);
  1912. } else if (strcmp(action, "get-selections") == 0) {
  1913. struct alternative_map *am;
  1914. char *current;
  1915. for (am = alt_map_obj; am && am->item; am = am->next) {
  1916. current = alternative_get_current(am->item);
  1917. printf("%-30s %-8s %s\n", am->key,
  1918. alternative_status_string(am->item->status),
  1919. current ? current : "");
  1920. free(current);
  1921. }
  1922. exit(0);
  1923. } else if (strcmp(action, "set-selections") == 0) {
  1924. log_msg("run with %s", get_argv_string(argc, argv));
  1925. alternative_set_selections(alt_map_obj, stdin, _("<standard input>"));
  1926. exit(0);
  1927. }
  1928. /* Load the alternative info, stop on failure except for --install. */
  1929. if (!alternative_load(a, false) && strcmp(action, "install") != 0) {
  1930. /* FIXME: Be consistent for now with the case when we try to remove a
  1931. * non-existing path from an existing link group file. */
  1932. if (strcmp(action, "remove") == 0) {
  1933. verbose(_("no alternatives for %s."), a->master_name);
  1934. exit(0);
  1935. }
  1936. error(_("no alternatives for %s."), a->master_name);
  1937. }
  1938. if (strcmp(action, "display") == 0) {
  1939. alternative_display_user(a);
  1940. exit(0);
  1941. } else if (strcmp(action, "query") == 0) {
  1942. alternative_display_query(a);
  1943. exit(0);
  1944. } else if (strcmp(action, "list") == 0) {
  1945. alternative_display_list(a);
  1946. exit(0);
  1947. }
  1948. /* Actions below might modify the system. */
  1949. log_msg("run with %s", get_argv_string(argc, argv));
  1950. if (alternative_has_current_link(a)) {
  1951. current_choice = alternative_get_current(a);
  1952. /* Detect manually modified alternative, switch to manual. */
  1953. if (!alternative_has_choice(a, current_choice)) {
  1954. struct stat st;
  1955. char *altlink;
  1956. xasprintf(&altlink, "%s/%s", altdir, a->master_name);
  1957. if (stat(current_choice, &st) == -1 &&
  1958. errno == ENOENT) {
  1959. warning(_("%s is dangling, it will be updated "
  1960. "with best choice."), altlink);
  1961. alternative_set_status(a, ALT_ST_AUTO);
  1962. } else if (a->status != ALT_ST_MANUAL) {
  1963. warning(_("%s has been changed (manually or by "
  1964. "a script). Switching to manual "
  1965. "updates only."), altlink);
  1966. alternative_set_status(a, ALT_ST_MANUAL);
  1967. }
  1968. free(altlink);
  1969. }
  1970. } else {
  1971. /* Lack of alternative link => automatic mode. */
  1972. verbose(_("setting up automatic selection of %s."),
  1973. a->master_name);
  1974. alternative_set_status(a, ALT_ST_AUTO);
  1975. }
  1976. if (strcmp(action, "set") == 0) {
  1977. if (alternative_has_choice(a, path))
  1978. new_choice = path;
  1979. else
  1980. error(_("alternative %s for %s not registered, "
  1981. "not setting."), path, a->master_name);
  1982. alternative_set_status(a, ALT_ST_MANUAL);
  1983. } else if (strcmp(action, "auto") == 0) {
  1984. alternative_set_status(a, ALT_ST_AUTO);
  1985. new_choice = alternative_get_best(a)->master_file;
  1986. } else if (strcmp(action, "config") == 0) {
  1987. if (alternative_choices_count(a) == 0) {
  1988. pr(_("There is no program which provides %s."),
  1989. a->master_name);
  1990. pr(_("Nothing to configure."));
  1991. } else if (opt_skip_auto && a->status == ALT_ST_AUTO) {
  1992. alternative_display_user(a);
  1993. } else if (alternative_choices_count(a) == 1 &&
  1994. a->status == ALT_ST_AUTO &&
  1995. alternative_has_current_link(a)) {
  1996. char *cur = alternative_get_current(a);
  1997. pr(_("There is only one alternative in link group %s: %s"),
  1998. a->master_name, cur);
  1999. pr(_("Nothing to configure."));
  2000. free(cur);
  2001. } else {
  2002. new_choice = alternative_select_choice(a);
  2003. }
  2004. } else if (strcmp(action, "remove") == 0) {
  2005. if (alternative_has_choice(a, path))
  2006. alternative_remove_choice(a, path);
  2007. else
  2008. verbose(_("alternative %s for %s not registered, not "
  2009. "removing."), path, a->master_name);
  2010. if (current_choice && strcmp(current_choice, path) == 0) {
  2011. struct fileset *best;
  2012. /* Current choice is removed. */
  2013. if (a->status == ALT_ST_MANUAL) {
  2014. /* And it was manual, switch to auto. */
  2015. info(_("removing manually selected alternative "
  2016. "- switching %s to auto mode"),
  2017. a->master_name);
  2018. alternative_set_status(a, ALT_ST_AUTO);
  2019. }
  2020. best = alternative_get_best(a);
  2021. if (best)
  2022. new_choice = best->master_file;
  2023. }
  2024. } else if (strcmp(action, "remove-all") == 0) {
  2025. struct fileset *fs;
  2026. for (fs = a->choices; fs; fs = fs->next)
  2027. alternative_remove_choice(a, fs->master_file);
  2028. } else if (strcmp(action, "install") == 0) {
  2029. if (a->master_link) {
  2030. /* Alternative already exists, check if anything got
  2031. * updated. */
  2032. alternative_evolve(a, inst_alt, current_choice, fileset);
  2033. } else {
  2034. /* Alternative doesn't exist, create from parameters. */
  2035. alternative_free(a);
  2036. a = inst_alt;
  2037. }
  2038. alternative_add_choice(a, fileset);
  2039. if (a->status == ALT_ST_AUTO) {
  2040. new_choice = alternative_get_best(a)->master_file;
  2041. } else {
  2042. char *fn;
  2043. xasprintf(&fn, "%s/%s", altdir, a->master_name);
  2044. verbose(_("automatic updates of %s are disabled, "
  2045. "leaving it alone."), fn);
  2046. verbose(_("to return to automatic updates use "
  2047. "`update-alternatives --auto %s'."),
  2048. a->master_name);
  2049. free(fn);
  2050. }
  2051. }
  2052. /* No choice left, remove everything. */
  2053. if (!alternative_choices_count(a)) {
  2054. log_msg("link group %s fully removed", a->master_name);
  2055. alternative_remove(a);
  2056. exit(0);
  2057. }
  2058. /* New choice wanted. */
  2059. if (new_choice && (!current_choice ||
  2060. strcmp(new_choice, current_choice) != 0)) {
  2061. log_msg("link group %s updated to point to %s", a->master_name,
  2062. new_choice);
  2063. info(_("using %s to provide %s (%s) in %s."), new_choice,
  2064. a->master_link, a->master_name,
  2065. (a->status == ALT_ST_AUTO) ? _("auto mode") :
  2066. _("manual mode"));
  2067. debug("prepare_install(%s)", new_choice);
  2068. alternative_prepare_install(a, new_choice);
  2069. } else if (alternative_is_broken(a)) {
  2070. log_msg("auto-repair link group %s", a->master_name);
  2071. warning(_("forcing reinstallation of alternative %s because "
  2072. "link group %s is broken."), current_choice,
  2073. a->master_name);
  2074. if (current_choice && !alternative_has_choice(a, current_choice)) {
  2075. struct fileset *best = alternative_get_best(a);
  2076. new_choice = best->master_file;
  2077. warning(_("current alternative %s is unknown, "
  2078. "switching to %s for link group %s."),
  2079. current_choice, best->master_file,
  2080. a->master_name);
  2081. current_choice = best->master_file;
  2082. alternative_set_status(a, ALT_ST_AUTO);
  2083. }
  2084. if (current_choice)
  2085. alternative_prepare_install(a, current_choice);
  2086. }
  2087. /* Save administrative file if needed. */
  2088. if (a->modified) {
  2089. char *fntmp, *fn;
  2090. xasprintf(&fntmp, "%s/%s" DPKG_TMP_EXT, admdir, a->master_name);
  2091. xasprintf(&fn, "%s/%s", admdir, a->master_name);
  2092. debug("%s is modified and will be saved", a->master_name);
  2093. alternative_save(a, fntmp);
  2094. checked_mv(fntmp, fn);
  2095. free(fntmp);
  2096. free(fn);
  2097. }
  2098. /* Replace all symlinks in one pass. */
  2099. alternative_commit(a);
  2100. return 0;
  2101. }