OSDN Git Service

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