update-alternatives.c 64 KB

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