update-alternatives.c 64 KB

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