update-alternatives.c 59 KB

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