update-alternatives.c 59 KB

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