update-alternatives.c 64 KB

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