update-alternatives.c 63 KB

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