update-alternatives.c 63 KB

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