update-alternatives.c 64 KB

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