update-alternatives.c 64 KB

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