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. 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. pid_t pid, r;
  333. int status;
  334. pid = fork();
  335. if (pid == -1)
  336. error(_("fork failed"));
  337. if (pid == 0) {
  338. execvp(prog, (char *const *)args);
  339. syserr(_("unable to execute %s (%s)"), prog, prog);
  340. }
  341. while ((r = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
  342. if (r != pid)
  343. error(_("wait for subprocess %s failed"), prog);
  344. return status;
  345. }
  346. static void DPKG_ATTR_SENTINEL
  347. subcall(const char *prog, ...)
  348. {
  349. va_list args;
  350. const char **cmd;
  351. int res, i, j, count;
  352. /* Count the arguments */
  353. va_start(args, prog);
  354. count = 0;
  355. while (va_arg(args, char *))
  356. count++;
  357. va_end(args);
  358. /* Prepare table for all parameters */
  359. cmd = xmalloc(sizeof(*cmd) * (nb_opts + count + 2));
  360. i = 0;
  361. cmd[i++] = prog;
  362. for (j = 0; j < nb_opts; j++)
  363. cmd[i++] = pass_opts[j];
  364. va_start(args, prog);
  365. for (j = 0; j < count; j++)
  366. cmd[i++] = va_arg(args, char *);
  367. va_end(args);
  368. cmd[i++] = NULL;
  369. /* Run the command */
  370. res = spawn(prog, cmd);
  371. free(cmd);
  372. if (WIFEXITED(res) && WEXITSTATUS(res) == 0)
  373. return;
  374. if (WIFEXITED(res))
  375. exit(WEXITSTATUS(res));
  376. exit(128);
  377. }
  378. static bool
  379. rename_mv(const char *src, const char *dst)
  380. {
  381. struct stat st;
  382. if (lstat(src, &st) != 0)
  383. return false;
  384. if (rename(src, dst) != 0) {
  385. const char *args[] = { "mv", src, dst, NULL };
  386. int r;
  387. r = spawn("mv", args);
  388. if (WIFEXITED(r) && WEXITSTATUS(r) == 0)
  389. return true;
  390. return false;
  391. }
  392. return true;
  393. }
  394. static void
  395. checked_symlink(const char *filename, const char *linkname)
  396. {
  397. if (symlink(filename, linkname))
  398. syserr(_("error creating symbolic link `%.255s'"), linkname);
  399. }
  400. static void
  401. checked_mv(const char *src, const char *dst)
  402. {
  403. if (!rename_mv(src, dst))
  404. syserr(_("unable to install `%.250s' as `%.250s'"), src, dst);
  405. }
  406. static void
  407. checked_rm(const char *f)
  408. {
  409. if (!unlink(f))
  410. return;
  411. if (errno != ENOENT)
  412. syserr(_("unable to remove '%s'"), f);
  413. }
  414. static void DPKG_ATTR_PRINTF(1)
  415. checked_rm_args(const char *fmt, ...)
  416. {
  417. va_list args;
  418. char *path;
  419. va_start(args, fmt);
  420. xvasprintf(&path, fmt, args);
  421. va_end(args);
  422. checked_rm(path);
  423. free(path);
  424. }
  425. /*
  426. * OBJECTS
  427. */
  428. struct fileset {
  429. struct fileset *next;
  430. char *master_file;
  431. int priority;
  432. struct slave_file {
  433. struct slave_file *next;
  434. char *name;
  435. char *file;
  436. } *slaves;
  437. };
  438. static struct fileset *
  439. fileset_new(const char *master_file, int prio)
  440. {
  441. struct fileset *fs;
  442. fs = xmalloc(sizeof(*fs));
  443. fs->next = NULL;
  444. fs->master_file = xstrdup(master_file);
  445. fs->priority = prio;
  446. fs->slaves = NULL;
  447. return fs;
  448. }
  449. static void
  450. fileset_free(struct fileset *fs)
  451. {
  452. struct slave_file *slave, *next;
  453. free(fs->master_file);
  454. for (slave = fs->slaves; slave; slave = next) {
  455. next = slave->next;
  456. free(slave->name);
  457. free(slave->file);
  458. free(slave);
  459. }
  460. free(fs);
  461. }
  462. /* name and file must be allocated with malloc */
  463. static void
  464. fileset_add_slave(struct fileset *fs, char *name, char *file)
  465. {
  466. struct slave_file *sl, *cur, *prev = NULL;
  467. /* Replace existing first */
  468. for (cur = fs->slaves; cur; cur = cur->next) {
  469. if (strcmp(cur->name, name) == 0) {
  470. free(cur->file);
  471. cur->file = xstrdup(file);
  472. return;
  473. }
  474. prev = cur;
  475. }
  476. /* Otherwise add new at the end */
  477. sl = xmalloc(sizeof(*sl));
  478. sl->next = NULL;
  479. sl->name = name;
  480. sl->file = file;
  481. if (prev)
  482. prev->next = sl;
  483. else
  484. fs->slaves = sl;
  485. }
  486. static const char *
  487. fileset_get_slave(struct fileset *fs, const char *name)
  488. {
  489. struct slave_file *slave;
  490. for (slave = fs->slaves; slave; slave = slave->next) {
  491. if (strcmp(slave->name, name) == 0)
  492. return slave->file;
  493. }
  494. return NULL;
  495. }
  496. static bool
  497. fileset_has_slave(struct fileset *fs, const char *name)
  498. {
  499. const char *file = fileset_get_slave(fs, name);
  500. if (file == NULL)
  501. return false;
  502. return file[0] != '\0';
  503. }
  504. static bool
  505. fileset_can_install_slave(struct fileset *fs, const char *slave_name)
  506. {
  507. struct stat st;
  508. /* Decide whether the slave alternative must be setup */
  509. if (fileset_has_slave(fs, slave_name)) {
  510. const char *slave = fileset_get_slave(fs, slave_name);
  511. errno = 0;
  512. if (stat(slave, &st) == -1 && errno != ENOENT)
  513. syserr(_("cannot stat file '%s'"), slave);
  514. if (errno == 0)
  515. return true;
  516. }
  517. return false;
  518. }
  519. struct slave_link {
  520. struct slave_link *next;
  521. char *name;
  522. char *link;
  523. };
  524. struct commit_operation {
  525. struct commit_operation *next;
  526. enum opcode {
  527. opcode_nop,
  528. opcode_rm,
  529. opcode_mv,
  530. } opcode;
  531. char *arg_a;
  532. char *arg_b;
  533. };
  534. struct alternative {
  535. char *master_name;
  536. char *master_link;
  537. char *current;
  538. enum alternative_status {
  539. ALT_ST_UNKNOWN,
  540. ALT_ST_AUTO,
  541. ALT_ST_MANUAL,
  542. } status;
  543. struct slave_link *slaves;
  544. struct fileset *choices;
  545. struct commit_operation *commit_ops;
  546. int ref_count;
  547. bool modified;
  548. bool known_current;
  549. };
  550. static void
  551. slave_link_free(struct slave_link *slave)
  552. {
  553. free(slave->name);
  554. free(slave->link);
  555. free(slave);
  556. }
  557. static void
  558. commit_operation_free(struct commit_operation *commit_op)
  559. {
  560. free(commit_op->arg_a);
  561. free(commit_op->arg_b);
  562. free(commit_op);
  563. }
  564. static struct alternative *
  565. alternative_new(const char *name)
  566. {
  567. struct alternative *alt;
  568. alt = xmalloc(sizeof(*alt));
  569. alt->master_name = xstrdup(name);
  570. alt->master_link = NULL;
  571. alt->current = NULL;
  572. alt->status = ALT_ST_UNKNOWN;
  573. alt->slaves = NULL;
  574. alt->choices = NULL;
  575. alt->commit_ops = NULL;
  576. alt->modified = false;
  577. alt->known_current = false;
  578. alt->ref_count = 1;
  579. return alt;
  580. }
  581. static inline void
  582. alternative_ref(struct alternative *a)
  583. {
  584. a->ref_count++;
  585. }
  586. static inline bool
  587. alternative_unref(struct alternative *a)
  588. {
  589. return --a->ref_count == 0;
  590. }
  591. static void
  592. alternative_choices_free(struct alternative *a)
  593. {
  594. struct fileset *fs;
  595. if (a->choices)
  596. a->modified = true;
  597. while (a->choices) {
  598. fs = a->choices;
  599. a->choices = fs->next;
  600. fileset_free(fs);
  601. }
  602. }
  603. static void
  604. alternative_commit_operations_free(struct alternative *a)
  605. {
  606. struct commit_operation *op;
  607. while (a->commit_ops) {
  608. op = a->commit_ops;
  609. a->commit_ops = op->next;
  610. commit_operation_free(op);
  611. }
  612. }
  613. static void
  614. alternative_reset(struct alternative *alt)
  615. {
  616. struct slave_link *slave;
  617. free(alt->current);
  618. alt->current = NULL;
  619. free(alt->master_link);
  620. alt->master_link = NULL;
  621. while (alt->slaves) {
  622. slave = alt->slaves;
  623. alt->slaves = slave->next;
  624. slave_link_free(slave);
  625. }
  626. alternative_choices_free(alt);
  627. alternative_commit_operations_free(alt);
  628. alt->modified = false;
  629. alt->known_current = false;
  630. }
  631. static void
  632. alternative_free(struct alternative *alt)
  633. {
  634. if (!alternative_unref(alt))
  635. return;
  636. alternative_reset(alt);
  637. free(alt->master_name);
  638. free(alt);
  639. }
  640. static int
  641. alternative_choices_count(struct alternative *alt)
  642. {
  643. struct fileset *fs;
  644. int count = 0;
  645. for (fs = alt->choices; fs; fs = fs->next)
  646. count++;
  647. return count;
  648. }
  649. static int
  650. alternative_slaves_count(struct alternative *alt)
  651. {
  652. struct slave_link *sl;
  653. int count = 0;
  654. for (sl = alt->slaves; sl; sl = sl->next)
  655. count++;
  656. return count;
  657. }
  658. static int
  659. compare_fileset(const void *va, const void *vb)
  660. {
  661. const struct fileset *a = *(const struct fileset **)va;
  662. const struct fileset *b = *(const struct fileset **)vb;
  663. assert(a && a->master_file);
  664. assert(b && b->master_file);
  665. return strcmp(a->master_file, b->master_file);
  666. }
  667. static int
  668. compare_slave_link(const void *va, const void *vb)
  669. {
  670. const struct slave_link *a = *(const struct slave_link **)va;
  671. const struct slave_link *b = *(const struct slave_link **)vb;
  672. assert(a && a->name);
  673. assert(b && b->name);
  674. return strcmp(a->name, b->name);
  675. }
  676. static void
  677. alternative_sort_choices(struct alternative *a)
  678. {
  679. int count, i;
  680. struct fileset **table, *fs;
  681. count = alternative_choices_count(a);
  682. if (count < 2) /* Nothing to sort */
  683. return;
  684. /* Store objects in a table instead of a linked list */
  685. table = xmalloc(sizeof(fs) * count);
  686. for (fs = a->choices, i = 0; fs; fs = fs->next) {
  687. assert(fs->master_file);
  688. table[i++] = fs;
  689. }
  690. qsort(table, count, sizeof(fs), compare_fileset);
  691. /* Rewrite the linked list from the sorted table */
  692. a->choices = fs = table[0];
  693. table[count - 1]->next = NULL;
  694. for (i = 1; i < count; fs = fs->next, i++)
  695. fs->next = table[i];
  696. free(table);
  697. }
  698. static void
  699. alternative_sort_slaves(struct alternative *a)
  700. {
  701. int count, i;
  702. struct slave_link **table, *sl;
  703. count = alternative_slaves_count(a);
  704. if (count < 2) /* Nothing to sort */
  705. return;
  706. /* Store objects in a table instead of a linked list */
  707. table = xmalloc(sizeof(sl) * count);
  708. for (sl = a->slaves, i = 0; sl; sl = sl->next, i++) {
  709. table[i] = sl;
  710. }
  711. qsort(table, count, sizeof(sl), compare_slave_link);
  712. /* Rewrite the linked list from the sorted table */
  713. a->slaves = sl = table[0];
  714. table[count - 1]->next = NULL;
  715. for (i = 1; i < count; sl = sl->next, i++)
  716. sl->next = table[i];
  717. free(table);
  718. }
  719. static struct fileset *
  720. alternative_get_fileset(struct alternative *a, const char *file)
  721. {
  722. struct fileset *fs;
  723. for (fs = a->choices; fs; fs = fs->next)
  724. if (strcmp(fs->master_file, file) == 0)
  725. return fs;
  726. return NULL;
  727. }
  728. static struct slave_link *
  729. alternative_get_slave(struct alternative *a, const char *name)
  730. {
  731. struct slave_link *sl;
  732. for (sl = a->slaves; sl; sl = sl->next)
  733. if (strcmp(sl->name, name) == 0)
  734. return sl;
  735. return NULL;
  736. }
  737. static bool
  738. alternative_has_slave(struct alternative *a, const char *name)
  739. {
  740. return alternative_get_slave(a, name) != NULL;
  741. }
  742. static bool
  743. alternative_has_choice(struct alternative *a, const char *file)
  744. {
  745. return alternative_get_fileset(a, file) != NULL;
  746. }
  747. static void
  748. alternative_add_choice(struct alternative *a, struct fileset *fs)
  749. {
  750. struct fileset *cur, *prev = NULL;
  751. /* Replace if already existing */
  752. for (cur = a->choices; cur; cur = cur->next) {
  753. if (strcmp(cur->master_file, fs->master_file) == 0) {
  754. fs->next = cur->next;
  755. fileset_free(cur);
  756. if (prev)
  757. prev->next = fs;
  758. else
  759. a->choices = fs;
  760. /* XXX: Be smarter in detecting change? */
  761. a->modified = true;
  762. return;
  763. }
  764. prev = cur;
  765. }
  766. /* Otherwise add at the end */
  767. if (prev == NULL)
  768. a->choices = fs;
  769. else
  770. prev->next = fs;
  771. fs->next = NULL;
  772. a->modified = true;
  773. }
  774. /* slave_name and slave_link must be allocated with malloc */
  775. static void
  776. alternative_add_slave(struct alternative *a, char *slave_name,
  777. char *slave_link)
  778. {
  779. struct slave_link *sl, *new;
  780. /* Replace if already existing */
  781. for (sl = a->slaves; sl; sl = sl->next) {
  782. if (strcmp(sl->name, slave_name) == 0) {
  783. free(sl->link);
  784. sl->link = xstrdup(slave_link);
  785. return;
  786. }
  787. if (sl->next == NULL)
  788. break;
  789. }
  790. /* Otherwise create new and add at the end */
  791. new = xmalloc(sizeof(*new));
  792. new->name = slave_name;
  793. new->link = slave_link;
  794. new->next = NULL;
  795. if (sl)
  796. sl->next = new;
  797. else
  798. a->slaves = new;
  799. }
  800. static const char *
  801. alternative_status_string(enum alternative_status status)
  802. {
  803. return (status == ALT_ST_AUTO) ? "auto" : "manual";
  804. }
  805. static void
  806. alternative_set_status(struct alternative *a, enum alternative_status status)
  807. {
  808. if (a->status == ALT_ST_UNKNOWN || status != a->status)
  809. a->modified = true;
  810. if (a->status != ALT_ST_UNKNOWN && status != a->status)
  811. log_msg("status of link group %s set to %s", a->master_link,
  812. alternative_status_string(status));
  813. a->status = status;
  814. }
  815. /* link must be allocated with malloc */
  816. static void
  817. alternative_set_link(struct alternative *a, char *linkname)
  818. {
  819. if (a->master_link == NULL || strcmp(linkname, a->master_link) != 0)
  820. a->modified = true;
  821. free(a->master_link);
  822. a->master_link = linkname;
  823. }
  824. static bool
  825. alternative_remove_choice(struct alternative *a, const char *file)
  826. {
  827. struct fileset *fs, *fs_prev;
  828. fs_prev = NULL;
  829. for (fs = a->choices; fs; fs = fs->next) {
  830. if (strcmp(fs->master_file, file) != 0) {
  831. fs_prev = fs;
  832. continue;
  833. }
  834. if (fs_prev)
  835. fs_prev->next = fs->next;
  836. else
  837. a->choices = fs->next;
  838. fileset_free(fs);
  839. a->modified = true;
  840. return true;
  841. }
  842. return false;
  843. }
  844. /*
  845. * Alternatives Database Load/Store functions.
  846. */
  847. enum altdb_flags {
  848. altdb_lax_parser = 1 << 0,
  849. altdb_warn_parser = 1 << 1,
  850. };
  851. struct altdb_context {
  852. FILE *fh;
  853. char *filename;
  854. enum altdb_flags flags;
  855. bool modified;
  856. void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  857. (*bad_format)(struct altdb_context *, const char *format, ...);
  858. jmp_buf on_error;
  859. };
  860. static int
  861. altdb_filter_namelist(const struct dirent *entry)
  862. {
  863. if (strcmp(entry->d_name, ".") == 0 ||
  864. strcmp(entry->d_name, "..") == 0 ||
  865. (strlen(entry->d_name) > strlen(DPKG_TMP_EXT) &&
  866. strcmp(entry->d_name + strlen(entry->d_name) -
  867. strlen(DPKG_TMP_EXT), DPKG_TMP_EXT) == 0))
  868. return 0;
  869. return 1;
  870. }
  871. static int
  872. altdb_get_namelist(struct dirent ***table)
  873. {
  874. int count;
  875. count = scandir(admdir, table, altdb_filter_namelist, alphasort);
  876. if (count < 0)
  877. syserr(_("cannot scan directory `%.255s'"), admdir);
  878. return count;
  879. }
  880. static void
  881. altdb_free_namelist(struct dirent **table, int n)
  882. {
  883. while (n--)
  884. free(table[n]);
  885. free(table);
  886. }
  887. static char *
  888. altdb_get_line(struct altdb_context *ctx, const char *name)
  889. {
  890. char *buf, *line;
  891. size_t len, bufsz, i;
  892. bufsz = 1024;
  893. buf = xmalloc(bufsz);
  894. for (i = 0; true; i += strlen(line)) {
  895. errno = 0;
  896. line = fgets(buf + i, bufsz - i, ctx->fh);
  897. if (line) {
  898. if (strlen(buf) < bufsz - 1 || buf[bufsz - 2] == '\n')
  899. break;
  900. /* Need more space */
  901. bufsz *= 2;
  902. buf = realloc(buf, bufsz);
  903. if (!buf)
  904. error(_("failed to allocate memory"));
  905. continue;
  906. }
  907. if (feof(ctx->fh))
  908. ctx->bad_format(ctx, _("unexpected end of file while trying "
  909. "to read %s"), name);
  910. ctx->bad_format(ctx, _("while reading %s: %s"),
  911. name, strerror(errno));
  912. }
  913. len = strlen(buf);
  914. if (len == 0 || buf[len - 1] != '\n') {
  915. ctx->bad_format(ctx, _("line not terminated while trying "
  916. "to read %s"), name);
  917. }
  918. line[len - 1] = '\0';
  919. return buf;
  920. }
  921. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  922. altdb_parse_error(struct altdb_context *ctx, const char *format, ...)
  923. {
  924. char *msg;
  925. va_list args;
  926. va_start(args, format);
  927. xvasprintf(&msg, format, args);
  928. va_end(args);
  929. error(_("%s corrupt: %s"), ctx->filename, msg);
  930. }
  931. static void DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2)
  932. altdb_parse_stop(struct altdb_context *ctx, const char *format, ...)
  933. {
  934. longjmp(ctx->on_error, 1);
  935. }
  936. static void
  937. altdb_print_line(struct altdb_context *ctx, const char *line)
  938. {
  939. if (strchr(line, '\n') != NULL)
  940. error(_("newlines prohibited in update-alternatives files (%s)"),
  941. line);
  942. if (fprintf(ctx->fh, "%s\n", line) < (int) strlen(line) + 1)
  943. syserr(_("unable to write file '%s'"), ctx->filename);
  944. }
  945. static bool
  946. alternative_parse_slave(struct alternative *a, struct altdb_context *ctx)
  947. {
  948. char *name, *linkname;
  949. struct slave_link *sl;
  950. name = altdb_get_line(ctx, _("slave name"));
  951. if (!strlen(name)) { /* End of list */
  952. free(name);
  953. return false;
  954. }
  955. if (alternative_has_slave(a, name)) {
  956. sl = alternative_get_slave(a, name);
  957. free(name);
  958. ctx->bad_format(ctx, _("duplicate slave name %s"), sl->name);
  959. }
  960. linkname = altdb_get_line(ctx, _("slave link"));
  961. if (strcmp(linkname, a->master_link) == 0) {
  962. free(linkname);
  963. free(name);
  964. ctx->bad_format(ctx, _("slave link same as main link %s"),
  965. a->master_link);
  966. }
  967. for (sl = a->slaves; sl; sl = sl->next) {
  968. if (strcmp(linkname, sl->link) == 0) {
  969. free(linkname);
  970. free(name);
  971. ctx->bad_format(ctx, _("duplicate slave link %s"),
  972. sl->link);
  973. }
  974. }
  975. alternative_add_slave(a, name, linkname);
  976. return true;
  977. }
  978. static bool
  979. alternative_parse_fileset(struct alternative *a, struct altdb_context *ctx)
  980. {
  981. struct fileset *fs;
  982. struct slave_link *sl;
  983. struct stat st;
  984. char *master_file;
  985. master_file = altdb_get_line(ctx, _("master file"));
  986. if (!strlen(master_file)) { /* End of list */
  987. free(master_file);
  988. return false;
  989. }
  990. fs = alternative_get_fileset(a, master_file);
  991. if (fs) {
  992. free(master_file);
  993. ctx->bad_format(ctx, _("duplicate path %s"), master_file);
  994. }
  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. }