update-alternatives.c 59 KB

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