update-alternatives.c 66 KB

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