update-alternatives.c 67 KB

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