update-alternatives.c 63 KB

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