update-alternatives.c 61 KB

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