update-alternatives.c 63 KB

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