update-alternatives.c 63 KB

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