update-alternatives.c 62 KB

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