update-alternatives.c 59 KB

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