OSDN Git Service

Remove the no-longer-needed #include <sys/user.h>
[uclinux-h8/uClibc.git] / utils / ldconfig.c
1 /*
2  * ldconfig - update shared library symlinks
3  *
4  * usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...
5  *        ldconfig -l [-Dv] lib ...
6  *        ldconfig -p
7  *        -D: debug mode, don't update links
8  *        -v: verbose mode, print things as we go
9  *        -q: quiet mode, don't print warnings
10  *        -n: don't process standard directories
11  *        -N: don't update the library cache
12  *        -X: don't update the library links
13  *        -l: library mode, manually link libraries
14  *        -p: print the current library cache
15  *        -f conf: use conf instead of /etc/ld.so.conf
16  *        -C cache: use cache instead of /etc/ld.so.cache
17  *        -r root: first, do a chroot to the indicated directory
18  *        dir ...: directories to process
19  *        lib ...: libraries to link
20  *
21  * Copyright 1994-2000 David Engel and Mitch D'Souza
22  *
23  * This program may be used for any purpose as long as this
24  * copyright notice is kept.
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <getopt.h>
33 #include <dirent.h>
34 #include <unistd.h>
35 #include <link.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include "dl-elf.h"
41 #include "readsoname.h"
42
43 #define BUFFER_SIZE 4096
44
45 struct exec
46 {
47   unsigned long a_info;         /* Use macros N_MAGIC, etc for access */
48   unsigned a_text;              /* length of text, in bytes */
49   unsigned a_data;              /* length of data, in bytes */
50   unsigned a_bss;               /* length of uninitialized data area for file, in bytes */
51   unsigned a_syms;              /* length of symbol table data in file, in bytes */
52   unsigned a_entry;             /* start address */
53   unsigned a_trsize;            /* length of relocation info for text, in bytes */
54   unsigned a_drsize;            /* length of relocation info for data, in bytes */
55 };
56
57 #if !defined (N_MAGIC)
58 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
59 #endif
60 /* Code indicating object file or impure executable.  */
61 #define OMAGIC 0407
62 /* Code indicating pure executable.  */
63 #define NMAGIC 0410
64 /* Code indicating demand-paged executable.  */
65 #define ZMAGIC 0413
66 /* This indicates a demand-paged executable with the header in the text. 
67    The first page is unmapped to help trap NULL pointer references */
68 #define QMAGIC 0314
69 /* Code indicating core file.  */
70 #define CMAGIC 0421
71
72 char *___strtok = NULL;
73
74 /* For SunOS */
75 #ifndef PATH_MAX
76 #include <limits.h>
77 #define PATH_MAX _POSIX_PATH_MAX
78 #endif
79
80 /* For SunOS */
81 #ifndef N_MAGIC
82 #define N_MAGIC(exec) ((exec).a_magic & 0xffff)
83 #endif
84
85 #define EXIT_OK    0
86 #define EXIT_FATAL 128
87
88 char *prog = NULL;
89 int debug = 0;                  /* debug mode */
90 int verbose = 0;                /* verbose mode */
91 int libmode = 0;                /* library mode */
92 int nolinks = 0;                /* don't update links */
93 int nocache = 0;                /* don't build cache */
94 void cache_print(void);
95 void cache_write(void);
96 void cache_dolib(const char *dir, const char *so, int libtype);
97 #ifdef __LDSO_CACHE_SUPPORT__
98 char *conffile = LDSO_CONF;     /* default conf file */
99 char *cachefile = LDSO_CACHE;   /* default cache file */
100 #endif
101
102 /* These two are used internally -- you shouldn't need to use them */
103 static void verror_msg(const char *s, va_list p)
104 {
105         fflush(stdout);
106         fprintf(stderr, "%s: ", prog);
107         vfprintf(stderr, s, p);
108 }
109
110 extern void warnx(const char *s, ...)
111 {
112         va_list p;
113
114         va_start(p, s);
115         verror_msg(s, p);
116         va_end(p);
117         fprintf(stderr, "\n");
118 }
119
120 extern void err(int errnum, const char *s, ...)
121 {
122         va_list p;
123
124         va_start(p, s);
125         verror_msg(s, p);
126         va_end(p);
127         fprintf(stderr, "\n");
128         exit(errnum);
129 }
130
131 static void vperror_msg(const char *s, va_list p)
132 {
133         int err = errno;
134
135         if (s == 0)
136                 s = "";
137         verror_msg(s, p);
138         if (*s)
139                 s = ": ";
140         fprintf(stderr, "%s%s\n", s, strerror(err));
141 }
142
143 extern void warn(const char *s, ...)
144 {
145         va_list p;
146
147         va_start(p, s);
148         vperror_msg(s, p);
149         va_end(p);
150 }
151
152 void *xmalloc(size_t size)
153 {
154     void *ptr;
155     if ((ptr = malloc(size)) == NULL)
156         err(EXIT_FATAL,"out of memory");
157     return ptr;
158 }
159
160 char *xstrdup(const char *str)
161 {
162     char *ptr;
163     if ((ptr = strdup(str)) == NULL)
164         err(EXIT_FATAL,"out of memory");
165     return ptr;
166 }
167
168 /* If shared library, return a malloced copy of the soname and set the
169    type, else return NULL.
170
171    expected_type should be either LIB_ANY or one of the following:-
172    LIB_DLL
173    LIB_ELF
174    LIB_ELF_LIBC5
175    LIB_ELF_LIBC6
176
177    If the lib is ELF and we can not deduce the type the type will
178    be set based on expected_type.
179
180    If the expected, actual/deduced types missmatch we display a warning
181    and use the actual/deduced type.
182 */
183 char *is_shlib(const char *dir, const char *name, int *type,
184         int *islink, int expected_type)
185 {
186     char *good = NULL;
187     char *cp, *cp2;
188     FILE *file;
189     struct exec exec;
190     ElfW(Ehdr) *elf_hdr;
191     struct stat statbuf;
192     char buff[BUFFER_SIZE];
193
194     /* see if name is of the form *.so* */
195     if (name[strlen(name)-1] != '~' && (cp = strstr(name, ".so")))
196     {
197         /* find the start of the Vminor part, if any */
198         if (cp[3] == '.' && (cp2 = strchr(cp + 4, '.')))
199             cp = cp2;
200         else
201             cp = cp + strlen(cp);
202
203         /* construct the full path name */
204         sprintf(buff, "%s%s%s", dir, (*dir && strcmp(dir, "/")) ?
205                 "/" : "", name);
206
207         /* first, make sure it's a regular file */
208         if (lstat(buff, &statbuf))
209             warn("skipping %s", buff);
210         else if (!S_ISREG(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode))
211             warnx("%s is not a regular file or symlink, skipping", buff);
212         else
213         {
214             /* is it a regular file or a symlink */
215             *islink = S_ISLNK(statbuf.st_mode);
216
217             /* then try opening it */
218             if (!(file = fopen(buff, "rb")))
219                 warn("skipping %s", buff);
220             else
221             {
222                 /* now make sure it's a shared library */
223                 if (fread(&exec, sizeof exec, 1, file) < 1)
224                     warnx("can't read header from %s, skipping", buff);
225                 else if (N_MAGIC(exec) != ZMAGIC && N_MAGIC(exec) != QMAGIC)
226                 {
227                     elf_hdr = (ElfW(Ehdr) *) &exec;
228                     if (elf_hdr->e_ident[0] != 0x7f ||
229                             strncmp(&elf_hdr->e_ident[1], "ELF",3) != 0)
230                     {
231                         /* silently ignore linker scripts */
232                         if (strncmp((char *)&exec, "/* GNU ld", 9) != 0)
233                             warnx("%s is not a shared library, skipping", buff);
234                     }
235                     else
236                     {
237                         /* always call readsoname to update type */
238                         if(expected_type == LIB_DLL) {
239                             warnx("%s is not an a.out library, its ELF!\n", buff);
240                             expected_type=LIB_ANY;
241                         }
242                         *type = LIB_ELF;
243                         good = readsoname(buff, file, expected_type, type, 
244                                 elf_hdr->e_ident[EI_CLASS]);
245                         if (good == NULL || *islink)
246                         {
247                             if (good != NULL)
248                                 free(good);
249                             good = xstrdup(name);
250                         }
251                         else
252                         {
253                             /* if the soname does not match the filename,
254                                issue a warning, but only in debug mode. */
255                             int len = strlen(good);
256                             if (debug && (strncmp(good, name, len) != 0 ||
257                                         (name[len] != '\0' && name[len] != '.')))
258                                 warnx("%s has inconsistent soname (%s)", buff, good);
259                         }
260                     }
261                 }
262                 else
263                 {
264                     if (*islink)
265                         good = xstrdup(name);
266                     else
267                     {
268                         good = xmalloc(cp - name + 1);
269                         strncpy(good, name, cp - name);
270                         good[cp - name] = '\0';
271                     }
272                     if(expected_type != LIB_ANY && expected_type != LIB_DLL)
273                     {
274                         warnx("%s is not an ELF library, its an a.out DLL!", buff);
275                         expected_type=LIB_ANY;
276                     }
277
278                     *type = LIB_DLL;
279                 }
280                 fclose(file);
281             }
282         }
283     }
284
285     return good;
286 }
287
288 /* update the symlink to new library */
289 void link_shlib(const char *dir, const char *file, const char *so)
290 {
291     int change = 1;
292     char libname[BUFFER_SIZE];
293     char linkname[BUFFER_SIZE];
294     struct stat libstat;
295     struct stat linkstat;
296
297     /* construct the full path names */
298     sprintf(libname, "%s/%s", dir, file);
299     sprintf(linkname, "%s/%s", dir, so);
300
301     /* see if a link already exists */
302     if (!stat(linkname, &linkstat))
303     {
304         /* now see if it's the one we want */
305         if (stat(libname, &libstat))
306             warn("can't stat %s", libname);
307         else if (libstat.st_dev == linkstat.st_dev &&
308                 libstat.st_ino == linkstat.st_ino)
309             change = 0;
310     }
311
312     /* then update the link, if required */
313     if (change > 0 && !nolinks)
314     {
315         if (!lstat(linkname, &linkstat))
316         {
317             if (!S_ISLNK(linkstat.st_mode))
318             {
319                 warnx("%s is not a symlink", linkname);
320                 change = -1;
321             }
322             else if (remove(linkname))
323             {
324                 warn("can't unlink %s", linkname);
325                 change = -1;
326             }
327         }
328         if (change > 0)
329         {
330             if (symlink(file, linkname))
331             {
332                 warn("can't link %s to %s", linkname, file);
333                 change = -1;
334             }
335         }
336     }
337
338     /* some people like to know what we're doing */
339     if (verbose > 0)
340         printf("\t%s => %s%s\n", so, file,
341                 change < 0 ? " (SKIPPED)" :
342                 (change > 0 ? " (changed)" : ""));
343
344     return;
345 }
346
347 /* figure out which library is greater */
348 int libcmp(char *p1, char *p2)
349 {
350     while (*p1)
351     {
352         if (isdigit(*p1) && isdigit(*p2))
353         {
354             /* must compare this numerically */
355             int v1, v2;
356             v1 = strtoul(p1, &p1, 10);
357             v2 = strtoul(p2, &p2, 10);
358             if (v1 != v2)
359                 return v1 - v2;
360         }
361         else if (isdigit(*p1) && !isdigit(*p2))
362             return 1;
363         else if (!isdigit(*p1) && isdigit(*p2))
364             return -1;
365         else if (*p1 != *p2)
366             return *p1 - *p2;
367         else
368             p1++, p2++;
369     }
370
371     return *p1 - *p2;
372 }
373
374 struct lib
375 {
376     char *so;                   /* soname of a library */
377     char *name;                 /* name of a library */
378     int libtype;                /* type of a library */
379     int islink;                 /* is it a symlink */
380     struct lib *next;           /* next library in list */
381 };
382
383 /* update all shared library links in a directory */
384 void scan_dir(const char *rawname)
385 {
386     DIR *dir;
387     const char *name;
388     struct dirent *ent;
389     char *so, *path, *path_n;
390     struct lib *lp, *libs = NULL;
391     int i, libtype, islink, expected_type = LIB_ANY;
392
393     /* We need a writable copy of this string */
394     path = strdup(rawname);
395     if (!path) {
396         err(EXIT_FATAL, "Out of memory!\n");
397     }
398     /* Eliminate all double //s */
399     path_n=path;
400     while((path_n=strstr(path_n, "//"))) {
401         i = strlen(path_n);
402         memmove(path_n, path_n+1, i-1);
403         *(path_n + i - 1)='\0';
404     }
405     name = path;
406
407 #if 0
408     char *t;
409     /* Check for an embedded expected type */
410     t=strrchr(name, '=');
411     if( t )
412     {
413         *t++ = '\0'; /* Skip = char */
414         if(strcasecmp(t, "libc4") == 0)
415         {
416             expected_type = LIB_DLL;
417         } 
418         else
419         {
420             if(strcasecmp(t, "libc5") == 0)
421             {
422                 expected_type = LIB_ELF_LIBC5; 
423             }
424             else
425             {
426                 if(strcasecmp(t, "libc6") == 0)
427                 {
428                     expected_type = LIB_ELF_LIBC6;
429                 }
430                 else 
431                 {
432                     if(strcasecmp(t, "libc0") == 0)
433                     {
434                         expected_type = LIB_ELF_LIBC0;
435                     }
436                     else
437                     {
438                         warnx("Unknown type field '%s' for dir '%s' - ignored\n", t, name);
439                         expected_type = LIB_ANY;
440                     }
441                 }
442             }
443         }
444     }
445 #endif
446
447     /* let 'em know what's going on */
448     if (verbose > 0)
449         printf("%s:\n", name);
450
451     /* if we can't open it, we can't do anything */
452     if ((dir = opendir(name)) == NULL)
453     {
454         warn("skipping %s", name);
455         free(path);
456         return;
457     }
458
459     /* yes, we have to look at every single file */
460     while ((ent = readdir(dir)) != NULL)
461     {
462         /* if it's not a shared library, don't bother */
463         if ((so = is_shlib(name, ent->d_name, &libtype, &islink, expected_type)) == NULL)
464             continue;
465
466         /* have we already seen one with the same so name? */
467         for (lp = libs; lp; lp = lp->next)
468         {
469             if (strcmp(so, lp->so) == 0)
470             {
471                 /* we have, which one do we want to use? */
472                 if ((!islink && lp->islink) ||
473                         (islink == lp->islink && 
474                          libcmp(ent->d_name, lp->name) > 0))
475                 {
476                     /* let's use the new one */
477                     free(lp->name);
478                     lp->name = xstrdup(ent->d_name);
479                     lp->libtype = libtype;
480                     lp->islink = islink;
481                 } 
482                 break;
483             }
484         }
485
486         /* congratulations, you're the first one we've seen */
487         if (!lp)
488         {
489             lp = xmalloc(sizeof *lp);
490             lp->so = xstrdup(so);
491             lp->name = xstrdup(ent->d_name);
492             lp->libtype = libtype;
493             lp->islink = islink;
494             lp->next = libs;
495             libs = lp;
496         }
497
498         free(so);
499     }
500
501     /* don't need this any more */
502     closedir(dir);
503
504     /* now we have all the latest libs, update the links */
505     for (lp = libs; lp; lp = lp->next)
506     {
507         if (!lp->islink)
508             link_shlib(name, lp->name, lp->so);
509         if (!nocache)
510             cache_dolib(name, lp->so, lp->libtype);
511     }
512
513     /* always try to clean up after ourselves */
514     while (libs)
515     {
516         lp = libs->next;
517         free(libs->so);
518         free(libs->name);
519         free(libs);
520         libs = lp;
521     }
522
523     free(path);
524     return;
525 }
526
527 #ifndef __LDSO_CACHE_SUPPORT__
528 void cache_print(void)
529 {
530     printf("Library cache disabled\n");
531 }
532 void cache_dolib(const char *dir, const char *so, int libtype)
533 {
534     return;
535 }
536 void cache_write(void)
537 {
538     return;
539 }
540 #else
541 /* return the list of system-specific directories */
542 char *get_extpath(void)
543 {
544     char *res = NULL, *cp;
545     FILE *file;
546     struct stat stat;
547
548     if ((file = fopen(conffile, "r")) != NULL)
549     {
550         fstat(fileno(file), &stat);
551         res = xmalloc(stat.st_size + 1);
552         fread(res, 1, stat.st_size, file);
553         fclose(file);
554         res[stat.st_size] = '\0';
555
556         /* convert comments fo spaces */
557         for (cp = res; *cp; /*nada*/) {
558             if (*cp == '#') {
559                 do
560                     *cp++ = ' ';
561                 while (*cp && *cp != '\n');
562             } else {
563                 cp++;
564             }
565         }         
566     }
567
568     return res;
569 }
570
571 typedef struct liblist
572 {
573     int flags;
574     int sooffset;
575     int liboffset;
576     char *soname;
577     char *libname;
578     struct liblist *next;
579 } liblist_t;
580
581 static header_t magic = { LDSO_CACHE_MAGIC, LDSO_CACHE_VER, 0 };
582 static liblist_t *lib_head = NULL;
583
584 static int liblistcomp(liblist_t *x, liblist_t *y)
585 {
586     int res;
587
588     if ((res = libcmp(x->soname, y->soname)) == 0)
589     {
590         res = libcmp(strrchr(x->libname, '/') + 1,
591                 strrchr(y->libname, '/') + 1);
592     }
593
594     return res;
595 }
596
597 void cache_dolib(const char *dir, const char *so, int libtype)
598 {
599     char fullpath[PATH_MAX];
600     liblist_t *new_lib, *cur_lib;
601
602     magic.nlibs++;
603     sprintf(fullpath, "%s/%s", dir, so);
604     new_lib = xmalloc(sizeof (liblist_t));
605     new_lib->flags = libtype;
606     new_lib->soname = xstrdup(so);
607     new_lib->libname = xstrdup(fullpath);
608
609     if (lib_head == NULL || liblistcomp(new_lib, lib_head) > 0)
610     {
611         new_lib->next = lib_head;
612         lib_head = new_lib;
613     }
614     else
615     {
616         for (cur_lib = lib_head; cur_lib->next != NULL &&
617                 liblistcomp(new_lib, cur_lib->next) <= 0;
618                 cur_lib = cur_lib->next)
619             /* nothing */;
620         new_lib->next = cur_lib->next;
621         cur_lib->next = new_lib;
622     }
623 }
624
625 void cache_write(void)
626 {
627     int cachefd;
628     int stroffset = 0;
629     char tempfile[BUFFER_SIZE];
630     liblist_t *cur_lib;
631
632     if (!magic.nlibs)
633         return;
634
635     sprintf(tempfile, "%s~", cachefile);
636
637     if (unlink(tempfile) && errno != ENOENT)
638         err(EXIT_FATAL,"can't unlink %s (%s)", tempfile, strerror(errno));
639
640     if ((cachefd = creat(tempfile, 0644)) < 0)
641         err(EXIT_FATAL,"can't create %s (%s)", tempfile, strerror(errno));
642
643     if (write(cachefd, &magic, sizeof (header_t)) != sizeof (header_t))
644         err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
645
646     for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
647     {
648         cur_lib->sooffset = stroffset;
649         stroffset += strlen(cur_lib->soname) + 1;
650         cur_lib->liboffset = stroffset;
651         stroffset += strlen(cur_lib->libname) + 1;
652         if (write(cachefd, cur_lib, sizeof (libentry_t)) !=
653                 sizeof (libentry_t))
654             err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
655     }
656
657     for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next)
658     {
659         if (write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
660                 != strlen(cur_lib->soname) + 1)
661             err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
662         if (write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
663                 != strlen(cur_lib->libname) + 1)
664             err(EXIT_FATAL,"can't write %s (%s)", tempfile, strerror(errno));
665     }
666
667     if (close(cachefd))
668         err(EXIT_FATAL,"can't close %s (%s)", tempfile, strerror(errno));
669
670     if (chmod(tempfile, 0644))
671         err(EXIT_FATAL,"can't chmod %s (%s)", tempfile, strerror(errno));
672
673     if (rename(tempfile, cachefile))
674         err(EXIT_FATAL,"can't rename %s (%s)", tempfile, strerror(errno));
675 }
676
677 void cache_print(void)
678 {
679     caddr_t c;
680     struct stat st;
681     int fd = 0;
682     char *strs;
683     header_t *header;
684     libentry_t *libent;
685
686     if (stat(cachefile, &st) || (fd = open(cachefile, O_RDONLY))<0)
687         err(EXIT_FATAL,"can't read %s (%s)", cachefile, strerror(errno));
688     if ((c = mmap(0,st.st_size, PROT_READ, MAP_SHARED ,fd, 0)) == (caddr_t)-1)
689         err(EXIT_FATAL,"can't map %s (%s)", cachefile, strerror(errno));
690     close(fd);
691
692     if (memcmp(((header_t *)c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
693         err(EXIT_FATAL,"%s cache corrupt", cachefile);
694
695     if (memcmp(((header_t *)c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
696         err(EXIT_FATAL,"wrong cache version - expected %s", LDSO_CACHE_VER);
697
698     header = (header_t *)c;
699     libent = (libentry_t *)(c + sizeof (header_t));
700     strs = (char *)&libent[header->nlibs];
701
702     printf("%d libs found in cache `%s' (version %s)\n",
703             header->nlibs, cachefile, LDSO_CACHE_VER);
704
705     for (fd = 0; fd < header->nlibs; fd++)
706     {
707         printf("\t%s ", strs + libent[fd].sooffset);
708         switch (libent[fd].flags & ~LIB_ELF64) 
709         {
710             case LIB_DLL:
711                 printf("(libc4)");
712                 break;
713             case LIB_ELF:
714                 printf("(ELF%s)", libent[fd].flags & LIB_ELF64 ? "/64" : "");
715                 break;
716             case LIB_ELF_LIBC0:
717                 printf("(libc0%s)",libent[fd].flags & LIB_ELF64 ? "/64" : "");
718                 break;
719             case LIB_ELF_LIBC5:
720             case LIB_ELF_LIBC6:
721                 printf("(libc%d%s)", (libent[fd].flags & ~LIB_ELF64) + 3,
722                         libent[fd].flags & LIB_ELF64 ? "/64" : "");
723                 break;
724             default:
725                 printf("(unknown)");
726                 break;
727         }
728         printf(" => %s\n", strs + libent[fd].liboffset);
729     }
730
731     munmap (c,st.st_size);
732 }
733 #endif
734
735 void usage(void)
736 {
737     fprintf(stderr,
738 #ifdef __LDSO_CACHE_SUPPORT__
739             "ldconfig - updates symlinks and cache for shared libraries\n\n"
740             "Usage: ldconfig [-DvqnNX] [-f conf] [-C cache] [-r root] dir ...\n"
741             "       ldconfig -l [-Dv] lib ...\n"
742             "       ldconfig -p\n\nOptions:\n"
743 #else
744             "ldconfig - updates symlinks for shared libraries\n\n"
745             "Usage: ldconfig [-DvqnX] [-r root] dir ...\n"
746             "       ldconfig -l [-Dv] lib ...\n\nOptions:\n"
747 #endif
748             "\t-D:\t\tdebug mode, don't update links\n"
749             "\t-v:\t\tverbose mode, print things as we go\n"
750             "\t-q:\t\tquiet mode, don't print warnings\n"
751             "\t-n:\t\tdon't process standard directories\n"
752             "\t-N:\t\tdon't update the library cache\n"
753             "\t-X:\t\tdon't update the library links\n"
754             "\t-l:\t\tlibrary mode, manually link libraries\n"
755             "\t-p:\t\tprint the current library cache\n"
756             "\t-f conf :\tuse conf instead of %s\n"
757             "\t-C cache:\tuse cache instead of %s\n"
758             "\t-r root :\tfirst, do a chroot to the indicated directory\n"
759             "\tdir ... :\tdirectories to process\n"
760 #ifdef __LDSO_CACHE_SUPPORT__
761             "\tlib ... :\tlibraries to link\n\n",
762             LDSO_CONF, LDSO_CACHE
763 #else
764             "\tlib ... :\tlibraries to link\n\n"
765 #endif
766            );
767     exit(EXIT_FATAL);
768 }
769
770 #define DIR_SEP      ":, \t\n"
771 int main(int argc, char **argv)
772 {
773     int i, c;
774     int nodefault = 0;
775     char *cp, *dir, *so;
776     int libtype, islink;
777     char *chroot_dir = NULL;
778     int printcache = 0;
779     char *extpath;
780
781     prog = argv[0];
782     opterr = 0;
783
784     while ((c = getopt(argc, argv, "DvqnNXlpf:C:r:")) != EOF)
785         switch (c)
786         {
787             case 'D':
788                 debug = 1;              /* debug mode */
789                 nocache = 1;
790                 nolinks = 1;
791                 verbose = 1;
792                 break;
793             case 'v':
794                 verbose = 1;    /* verbose mode */
795                 break;
796             case 'q':
797                 if (verbose <= 0)
798                     verbose = -1;       /* quiet mode */
799                 break;
800             case 'n':
801                 nodefault = 1;  /* no default dirs */
802                 nocache = 1;
803                 break;
804             case 'N':
805                 nocache = 1;    /* don't build cache */
806                 break;
807             case 'X':
808                 nolinks = 1;    /* don't update links */
809                 break;
810             case 'l':
811                 libmode = 1;    /* library mode */
812                 break;
813             case 'p':
814                 printcache = 1; /* print cache */
815                 break;
816             case 'f':
817 #ifdef __LDSO_CACHE_SUPPORT__
818                 conffile = optarg;      /* alternate conf file */
819 #endif
820                 break;
821             case 'C':
822 #ifdef __LDSO_CACHE_SUPPORT__
823                 cachefile = optarg;     /* alternate cache file */
824 #endif
825                 break;
826             case 'r':
827                 chroot_dir = optarg;
828                 break;
829             default:
830                 usage();
831                 break;
832
833                 /* THE REST OF THESE ARE UNDOCUMENTED AND MAY BE REMOVED
834                    IN FUTURE VERSIONS. */
835         }
836
837     if (chroot_dir && *chroot_dir) {
838         if (chroot(chroot_dir) < 0)
839             err(EXIT_FATAL,"couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
840         if (chdir("/") < 0)
841             err(EXIT_FATAL,"couldn't chdir to / (%s)", strerror(errno));
842     }
843
844     /* allow me to introduce myself, hi, my name is ... */
845     if (verbose > 0)
846         printf("%s: uClibc version\n", argv[0]);
847
848     if (printcache)
849     {
850         /* print the cache -- don't you trust me? */
851         cache_print();
852         exit(EXIT_OK);
853     }
854     else if (libmode)
855     {
856         /* so you want to do things manually, eh? */
857
858         /* ok, if you're so smart, which libraries do we link? */
859         for (i = optind; i < argc; i++)
860         {
861             /* split into directory and file parts */
862             if (!(cp = strrchr(argv[i], '/')))
863             {
864                 dir = ".";      /* no dir, only a filename */
865                 cp = argv[i];
866             }
867             else
868             {
869                 if (cp == argv[i])
870                     dir = "/";  /* file in root directory */
871                 else
872                     dir = argv[i];
873                 *cp++ = '\0';   /* neither of the above */
874             }
875
876             /* we'd better do a little bit of checking */
877             if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
878                 err(EXIT_FATAL,"%s%s%s is not a shared library", dir,
879                         (*dir && strcmp(dir, "/")) ? "/" : "", cp);
880
881             /* so far, so good, maybe he knows what he's doing */
882             link_shlib(dir, cp, so);
883         }
884     }
885     else
886     {
887         /* the lazy bum want's us to do all the work for him */
888
889         /* don't cache dirs on the command line */
890         int nocache_save = nocache;
891         nocache = 1;
892
893         /* OK, which directories should we do? */
894         for (i = optind; i < argc; i++)
895             scan_dir(argv[i]);
896
897         /* restore the desired caching state */
898         nocache = nocache_save;
899
900         /* look ma, no defaults */
901         if (!nodefault)
902         {
903             scan_dir(UCLIBC_RUNTIME_PREFIX "lib");
904             scan_dir(UCLIBC_RUNTIME_PREFIX "usr/lib");
905 #ifndef __LDSO_CACHE_SUPPORT__
906             scan_dir(UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib");
907 #else
908             /* I guess the defaults aren't good enough */
909             if ((extpath = get_extpath()))
910             {
911                 for (cp = strtok(extpath, DIR_SEP); cp; cp = strtok(NULL, DIR_SEP)) {
912                         /* strip trailing slashes */
913                         int len = strlen(cp);
914                         if (len) 
915                                 while (cp[--len] == '/' && len)
916                                         cp[len] = 0;
917                         /* we do the redundancy check only if cache usage is enabled */
918                         if (strcmp(UCLIBC_RUNTIME_PREFIX "lib", cp) == 0 ||
919                             strcmp(UCLIBC_RUNTIME_PREFIX "usr/lib", cp) == 0) {
920                                 if (verbose >= 0)
921                                         warnx("Remove `%s' from `%s'\n", cp, LDSO_CONF);
922                                 continue;
923                         }
924                     scan_dir(cp);
925                 }
926                 free(extpath);
927             }
928 #endif
929         }
930
931         if (!nocache)
932             cache_write();
933     }
934
935     exit(EXIT_OK);
936 }
937