]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - usr.bin/du/du.c
Fix Denial of Service vulnerability in named(8) with DNS64. [13:01]
[FreeBSD/releng/9.1.git] / usr.bin / du / du.c
1 /*
2  * Copyright (c) 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Chris Newcomb.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993, 1994\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static const char sccsid[] = "@(#)du.c  8.5 (Berkeley) 5/4/95";
42 #endif
43 #endif /* not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/queue.h>
49 #include <sys/stat.h>
50
51 #include <err.h>
52 #include <errno.h>
53 #include <fnmatch.h>
54 #include <fts.h>
55 #include <libutil.h>
56 #include <locale.h>
57 #include <stdint.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sysexits.h>
62 #include <unistd.h>
63
64 SLIST_HEAD(ignhead, ignentry) ignores;
65 struct ignentry {
66         char                    *mask;
67         SLIST_ENTRY(ignentry)   next;
68 };
69
70 static int      linkchk(FTSENT *);
71 static void     usage(void);
72 static void     prthumanval(int64_t);
73 static void     ignoreadd(const char *);
74 static void     ignoreclean(void);
75 static int      ignorep(FTSENT *);
76 static void     siginfo(int __unused);
77
78 static int      nodumpflag = 0;
79 static int      Aflag;
80 static long     blocksize, cblocksize;
81 static volatile sig_atomic_t info;
82
83 int
84 main(int argc, char *argv[])
85 {
86         FTS             *fts;
87         FTSENT          *p;
88         off_t           savednumber, curblocks;
89         off_t           threshold, threshold_sign;
90         int             ftsoptions;
91         int             listall;
92         int             depth;
93         int             Hflag, Lflag, aflag, sflag, dflag, cflag;
94         int             hflag, lflag, ch, notused, rval;
95         char            **save;
96         static char     dot[] = ".";
97
98         setlocale(LC_ALL, "");
99
100         Hflag = Lflag = aflag = sflag = dflag = cflag = hflag =
101             lflag = Aflag = 0;
102
103         save = argv;
104         ftsoptions = FTS_PHYSICAL;
105         savednumber = 0;
106         threshold = 0;
107         threshold_sign = 1;
108         cblocksize = DEV_BSIZE;
109         blocksize = 0;
110         depth = INT_MAX;
111         SLIST_INIT(&ignores);
112
113         while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrt:x")) != -1)
114                 switch (ch) {
115                 case 'A':
116                         Aflag = 1;
117                         break;
118                 case 'B':
119                         errno = 0;
120                         cblocksize = atoi(optarg);
121                         if (errno == ERANGE || cblocksize <= 0) {
122                                 warnx("invalid argument to option B: %s",
123                                     optarg);
124                                 usage();
125                         }
126                         break;
127                 case 'H':
128                         Hflag = 1;
129                         Lflag = 0;
130                         break;
131                 case 'I':
132                         ignoreadd(optarg);
133                         break;
134                 case 'L':
135                         Lflag = 1;
136                         Hflag = 0;
137                         break;
138                 case 'P':
139                         Hflag = Lflag = 0;
140                         break;
141                 case 'a':
142                         aflag = 1;
143                         break;
144                 case 's':
145                         sflag = 1;
146                         break;
147                 case 'd':
148                         dflag = 1;
149                         errno = 0;
150                         depth = atoi(optarg);
151                         if (errno == ERANGE || depth < 0) {
152                                 warnx("invalid argument to option d: %s",
153                                     optarg);
154                                 usage();
155                         }
156                         break;
157                 case 'c':
158                         cflag = 1;
159                         break;
160                 case 'h':
161                         hflag = 1;
162                         break;
163                 case 'k':
164                         hflag = 0;
165                         blocksize = 1024;
166                         break;
167                 case 'l':
168                         lflag = 1;
169                         break;
170                 case 'm':
171                         hflag = 0;
172                         blocksize = 1048576;
173                         break;
174                 case 'n':
175                         nodumpflag = 1;
176                         break;
177                 case 'r':                /* Compatibility. */
178                         break;
179                 case 't' :
180                         if (expand_number(optarg, &threshold) != 0 ||
181                             threshold == 0) {
182                                 warnx("invalid threshold: %s", optarg);
183                                 usage();
184                         } else if (threshold < 0)
185                                 threshold_sign = -1;
186                         break;
187                 case 'x':
188                         ftsoptions |= FTS_XDEV;
189                         break;
190                 case '?':
191                 default:
192                         usage();
193                         /* NOTREACHED */
194                 }
195
196         argc -= optind;
197         argv += optind;
198
199         /*
200          * XXX
201          * Because of the way that fts(3) works, logical walks will not count
202          * the blocks actually used by symbolic links.  We rationalize this by
203          * noting that users computing logical sizes are likely to do logical
204          * copies, so not counting the links is correct.  The real reason is
205          * that we'd have to re-implement the kernel's symbolic link traversing
206          * algorithm to get this right.  If, for example, you have relative
207          * symbolic links referencing other relative symbolic links, it gets
208          * very nasty, very fast.  The bottom line is that it's documented in
209          * the man page, so it's a feature.
210          */
211
212         if (Hflag)
213                 ftsoptions |= FTS_COMFOLLOW;
214         if (Lflag) {
215                 ftsoptions &= ~FTS_PHYSICAL;
216                 ftsoptions |= FTS_LOGICAL;
217         }
218
219         if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
220                 cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
221
222         listall = 0;
223
224         if (aflag) {
225                 if (sflag || dflag)
226                         usage();
227                 listall = 1;
228         } else if (sflag) {
229                 if (dflag)
230                         usage();
231                 depth = 0;
232         }
233
234         if (!*argv) {
235                 argv = save;
236                 argv[0] = dot;
237                 argv[1] = NULL;
238         }
239
240         if (blocksize == 0)
241                 (void)getbsize(&notused, &blocksize);
242
243         if (!Aflag) {
244                 cblocksize /= DEV_BSIZE;
245                 blocksize /= DEV_BSIZE;
246         }
247
248         if (threshold != 0)
249                 threshold = howmany(threshold / DEV_BSIZE * cblocksize,
250                     blocksize);
251
252         rval = 0;
253
254         (void)signal(SIGINFO, siginfo);
255
256         if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
257                 err(1, "fts_open");
258
259         while ((p = fts_read(fts)) != NULL) {
260                 switch (p->fts_info) {
261                 case FTS_D:                     /* Ignore. */
262                         if (ignorep(p))
263                                 fts_set(fts, p, FTS_SKIP);
264                         break;
265                 case FTS_DP:
266                         if (ignorep(p))
267                                 break;
268
269                         curblocks = Aflag ?
270                             howmany(p->fts_statp->st_size, cblocksize) :
271                             howmany(p->fts_statp->st_blocks, cblocksize);
272                         p->fts_parent->fts_bignum += p->fts_bignum +=
273                             curblocks;
274
275                         if (p->fts_level <= depth && threshold <=
276                             threshold_sign * howmany(p->fts_bignum *
277                             cblocksize, blocksize)) {
278                                 if (hflag) {
279                                         prthumanval(p->fts_bignum);
280                                         (void)printf("\t%s\n", p->fts_path);
281                                 } else {
282                                         (void)printf("%jd\t%s\n",
283                                             (intmax_t)howmany(p->fts_bignum *
284                                             cblocksize, blocksize),
285                                             p->fts_path);
286                                 }
287                         }
288                         if (info) {
289                                 info = 0;
290                                 (void)printf("\t%s\n", p->fts_path);
291                         }
292                         break;
293                 case FTS_DC:                    /* Ignore. */
294                         break;
295                 case FTS_DNR:                   /* Warn, continue. */
296                 case FTS_ERR:
297                 case FTS_NS:
298                         warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
299                         rval = 1;
300                         break;
301                 default:
302                         if (ignorep(p))
303                                 break;
304
305                         if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
306                             linkchk(p))
307                                 break;
308
309                         curblocks = Aflag ?
310                             howmany(p->fts_statp->st_size, cblocksize) :
311                             howmany(p->fts_statp->st_blocks, cblocksize);
312
313                         if (listall || p->fts_level == 0) {
314                                 if (hflag) {
315                                         prthumanval(curblocks);
316                                         (void)printf("\t%s\n", p->fts_path);
317                                 } else {
318                                         (void)printf("%jd\t%s\n",
319                                             (intmax_t)howmany(curblocks *
320                                             cblocksize, blocksize),
321                                             p->fts_path);
322                                 }
323                         }
324
325                         p->fts_parent->fts_bignum += curblocks;
326                 }
327                 savednumber = p->fts_parent->fts_bignum;
328         }
329
330         if (errno)
331                 err(1, "fts_read");
332
333         if (cflag) {
334                 if (hflag) {
335                         prthumanval(savednumber);
336                         (void)printf("\ttotal\n");
337                 } else {
338                         (void)printf("%jd\ttotal\n", (intmax_t)howmany(
339                             savednumber * cblocksize, blocksize));
340                 }
341         }
342
343         ignoreclean();
344         exit(rval);
345 }
346
347 static int
348 linkchk(FTSENT *p)
349 {
350         struct links_entry {
351                 struct links_entry *next;
352                 struct links_entry *previous;
353                 int      links;
354                 dev_t    dev;
355                 ino_t    ino;
356         };
357         static const size_t links_hash_initial_size = 8192;
358         static struct links_entry **buckets;
359         static struct links_entry *free_list;
360         static size_t number_buckets;
361         static unsigned long number_entries;
362         static char stop_allocating;
363         struct links_entry *le, **new_buckets;
364         struct stat *st;
365         size_t i, new_size;
366         int hash;
367
368         st = p->fts_statp;
369
370         /* If necessary, initialize the hash table. */
371         if (buckets == NULL) {
372                 number_buckets = links_hash_initial_size;
373                 buckets = malloc(number_buckets * sizeof(buckets[0]));
374                 if (buckets == NULL)
375                         errx(1, "No memory for hardlink detection");
376                 for (i = 0; i < number_buckets; i++)
377                         buckets[i] = NULL;
378         }
379
380         /* If the hash table is getting too full, enlarge it. */
381         if (number_entries > number_buckets * 10 && !stop_allocating) {
382                 new_size = number_buckets * 2;
383                 new_buckets = malloc(new_size * sizeof(struct links_entry *));
384
385                 /* Try releasing the free list to see if that helps. */
386                 if (new_buckets == NULL && free_list != NULL) {
387                         while (free_list != NULL) {
388                                 le = free_list;
389                                 free_list = le->next;
390                                 free(le);
391                         }
392                         new_buckets = malloc(new_size *
393                             sizeof(new_buckets[0]));
394                 }
395
396                 if (new_buckets == NULL) {
397                         stop_allocating = 1;
398                         warnx("No more memory for tracking hard links");
399                 } else {
400                         memset(new_buckets, 0,
401                             new_size * sizeof(struct links_entry *));
402                         for (i = 0; i < number_buckets; i++) {
403                                 while (buckets[i] != NULL) {
404                                         /* Remove entry from old bucket. */
405                                         le = buckets[i];
406                                         buckets[i] = le->next;
407
408                                         /* Add entry to new bucket. */
409                                         hash = (le->dev ^ le->ino) % new_size;
410
411                                         if (new_buckets[hash] != NULL)
412                                                 new_buckets[hash]->previous =
413                                                     le;
414                                         le->next = new_buckets[hash];
415                                         le->previous = NULL;
416                                         new_buckets[hash] = le;
417                                 }
418                         }
419                         free(buckets);
420                         buckets = new_buckets;
421                         number_buckets = new_size;
422                 }
423         }
424
425         /* Try to locate this entry in the hash table. */
426         hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
427         for (le = buckets[hash]; le != NULL; le = le->next) {
428                 if (le->dev == st->st_dev && le->ino == st->st_ino) {
429                         /*
430                          * Save memory by releasing an entry when we've seen
431                          * all of it's links.
432                          */
433                         if (--le->links <= 0) {
434                                 if (le->previous != NULL)
435                                         le->previous->next = le->next;
436                                 if (le->next != NULL)
437                                         le->next->previous = le->previous;
438                                 if (buckets[hash] == le)
439                                         buckets[hash] = le->next;
440                                 number_entries--;
441                                 /* Recycle this node through the free list */
442                                 if (stop_allocating) {
443                                         free(le);
444                                 } else {
445                                         le->next = free_list;
446                                         free_list = le;
447                                 }
448                         }
449                         return (1);
450                 }
451         }
452
453         if (stop_allocating)
454                 return (0);
455
456         /* Add this entry to the links cache. */
457         if (free_list != NULL) {
458                 /* Pull a node from the free list if we can. */
459                 le = free_list;
460                 free_list = le->next;
461         } else
462                 /* Malloc one if we have to. */
463                 le = malloc(sizeof(struct links_entry));
464         if (le == NULL) {
465                 stop_allocating = 1;
466                 warnx("No more memory for tracking hard links");
467                 return (0);
468         }
469         le->dev = st->st_dev;
470         le->ino = st->st_ino;
471         le->links = st->st_nlink - 1;
472         number_entries++;
473         le->next = buckets[hash];
474         le->previous = NULL;
475         if (buckets[hash] != NULL)
476                 buckets[hash]->previous = le;
477         buckets[hash] = le;
478         return (0);
479 }
480
481 static void
482 prthumanval(int64_t bytes)
483 {
484         char buf[5];
485
486         bytes *= cblocksize;
487         if (!Aflag)
488                 bytes *= DEV_BSIZE;
489
490         humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
491             HN_B | HN_NOSPACE | HN_DECIMAL);
492
493         (void)printf("%4s", buf);
494 }
495
496 static void
497 usage(void)
498 {
499         (void)fprintf(stderr,
500                 "usage: du [-Aclnx] [-H | -L | -P] [-h | -k | -m ] "
501                 "[-a | -s | -d depth] [-B blocksize] [-I mask] "
502                 "[-t threshold] [file ...]\n");
503         exit(EX_USAGE);
504 }
505
506 static void
507 ignoreadd(const char *mask)
508 {
509         struct ignentry *ign;
510
511         ign = calloc(1, sizeof(*ign));
512         if (ign == NULL)
513                 errx(1, "cannot allocate memory");
514         ign->mask = strdup(mask);
515         if (ign->mask == NULL)
516                 errx(1, "cannot allocate memory");
517         SLIST_INSERT_HEAD(&ignores, ign, next);
518 }
519
520 static void
521 ignoreclean(void)
522 {
523         struct ignentry *ign;
524         
525         while (!SLIST_EMPTY(&ignores)) {
526                 ign = SLIST_FIRST(&ignores);
527                 SLIST_REMOVE_HEAD(&ignores, next);
528                 free(ign->mask);
529                 free(ign);
530         }
531 }
532
533 static int
534 ignorep(FTSENT *ent)
535 {
536         struct ignentry *ign;
537
538         if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
539                 return 1;
540         SLIST_FOREACH(ign, &ignores, next)
541                 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
542                         return 1;
543         return 0;
544 }
545
546 static void
547 siginfo(int sig __unused)
548 {
549
550         info = 1;
551 }