update-alternatives.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523
  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. altdb_get_namelist(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 = altdb_get_namelist(&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 bool
  1366. alternative_can_replace_path(const char *linkname)
  1367. {
  1368. struct stat st;
  1369. bool replace_link;
  1370. if (opt_force)
  1371. return true;
  1372. errno = 0;
  1373. if (lstat(linkname, &st) == -1) {
  1374. if (errno != ENOENT)
  1375. syserr(_("cannot stat file '%s'"), linkname);
  1376. replace_link = true;
  1377. } else {
  1378. replace_link = S_ISLNK(st.st_mode);
  1379. }
  1380. return replace_link;
  1381. }
  1382. static void
  1383. alternative_prepare_install_single(struct alternative *a, const char *name,
  1384. const char *linkname, const char *file)
  1385. {
  1386. char *fntmp, *fn;
  1387. /* Create link in /etc/alternatives. */
  1388. xasprintf(&fntmp, "%s/%s" DPKG_TMP_EXT, altdir, name);
  1389. xasprintf(&fn, "%s/%s", altdir, name);
  1390. checked_rm(fntmp);
  1391. checked_symlink(file, fntmp);
  1392. alternative_add_commit_op(a, opcode_mv, fntmp, fn);
  1393. free(fntmp);
  1394. if (alternative_can_replace_path(linkname)) {
  1395. /* Create alternative link. */
  1396. xasprintf(&fntmp, "%s" DPKG_TMP_EXT, linkname);
  1397. checked_rm(fntmp);
  1398. checked_symlink(fn, fntmp);
  1399. alternative_add_commit_op(a, opcode_mv, fntmp, linkname);
  1400. free(fntmp);
  1401. } else {
  1402. warning(_("not replacing %s with a link."), linkname);
  1403. }
  1404. free(fn);
  1405. }
  1406. static void
  1407. alternative_prepare_install(struct alternative *a, const char *choice)
  1408. {
  1409. struct slave_link *sl;
  1410. struct fileset *fs;
  1411. fs = alternative_get_fileset(a, choice);
  1412. if (fs == NULL)
  1413. error(_("can't install unknown choice %s"), choice);
  1414. /* Take care of master alternative */
  1415. alternative_prepare_install_single(a, a->master_name, a->master_link,
  1416. choice);
  1417. /* Take care of slaves alternatives */
  1418. for (sl = a->slaves; sl; sl = sl->next) {
  1419. char *fn;
  1420. if (fileset_can_install_slave(fs, sl->name)) {
  1421. alternative_prepare_install_single(a, sl->name,
  1422. sl->link, fileset_get_slave(fs, sl->name));
  1423. continue;
  1424. }
  1425. /* Slave can't be installed */
  1426. if (fileset_has_slave(fs, sl->name))
  1427. warning(_("skip creation of %s because associated "
  1428. "file %s (of link group %s) doesn't exist."),
  1429. sl->link, fileset_get_slave(fs, sl->name),
  1430. a->master_name);
  1431. /* Drop unused slave. */
  1432. xasprintf(&fn, "%s/%s", altdir, sl->name);
  1433. if (alternative_can_replace_path(sl->link))
  1434. alternative_add_commit_op(a, opcode_rm, sl->link, NULL);
  1435. else
  1436. warning(_("not removing %s since it's not a symlink."),
  1437. sl->link);
  1438. alternative_add_commit_op(a, opcode_rm, fn, NULL);
  1439. free(fn);
  1440. }
  1441. }
  1442. static void
  1443. alternative_remove(struct alternative *a)
  1444. {
  1445. struct slave_link *sl;
  1446. checked_rm_args("%s" DPKG_TMP_EXT, a->master_link);
  1447. if (alternative_can_replace_path(a->master_link))
  1448. checked_rm(a->master_link);
  1449. checked_rm_args("%s/%s" DPKG_TMP_EXT, altdir, a->master_name);
  1450. checked_rm_args("%s/%s", altdir, a->master_name);
  1451. for (sl = a->slaves; sl; sl = sl->next) {
  1452. checked_rm_args("%s" DPKG_TMP_EXT, sl->link);
  1453. if (alternative_can_replace_path(sl->link))
  1454. checked_rm(sl->link);
  1455. checked_rm_args("%s/%s" DPKG_TMP_EXT, altdir, sl->name);
  1456. checked_rm_args("%s/%s", altdir, sl->name);
  1457. }
  1458. /* Drop admin file */
  1459. checked_rm_args("%s/%s", admdir, a->master_name);
  1460. }
  1461. static bool
  1462. alternative_is_broken(struct alternative *a)
  1463. {
  1464. char *altlnk, *wanted, *current;
  1465. struct fileset *fs;
  1466. struct slave_link *sl;
  1467. struct stat st;
  1468. if (!alternative_has_current_link(a))
  1469. return true;
  1470. /* Check master link */
  1471. altlnk = xreadlink(a->master_link, false);
  1472. if (!altlnk)
  1473. return true;
  1474. xasprintf(&wanted, "%s/%s", altdir, a->master_name);
  1475. if (strcmp(altlnk, wanted) != 0) {
  1476. free(wanted);
  1477. free(altlnk);
  1478. return true;
  1479. }
  1480. free(wanted);
  1481. free(altlnk);
  1482. /* Stop if we have an unmanaged alternative */
  1483. current = alternative_get_current(a);
  1484. if (!alternative_has_choice(a, current)) {
  1485. free(current);
  1486. return false;
  1487. }
  1488. fs = alternative_get_fileset(a, current);
  1489. free(current);
  1490. /* Check slaves */
  1491. for (sl = a->slaves; sl; sl = sl->next) {
  1492. if (fileset_can_install_slave(fs, sl->name)) {
  1493. char *sl_altlnk, *sl_current;
  1494. /* Verify link -> /etc/alternatives/foo */
  1495. sl_altlnk = xreadlink(sl->link, false);
  1496. if (!sl_altlnk)
  1497. return true;
  1498. xasprintf(&wanted, "%s/%s", altdir, sl->name);
  1499. if (strcmp(sl_altlnk, wanted) != 0) {
  1500. free(wanted);
  1501. free(sl_altlnk);
  1502. return true;
  1503. }
  1504. free(sl_altlnk);
  1505. /* Verify /etc/alternatives/foo -> file */
  1506. sl_current = xreadlink(wanted, false);
  1507. free(wanted);
  1508. if (!sl_current)
  1509. return true;
  1510. if (strcmp(sl_current, fileset_get_slave(fs, sl->name)) != 0) {
  1511. free(sl_current);
  1512. return true;
  1513. }
  1514. free(sl_current);
  1515. } else {
  1516. char *sl_altlnk;
  1517. /* Slave link must not exist. */
  1518. if (lstat(sl->link, &st) == 0)
  1519. return true;
  1520. xasprintf(&sl_altlnk, "%s/%s", altdir, sl->name);
  1521. if (lstat(sl_altlnk, &st) == 0) {
  1522. free(sl_altlnk);
  1523. return true;
  1524. }
  1525. free(sl_altlnk);
  1526. }
  1527. }
  1528. return false;
  1529. }
  1530. struct alternative_map {
  1531. struct alternative_map *next;
  1532. const char *key;
  1533. struct alternative *item;
  1534. };
  1535. static struct alternative_map *
  1536. alternative_map_new(const char *key, struct alternative *a)
  1537. {
  1538. struct alternative_map *am;
  1539. am = xmalloc(sizeof(*am));
  1540. am->next = NULL;
  1541. am->key = key;
  1542. am->item = a;
  1543. return am;
  1544. }
  1545. static struct alternative *
  1546. alternative_map_find(struct alternative_map *am, const char *key)
  1547. {
  1548. for (; am; am = am->next)
  1549. if (am->key && strcmp(am->key, key) == 0)
  1550. return am->item;
  1551. return NULL;
  1552. }
  1553. static void
  1554. alternative_map_add(struct alternative_map *am, const char *key, struct alternative *a)
  1555. {
  1556. if (am->key == NULL) {
  1557. am->key = key;
  1558. am->item = a;
  1559. } else {
  1560. struct alternative_map *new = alternative_map_new(key, a);
  1561. while(am->next)
  1562. am = am->next;
  1563. am->next = new;
  1564. }
  1565. }
  1566. static const char *
  1567. get_argv_string(int argc, char **argv)
  1568. {
  1569. static char string[2048];
  1570. size_t cur_len;
  1571. int i;
  1572. string[0] = '\0';
  1573. cur_len = 0;
  1574. for (i = 1; i < argc; i++) {
  1575. size_t arg_len = strlen(argv[i]);
  1576. if (cur_len + arg_len + 2 > sizeof(string))
  1577. break;
  1578. if (cur_len) {
  1579. strcpy(string + cur_len, " ");
  1580. cur_len++;
  1581. }
  1582. strcpy(string + cur_len, argv[i]);
  1583. cur_len += arg_len;
  1584. }
  1585. return string;
  1586. }
  1587. static void
  1588. alternative_set_selection(struct alternative_map *all, const char *name,
  1589. const char *status, const char *choice)
  1590. {
  1591. struct alternative *a;
  1592. debug("set_selection(%s, %s, %s)", name, status, choice);
  1593. if ((a = alternative_map_find(all, name))) {
  1594. char *cmd;
  1595. if (strcmp(status, "auto") == 0) {
  1596. xasprintf(&cmd, "%s --auto %s", PROGNAME, name);
  1597. pr(_("Call %s."), cmd);
  1598. free(cmd);
  1599. subcall(prog_path, "--auto", name, NULL);
  1600. } else if (alternative_has_choice(a, choice)) {
  1601. xasprintf(&cmd, "%s --set %s %s", PROGNAME,
  1602. name, choice);
  1603. pr(_("Call %s."), cmd);
  1604. free(cmd);
  1605. subcall(prog_path, "--set", name, choice, NULL);
  1606. } else {
  1607. pr(_("Alternative %s unchanged because choice "
  1608. "%s is not available."), name, choice);
  1609. }
  1610. } else {
  1611. pr(_("Skip unknown alternative %s."), name);
  1612. }
  1613. }
  1614. static void
  1615. alternative_set_selections(struct alternative_map *all, FILE* input, const char *desc)
  1616. {
  1617. for (;;) {
  1618. char line[1024], *res, *name, *status, *choice;
  1619. size_t len, i;
  1620. errno = 0;
  1621. /* Can't use scanf("%s %s %s") because choice can
  1622. * contain a space */
  1623. res = fgets(line, sizeof(line), input);
  1624. if (res == NULL && errno) {
  1625. syserr(_("read error in %.250s"), desc);
  1626. } else if (res == NULL) {
  1627. break;
  1628. }
  1629. len = strlen(line);
  1630. if (len == 0 || line[len - 1] != '\n') {
  1631. error(_("line too long or not terminated while "
  1632. "trying to read %s"), desc);
  1633. }
  1634. line[len - 1] = '\0';
  1635. len--;
  1636. /* Delimit name string in line */
  1637. i = 0;
  1638. name = line;
  1639. while (i < len && !isblank(line[i]))
  1640. i++;
  1641. if (i >= len) {
  1642. printf("[%s %s] ", PROGNAME, "--set-selections");
  1643. pr(_("Skip invalid line: %s"), line);
  1644. continue;
  1645. }
  1646. line[i++] = '\0';
  1647. while (i < len && isblank(line[i]))
  1648. i++;
  1649. /* Delimit status string in line */
  1650. status = line + i;
  1651. while (i < len && !isblank(line[i]))
  1652. i++;
  1653. if (i >= len) {
  1654. printf("[%s %s] ", PROGNAME, "--set-selections");
  1655. pr(_("Skip invalid line: %s"), line);
  1656. continue;
  1657. }
  1658. line[i++] = '\0';
  1659. while (i < len && isblank(line[i]))
  1660. i++;
  1661. /* Delimit choice string in the line */
  1662. if (i >= len) {
  1663. printf("[%s %s] ", PROGNAME, "--set-selections");
  1664. pr(_("Skip invalid line: %s"), line);
  1665. continue;
  1666. }
  1667. choice = line + i;
  1668. printf("[%s %s] ", PROGNAME, "--set-selections");
  1669. alternative_set_selection(all, name, status, choice);
  1670. }
  1671. }
  1672. static void
  1673. alternative_evolve(struct alternative *a, struct alternative *b,
  1674. const char *cur_choice, struct fileset *fs)
  1675. {
  1676. struct slave_link *sl;
  1677. struct stat st;
  1678. bool is_link = (lstat(a->master_link, &st) == 0 && S_ISLNK(st.st_mode));
  1679. if (is_link && strcmp(a->master_link, b->master_link) != 0) {
  1680. info(_("renaming %s link from %s to %s."), b->master_name,
  1681. a->master_link, b->master_link);
  1682. checked_mv(a->master_link, b->master_link);
  1683. }
  1684. alternative_set_link(a, xstrdup(b->master_link));
  1685. /* Check if new slaves have been added, or existing
  1686. * ones renamed. */
  1687. for (sl = b->slaves; sl; sl = sl->next) {
  1688. char *new_file = NULL;
  1689. const char *old, *new;
  1690. if (!alternative_has_slave(a, sl->name)) {
  1691. alternative_add_slave(a, xstrdup(sl->name),
  1692. xstrdup(sl->link));
  1693. continue;
  1694. }
  1695. old = alternative_get_slave(a, sl->name)->link;
  1696. new = sl->link;
  1697. if (cur_choice && strcmp(cur_choice, fs->master_file) == 0) {
  1698. new_file = xstrdup(fileset_get_slave(fs, sl->name));
  1699. } else {
  1700. char *lnk;
  1701. xasprintf(&lnk, "%s/%s", altdir, sl->name);
  1702. new_file = xreadlink(lnk, false);
  1703. free(lnk);
  1704. }
  1705. if (strcmp(old, new) != 0 && lstat(old, &st) == 0 &&
  1706. S_ISLNK(st.st_mode)) {
  1707. if (stat(new_file, &st) == 0) {
  1708. info(_("renaming %s slave link from %s to %s."),
  1709. sl->name, old, new);
  1710. checked_mv(old, new);
  1711. } else {
  1712. checked_rm(old);
  1713. }
  1714. }
  1715. free(new_file);
  1716. alternative_add_slave(a, xstrdup(sl->name), xstrdup(sl->link));
  1717. }
  1718. }
  1719. /*
  1720. * Main program
  1721. */
  1722. #define MISSING_ARGS(nb) (argc < i + nb + 1)
  1723. int
  1724. main(int argc, char **argv)
  1725. {
  1726. /* Alternative worked on. */
  1727. struct alternative *a = NULL;
  1728. /* Alternative to install. */
  1729. struct alternative *inst_alt = NULL;
  1730. /* Set of files to install in the alternative. */
  1731. struct fileset *fileset = NULL;
  1732. /* Path of alternative we are offering. */
  1733. char *path = NULL, *current_choice = NULL;
  1734. /* Alternatives maps for checks */
  1735. struct alternative_map *alt_map_obj, *alt_map_links, *alt_map_parent;
  1736. struct dirent **table;
  1737. const char *new_choice = NULL;
  1738. int i = 0, count;
  1739. setlocale(LC_ALL, "");
  1740. bindtextdomain("dpkg", LOCALEDIR);
  1741. textdomain("dpkg");
  1742. admdir = admindir_init();
  1743. if (setvbuf(stdout, NULL, _IONBF, 0))
  1744. syserr("setvbuf failed");
  1745. prog_path = argv[0];
  1746. for (i = 1; i < argc; i++) {
  1747. if (strstr(argv[i], "--") != argv[i]) {
  1748. error(_("unknown argument `%s'"), argv[i]);
  1749. } else if (strcmp("--help", argv[i]) == 0) {
  1750. usage();
  1751. exit(0);
  1752. } else if (strcmp("--version", argv[i]) == 0) {
  1753. version();
  1754. exit(0);
  1755. } else if (strcmp("--verbose", argv[i]) == 0) {
  1756. opt_verbose++;
  1757. PUSH_OPT(argv[i]);
  1758. } else if (strcmp("--quiet", argv[i]) == 0) {
  1759. opt_verbose--;
  1760. PUSH_OPT(argv[i]);
  1761. } else if (strcmp("--install", argv[i]) == 0) {
  1762. long priority;
  1763. char *endptr;
  1764. set_action("install");
  1765. if (MISSING_ARGS(4))
  1766. badusage(_("--install needs <link> <name> "
  1767. "<path> <priority>"));
  1768. if (strcmp(argv[i+1], argv[i+3]) == 0)
  1769. badusage(_("<link> and <path> can't be the same"));
  1770. priority = strtol(argv[i+4], &endptr, 10);
  1771. if (*endptr != '\0')
  1772. badusage(_("priority must be an integer"));
  1773. a = alternative_new(argv[i + 2]);
  1774. inst_alt = alternative_new(argv[i + 2]);
  1775. alternative_set_status(inst_alt, ALT_ST_AUTO);
  1776. alternative_set_link(inst_alt, xstrdup(argv[i + 1]));
  1777. fileset = fileset_new(argv[i + 3], priority);
  1778. i += 4;
  1779. } else if (strcmp("--remove", argv[i]) == 0 ||
  1780. strcmp("--set", argv[i]) == 0) {
  1781. set_action(argv[i] + 2);
  1782. if (MISSING_ARGS(2))
  1783. badusage(_("--%s needs <name> <path>"), argv[i] + 2);
  1784. a = alternative_new(argv[i + 1]);
  1785. path = xstrdup(argv[i + 2]);
  1786. i += 2;
  1787. } else if (strcmp("--display", argv[i]) == 0 ||
  1788. strcmp("--query", argv[i]) == 0 ||
  1789. strcmp("--auto", argv[i]) == 0 ||
  1790. strcmp("--config", argv[i]) == 0 ||
  1791. strcmp("--list", argv[i]) == 0 ||
  1792. strcmp("--remove-all", argv[i]) == 0) {
  1793. set_action(argv[i] + 2);
  1794. if (MISSING_ARGS(1))
  1795. badusage(_("--%s needs <name>"), argv[i] + 2);
  1796. a = alternative_new(argv[i + 1]);
  1797. i++;
  1798. } else if (strcmp("--all", argv[i]) == 0 ||
  1799. strcmp("--get-selections", argv[i]) == 0 ||
  1800. strcmp("--set-selections", argv[i]) == 0) {
  1801. set_action(argv[i] + 2);
  1802. } else if (strcmp("--slave", argv[i]) == 0) {
  1803. char *slink, *sname, *spath;
  1804. struct slave_link *sl;
  1805. if (action && strcmp(action, "install") != 0)
  1806. badusage(_("--slave only allowed with --install"));
  1807. if (MISSING_ARGS(3))
  1808. badusage(_("--slave needs <link> <name> <path>"));
  1809. slink = xstrdup(argv[i + 1]);
  1810. sname = xstrdup(argv[i + 2]);
  1811. spath = xstrdup(argv[i + 3]);
  1812. if (strcmp(slink, spath) == 0)
  1813. badusage(_("<link> and <path> can't be the same"));
  1814. if (strcmp(inst_alt->master_name, sname) == 0)
  1815. badusage(_("name %s is both primary and slave"),
  1816. sname);
  1817. if (strcmp(slink, inst_alt->master_link) == 0)
  1818. badusage(_("link %s is both primary and slave"),
  1819. slink);
  1820. if (alternative_has_slave(inst_alt, sname))
  1821. badusage(_("duplicate slave name %s"), sname);
  1822. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  1823. const char *linkname = sl->link;
  1824. if (linkname == NULL)
  1825. linkname = "";
  1826. if (strcmp(linkname, slink) == 0)
  1827. badusage(_("duplicate slave link %s"),
  1828. slink);
  1829. }
  1830. alternative_add_slave(inst_alt, sname, slink);
  1831. fileset_add_slave(fileset, xstrdup(sname), spath);
  1832. i+= 3;
  1833. } else if (strcmp("--log", argv[i]) == 0) {
  1834. if (MISSING_ARGS(1))
  1835. badusage(_("--%s needs a <file> argument"), "log");
  1836. PUSH_OPT(argv[i]);
  1837. PUSH_OPT(argv[i + 1]);
  1838. log_file = argv[i + 1];
  1839. i++;
  1840. } else if (strcmp("--altdir", argv[i]) == 0) {
  1841. if (MISSING_ARGS(1))
  1842. badusage(_("--%s needs a <directory> argument"), "log");
  1843. PUSH_OPT(argv[i]);
  1844. PUSH_OPT(argv[i + 1]);
  1845. altdir = argv[i + 1];
  1846. i++;
  1847. } else if (strcmp("--admindir", argv[i]) == 0) {
  1848. if (MISSING_ARGS(1))
  1849. badusage(_("--%s needs a <directory> argument"), "log");
  1850. PUSH_OPT(argv[i]);
  1851. PUSH_OPT(argv[i + 1]);
  1852. admdir = argv[i + 1];
  1853. i++;
  1854. } else if (strcmp("--skip-auto", argv[i]) == 0) {
  1855. opt_skip_auto = 1;
  1856. PUSH_OPT(argv[i]);
  1857. } else if (strcmp("--force", argv[i]) == 0) {
  1858. opt_force = 1;
  1859. PUSH_OPT(argv[i]);
  1860. } else {
  1861. badusage(_("unknown option `%s'"), argv[i]);
  1862. }
  1863. }
  1864. if (!action)
  1865. badusage(_("need --display, --query, --list, --get-selections, "
  1866. "--config, --set, --set-selections, --install, "
  1867. "--remove, --all, --remove-all or --auto"));
  1868. /* Load infos about all alternatives to be able to check for mistakes. */
  1869. alt_map_obj = alternative_map_new(NULL, NULL);
  1870. alt_map_links = alternative_map_new(NULL, NULL);
  1871. alt_map_parent = alternative_map_new(NULL, NULL);
  1872. count = altdb_get_namelist(&table);
  1873. for (i = 0; i < count; i++) {
  1874. struct slave_link *sl;
  1875. struct alternative *a_new = alternative_new(table[i]->d_name);
  1876. if (!alternative_load(a_new, true)) {
  1877. alternative_free(a_new);
  1878. free(table[i]);
  1879. continue;
  1880. }
  1881. alternative_map_add(alt_map_obj, a_new->master_name, a_new);
  1882. alternative_map_add(alt_map_links, a_new->master_link, a_new);
  1883. alternative_map_add(alt_map_parent, a_new->master_name, a_new);
  1884. for (sl = a_new->slaves; sl; sl = sl->next) {
  1885. alternative_map_add(alt_map_links, sl->link, a_new);
  1886. alternative_map_add(alt_map_parent, sl->name, a_new);
  1887. }
  1888. free(table[i]);
  1889. }
  1890. free(table);
  1891. /* Check that caller don't mix links between alternatives and don't mix
  1892. * alternatives between slave/master, and that the various parameters
  1893. * are fine. */
  1894. if (strcmp(action, "install") == 0) {
  1895. struct alternative *found;
  1896. struct stat st;
  1897. struct slave_link *sl;
  1898. found = alternative_map_find(alt_map_parent,
  1899. inst_alt->master_name);
  1900. if (found && strcmp(found->master_name,
  1901. inst_alt->master_name) != 0) {
  1902. error(_("alternative %s can't be master: "
  1903. "it is a slave of %s"),
  1904. inst_alt->master_name, found->master_name);
  1905. }
  1906. found = alternative_map_find(alt_map_links,
  1907. inst_alt->master_link);
  1908. if (found && strcmp(found->master_name,
  1909. inst_alt->master_name) != 0) {
  1910. found = alternative_map_find(alt_map_parent,
  1911. found->master_name);
  1912. error(_("alternative link %s is already managed by %s."),
  1913. inst_alt->master_link, found->master_name);
  1914. }
  1915. if (inst_alt->master_link[0] != '/')
  1916. error(_("alternative link is not absolute as it "
  1917. "should be: %s"), inst_alt->master_link);
  1918. if (fileset->master_file[0] != '/')
  1919. error(_("alternative path is not absolute as it "
  1920. "should be: %s"), fileset->master_file);
  1921. if (stat(fileset->master_file, &st) == -1 && errno == ENOENT)
  1922. error(_("alternative path %s doesn't exist."),
  1923. fileset->master_file);
  1924. if (strpbrk(inst_alt->master_name, "/ \t"))
  1925. error(_("alternative name (%s) must not contain '/' "
  1926. "and spaces."), inst_alt->master_name);
  1927. for (sl = inst_alt->slaves; sl; sl = sl->next) {
  1928. const char *file = fileset_get_slave(fileset, sl->name);
  1929. found = alternative_map_find(alt_map_parent, sl->name);
  1930. if (found && strcmp(found->master_name,
  1931. inst_alt->master_name) != 0) {
  1932. char *msg;
  1933. if (strcmp(found->master_name, sl->name) == 0)
  1934. msg = _("it is a master alternative.");
  1935. else
  1936. xasprintf(&msg, _("it is a slave of %s"),
  1937. found->master_name);
  1938. error(_("alternative %s can't be slave of "
  1939. "%s: %s"), sl->name,
  1940. inst_alt->master_name, msg);
  1941. }
  1942. found = alternative_map_find(alt_map_links, sl->link);
  1943. if (found && strcmp(found->master_name,
  1944. inst_alt->master_name) != 0) {
  1945. error(_("alternative link %s is already "
  1946. "managed by %s."), sl->link,
  1947. found->master_name);
  1948. }
  1949. if (found) {
  1950. struct slave_link *sl2;
  1951. for (sl2 = found->slaves; sl2; sl2 = sl2->next)
  1952. if (strcmp(sl2->link, sl->link) == 0)
  1953. break;
  1954. if (sl2 && strcmp(sl2->name, sl->name) != 0)
  1955. error(_("alternative link %s is already "
  1956. "managed by %s (slave of %s)."),
  1957. sl->link, sl2->name,
  1958. found->master_name);
  1959. }
  1960. if (sl->link[0] != '/')
  1961. error(_("alternative link is not absolute as "
  1962. "it should be: %s"), sl->link);
  1963. if (!file || file[0] != '/')
  1964. error(_("alternative path is not absolute as "
  1965. "it should be: %s"), file);
  1966. if (strpbrk(sl->name, "/ \t"))
  1967. error(_("alternative name (%s) must not contain '/' "
  1968. "and spaces."), sl->name);
  1969. }
  1970. }
  1971. /* Handle actions. */
  1972. if (strcmp(action, "all") == 0) {
  1973. config_all();
  1974. exit(0);
  1975. } else if (strcmp(action, "get-selections") == 0) {
  1976. struct alternative_map *am;
  1977. for (am = alt_map_obj; am && am->item; am = am->next) {
  1978. char *current;
  1979. current = alternative_get_current(am->item);
  1980. printf("%-30s %-8s %s\n", am->key,
  1981. alternative_status_string(am->item->status),
  1982. current ? current : "");
  1983. free(current);
  1984. }
  1985. exit(0);
  1986. } else if (strcmp(action, "set-selections") == 0) {
  1987. log_msg("run with %s", get_argv_string(argc, argv));
  1988. alternative_set_selections(alt_map_obj, stdin, _("<standard input>"));
  1989. exit(0);
  1990. }
  1991. /* Load the alternative info, stop on failure except for --install. */
  1992. if (!alternative_load(a, false) && strcmp(action, "install") != 0) {
  1993. /* FIXME: Be consistent for now with the case when we try to remove a
  1994. * non-existing path from an existing link group file. */
  1995. if (strcmp(action, "remove") == 0) {
  1996. verbose(_("no alternatives for %s."), a->master_name);
  1997. exit(0);
  1998. }
  1999. error(_("no alternatives for %s."), a->master_name);
  2000. }
  2001. if (strcmp(action, "display") == 0) {
  2002. alternative_display_user(a);
  2003. exit(0);
  2004. } else if (strcmp(action, "query") == 0) {
  2005. alternative_display_query(a);
  2006. exit(0);
  2007. } else if (strcmp(action, "list") == 0) {
  2008. alternative_display_list(a);
  2009. exit(0);
  2010. }
  2011. /* Actions below might modify the system. */
  2012. log_msg("run with %s", get_argv_string(argc, argv));
  2013. if (alternative_has_current_link(a)) {
  2014. current_choice = alternative_get_current(a);
  2015. /* Detect manually modified alternative, switch to manual. */
  2016. if (!alternative_has_choice(a, current_choice)) {
  2017. struct stat st;
  2018. if (stat(current_choice, &st) == -1 &&
  2019. errno == ENOENT) {
  2020. warning(_("%s/%s is dangling, it will be updated "
  2021. "with best choice."), altdir, a->master_name);
  2022. alternative_set_status(a, ALT_ST_AUTO);
  2023. } else if (a->status != ALT_ST_MANUAL) {
  2024. warning(_("%s/%s has been changed (manually or by "
  2025. "a script). Switching to manual "
  2026. "updates only."), altdir, a->master_name);
  2027. alternative_set_status(a, ALT_ST_MANUAL);
  2028. }
  2029. }
  2030. } else {
  2031. /* Lack of alternative link => automatic mode. */
  2032. verbose(_("setting up automatic selection of %s."),
  2033. a->master_name);
  2034. alternative_set_status(a, ALT_ST_AUTO);
  2035. }
  2036. if (strcmp(action, "set") == 0) {
  2037. if (alternative_has_choice(a, path))
  2038. new_choice = path;
  2039. else
  2040. error(_("alternative %s for %s not registered, "
  2041. "not setting."), path, a->master_name);
  2042. alternative_set_status(a, ALT_ST_MANUAL);
  2043. } else if (strcmp(action, "auto") == 0) {
  2044. alternative_set_status(a, ALT_ST_AUTO);
  2045. if (alternative_choices_count(a) == 0)
  2046. pr(_("There is no program which provides %s."),
  2047. a->master_name);
  2048. else
  2049. new_choice = alternative_get_best(a)->master_file;
  2050. } else if (strcmp(action, "config") == 0) {
  2051. if (alternative_choices_count(a) == 0) {
  2052. pr(_("There is no program which provides %s."),
  2053. a->master_name);
  2054. pr(_("Nothing to configure."));
  2055. } else if (opt_skip_auto && a->status == ALT_ST_AUTO) {
  2056. alternative_display_user(a);
  2057. } else if (alternative_choices_count(a) == 1 &&
  2058. a->status == ALT_ST_AUTO &&
  2059. alternative_has_current_link(a)) {
  2060. char *cur = alternative_get_current(a);
  2061. pr(_("There is only one alternative in link group %s: %s"),
  2062. a->master_name, cur);
  2063. pr(_("Nothing to configure."));
  2064. free(cur);
  2065. } else {
  2066. new_choice = alternative_select_choice(a);
  2067. }
  2068. } else if (strcmp(action, "remove") == 0) {
  2069. if (alternative_has_choice(a, path))
  2070. alternative_remove_choice(a, path);
  2071. else
  2072. verbose(_("alternative %s for %s not registered, not "
  2073. "removing."), path, a->master_name);
  2074. if (current_choice && strcmp(current_choice, path) == 0) {
  2075. struct fileset *best;
  2076. /* Current choice is removed. */
  2077. if (a->status == ALT_ST_MANUAL) {
  2078. /* And it was manual, switch to auto. */
  2079. info(_("removing manually selected alternative "
  2080. "- switching %s to auto mode"),
  2081. a->master_name);
  2082. alternative_set_status(a, ALT_ST_AUTO);
  2083. }
  2084. best = alternative_get_best(a);
  2085. if (best)
  2086. new_choice = best->master_file;
  2087. }
  2088. } else if (strcmp(action, "remove-all") == 0) {
  2089. alternative_choices_free(a);
  2090. } else if (strcmp(action, "install") == 0) {
  2091. if (a->master_link) {
  2092. /* Alternative already exists, check if anything got
  2093. * updated. */
  2094. alternative_evolve(a, inst_alt, current_choice, fileset);
  2095. } else {
  2096. /* Alternative doesn't exist, create from parameters. */
  2097. alternative_free(a);
  2098. a = inst_alt;
  2099. }
  2100. alternative_add_choice(a, fileset);
  2101. if (a->status == ALT_ST_AUTO) {
  2102. new_choice = alternative_get_best(a)->master_file;
  2103. } else {
  2104. verbose(_("automatic updates of %s/%s are disabled, "
  2105. "leaving it alone."), altdir, a->master_name);
  2106. verbose(_("to return to automatic updates use "
  2107. "'%s --auto %s'."), PROGNAME, a->master_name);
  2108. }
  2109. }
  2110. /* No choice left, remove everything. */
  2111. if (!alternative_choices_count(a)) {
  2112. log_msg("link group %s fully removed", a->master_name);
  2113. alternative_remove(a);
  2114. exit(0);
  2115. }
  2116. /* New choice wanted. */
  2117. if (new_choice && (!current_choice ||
  2118. strcmp(new_choice, current_choice) != 0)) {
  2119. log_msg("link group %s updated to point to %s", a->master_name,
  2120. new_choice);
  2121. info(_("using %s to provide %s (%s) in %s."), new_choice,
  2122. a->master_link, a->master_name,
  2123. (a->status == ALT_ST_AUTO) ? _("auto mode") :
  2124. _("manual mode"));
  2125. debug("prepare_install(%s)", new_choice);
  2126. alternative_prepare_install(a, new_choice);
  2127. } else if (alternative_is_broken(a)) {
  2128. log_msg("auto-repair link group %s", a->master_name);
  2129. warning(_("forcing reinstallation of alternative %s because "
  2130. "link group %s is broken."), current_choice,
  2131. a->master_name);
  2132. if (current_choice && !alternative_has_choice(a, current_choice)) {
  2133. struct fileset *best = alternative_get_best(a);
  2134. warning(_("current alternative %s is unknown, "
  2135. "switching to %s for link group %s."),
  2136. current_choice, best->master_file,
  2137. a->master_name);
  2138. current_choice = best->master_file;
  2139. alternative_set_status(a, ALT_ST_AUTO);
  2140. }
  2141. if (current_choice)
  2142. alternative_prepare_install(a, current_choice);
  2143. }
  2144. /* Save administrative file if needed. */
  2145. if (a->modified) {
  2146. debug("%s is modified and will be saved", a->master_name);
  2147. alternative_save(a);
  2148. }
  2149. /* Replace all symlinks in one pass. */
  2150. alternative_commit(a);
  2151. return 0;
  2152. }