update-alternatives.c 59 KB

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