xinstall.c revision 1.37 1 /* $NetBSD: xinstall.c,v 1.37 1999/07/06 14:45:31 christos Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
45 #else
46 __RCSID("$NetBSD: xinstall.c,v 1.37 1999/07/06 14:45:31 christos Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/wait.h>
52 #include <sys/mman.h>
53 #include <sys/stat.h>
54
55 #include <ctype.h>
56 #include <errno.h>
57 #include <fcntl.h>
58 #include <grp.h>
59 #include <paths.h>
60 #include <pwd.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <err.h>
66
67 #include "pathnames.h"
68 #include "stat_flags.h"
69
70 #define STRIP_ARGS_MAX 32
71 #define BACKUP_SUFFIX ".old"
72
73 struct passwd *pp;
74 struct group *gp;
75 int dobackup=0, docopy=0, dodir=0, dostrip=0, dolink=0, dopreserve=0,
76 dorename = 0;
77 static int numberedbackup = 0;
78 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
79 char pathbuf[MAXPATHLEN];
80 uid_t uid;
81 gid_t gid;
82 char *stripArgs=NULL;
83 char *suffix=BACKUP_SUFFIX;
84
85 #define LN_ABSOLUTE 0x01
86 #define LN_RELATIVE 0x02
87 #define LN_HARD 0x04
88 #define LN_SYMBOLIC 0x08
89 #define LN_MIXED 0x10
90
91 #define DIRECTORY 0x01 /* Tell install it's a directory. */
92 #define SETFLAGS 0x02 /* Tell install to set flags. */
93
94 void copy __P((int, char *, int, char *, off_t));
95 void makelink __P((char *, char *));
96 void install __P((char *, char *, u_long, u_int));
97 void install_dir __P((char *));
98 void strip __P((char *));
99 void backup __P((const char *));
100 void usage __P((void));
101 int main __P((int, char *[]));
102
103 int
104 main(argc, argv)
105 int argc;
106 char *argv[];
107 {
108 struct stat from_sb, to_sb;
109 mode_t *set;
110 u_long fset;
111 u_int iflags;
112 int ch, no_target;
113 char *p;
114 char *flags = NULL, *to_name, *group = NULL, *owner = NULL;
115
116 iflags = 0;
117 while ((ch = getopt(argc, argv, "cbB:df:g:l:m:o:prsS:")) != -1)
118 switch((char)ch) {
119 case 'B':
120 suffix = optarg;
121 numberedbackup = 0;
122 {
123 /* Check if given suffix really generates
124 different suffixes - catch e.g. ".%" */
125 char suffix_expanded0[FILENAME_MAX],
126 suffix_expanded1[FILENAME_MAX];
127 (void)snprintf(suffix_expanded0, FILENAME_MAX,
128 suffix, 0);
129 (void)snprintf(suffix_expanded1, FILENAME_MAX,
130 suffix, 1);
131 if (strcmp(suffix_expanded0, suffix_expanded1)
132 != 0)
133 numberedbackup = 1;
134 }
135 /* fall through; -B implies -b */
136 case 'b':
137 dobackup = 1;
138 break;
139 case 'c':
140 docopy = 1;
141 break;
142 case 'd':
143 dodir = 1;
144 break;
145 case 'f':
146 flags = optarg;
147 if (string_to_flags(&flags, &fset, NULL))
148 errx(1, "%s: invalid flag", flags);
149 iflags |= SETFLAGS;
150 break;
151 case 'g':
152 group = optarg;
153 break;
154 case 'l':
155 for (p = optarg; *p; p++)
156 switch (*p) {
157 case 's':
158 dolink &= ~(LN_HARD|LN_MIXED);
159 dolink |= LN_SYMBOLIC;
160 break;
161 case 'h':
162 dolink &= ~(LN_SYMBOLIC|LN_MIXED);
163 dolink |= LN_HARD;
164 break;
165 case 'm':
166 dolink &= ~(LN_SYMBOLIC|LN_HARD);
167 dolink |= LN_MIXED;
168 break;
169 case 'a':
170 dolink &= ~LN_RELATIVE;
171 dolink |= LN_ABSOLUTE;
172 break;
173 case 'r':
174 dolink &= ~LN_ABSOLUTE;
175 dolink |= LN_RELATIVE;
176 break;
177 default:
178 errx(1, "%c: invalid link type", *p);
179 break;
180 }
181 break;
182 case 'm':
183 if (!(set = setmode(optarg)))
184 errx(1, "%s: invalid file mode", optarg);
185 mode = getmode(set, 0);
186 break;
187 case 'o':
188 owner = optarg;
189 break;
190 case 'p':
191 dopreserve = 1;
192 break;
193 case 'r':
194 dorename = 1;
195 break;
196 case 'S':
197 stripArgs = (char*)malloc(sizeof(char)*(strlen(optarg)+1));
198 strcpy(stripArgs,optarg);
199 /* fall through; -S implies -s */
200 case 's':
201 dostrip = 1;
202 break;
203 case '?':
204 default:
205 usage();
206 }
207 argc -= optind;
208 argv += optind;
209
210 /* strip and link options make no sense when creating directories */
211 if ((dostrip || dolink) && dodir)
212 usage();
213
214 /* strip and flags make no sense with links */
215 if ((dostrip || flags) && dolink)
216 usage();
217
218 /* must have at least two arguments, except when creating directories */
219 if (argc < 2 && !dodir)
220 usage();
221
222 /* get group and owner id's */
223 if (group && !(gp = getgrnam(group)) && !isdigit((unsigned char)*group))
224 errx(1, "unknown group %s", group);
225 gid = (group) ? ((gp) ? gp->gr_gid : atoi(group)) : -1;
226 if (owner && !(pp = getpwnam(owner)) && !isdigit((unsigned char)*owner))
227 errx(1, "unknown user %s", owner);
228 uid = (owner) ? ((pp) ? pp->pw_uid : atoi(owner)) : -1;
229
230 if (dodir) {
231 for (; *argv != NULL; ++argv)
232 install_dir(*argv);
233 exit (0);
234 }
235
236 no_target = stat(to_name = argv[argc - 1], &to_sb);
237 if (!no_target && S_ISDIR(to_sb.st_mode)) {
238 for (; *argv != to_name; ++argv)
239 install(*argv, to_name, fset, iflags | DIRECTORY);
240 exit(0);
241 }
242
243 /* can't do file1 file2 directory/file */
244 if (argc != 2)
245 usage();
246
247 if (!no_target) {
248 if (stat(*argv, &from_sb))
249 err(1, "%s", *argv);
250 if (!S_ISREG(to_sb.st_mode))
251 errx(1, "%s: %s", to_name, strerror(EFTYPE));
252 if (!dolink && to_sb.st_dev == from_sb.st_dev &&
253 to_sb.st_ino == from_sb.st_ino)
254 errx(1, "%s and %s are the same file", *argv, to_name);
255 /*
256 * Unlink now... avoid ETXTBSY errors later. Try and turn
257 * off the append/immutable bits -- if we fail, go ahead,
258 * it might work.
259 */
260 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
261 if (to_sb.st_flags & NOCHANGEBITS)
262 (void)chflags(to_name,
263 to_sb.st_flags & ~(NOCHANGEBITS));
264 if (dobackup)
265 backup(to_name);
266 else if (!dorename)
267 (void)unlink(to_name);
268 }
269 install(*argv, to_name, fset, iflags);
270 exit(0);
271 }
272
273 /*
274 * makelink --
275 * make a link from source to destination
276 */
277 void
278 makelink(from_name, to_name)
279 char *from_name;
280 char *to_name;
281 {
282 char src[MAXPATHLEN], dst[MAXPATHLEN], lnk[MAXPATHLEN];
283
284 /* Try hard links first */
285 if (dolink & (LN_HARD|LN_MIXED)) {
286 if (link(from_name, to_name) == -1) {
287 if ((dolink & LN_HARD) || errno != EXDEV)
288 err(1, "link %s -> %s", from_name, to_name);
289 }
290 else
291 return;
292 }
293
294 /* Symbolic links */
295 if (dolink & LN_ABSOLUTE) {
296 /* Convert source path to absolute */
297 if (realpath(from_name, src) == NULL)
298 err(1, "%s", src);
299 if (symlink(src, to_name) == -1)
300 err(1, "symlink %s -> %s", src, to_name);
301 return;
302 }
303
304 if (dolink & LN_RELATIVE) {
305 char *s, *d;
306
307 /* Resolve pathnames */
308 if (realpath(from_name, src) == NULL)
309 err(1, "%s", src);
310 if (realpath(to_name, dst) == NULL)
311 err(1, "%s", dst);
312
313 /* trim common path components */
314 for (s = src, d = dst; *s == *d; s++, d++)
315 continue;
316 while (*s != '/')
317 s--, d--;
318
319 /* count the number of directories we need to backtrack */
320 for (++d, lnk[0] = '\0'; *d; d++)
321 if (*d == '/')
322 (void) strcat(lnk, "../");
323
324 (void) strcat(lnk, ++s);
325
326 if (symlink(lnk, dst) == -1)
327 err(1, "symlink %s -> %s", lnk, dst);
328 return;
329 }
330
331 /*
332 * If absolute or relative was not specified,
333 * try the names the user provided
334 */
335 if (symlink(from_name, to_name) == -1)
336 err(1, "symlink %s -> %s", from_name, to_name);
337
338 }
339
340 /*
341 * install --
342 * build a path name and install the file
343 */
344 void
345 install(from_name, to_name, fset, flags)
346 char *from_name, *to_name;
347 u_long fset;
348 u_int flags;
349 {
350 struct stat from_sb, to_sb;
351 int devnull, from_fd, to_fd, serrno;
352 char *p, tmpl[MAXPATHLEN], *oto_name;
353
354 /* If try to install NULL file to a directory, fails. */
355 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
356 if (stat(from_name, &from_sb))
357 err(1, "%s", from_name);
358 if (!S_ISREG(from_sb.st_mode))
359 errx(1, "%s: %s", from_name, strerror(EFTYPE));
360 /* Build the target path. */
361 if (flags & DIRECTORY) {
362 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
363 to_name,
364 (p = strrchr(from_name, '/')) ? ++p : from_name);
365 to_name = pathbuf;
366 }
367 devnull = 0;
368 } else {
369 from_sb.st_flags = 0; /* XXX */
370 devnull = 1;
371 }
372
373 /*
374 * Unlink now... avoid ETXTBSY errors later. Try and turn
375 * off the append/immutable bits -- if we fail, go ahead,
376 * it might work.
377 */
378 if (stat(to_name, &to_sb) == 0 &&
379 to_sb.st_flags & (NOCHANGEBITS))
380 (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
381 if (dorename) {
382 char *ptr, c, *dir;
383
384 if ((ptr = strrchr(to_name, '/')) != NULL) {
385 c = *++ptr;
386 *ptr = '\0';
387 dir = to_name;
388 } else {
389 c = '\0'; /* pacify gcc */
390 dir = tmpl;
391 *dir = '\0';
392 }
393 (void)snprintf(tmpl, sizeof(tmpl), "%sinst.XXXXXX", dir);
394 if (ptr)
395 *ptr = c;
396 oto_name = to_name;
397 to_name = tmpl;
398
399 } else {
400 oto_name = NULL; /* pacify gcc */
401 if (dobackup)
402 backup(to_name);
403 else
404 (void)unlink(to_name);
405 }
406
407 if (dolink) {
408 makelink(from_name, to_name);
409 return;
410 }
411
412 /* Create target. */
413 if (dorename) {
414 if ((to_fd = mkstemp(to_name)) == -1)
415 err(1, "%s", to_name);
416 } else {
417 if ((to_fd = open(to_name,
418 O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
419 err(1, "%s", to_name);
420 }
421 if (!devnull) {
422 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
423 (void)unlink(to_name);
424 err(1, "%s", from_name);
425 }
426 copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
427 (void)close(from_fd);
428 }
429
430 if (dostrip) {
431 strip(to_name);
432
433 /*
434 * Re-open our fd on the target, in case we used a strip
435 * that does not work in-place -- like gnu binutils strip.
436 */
437 close(to_fd);
438 if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
439 err(1, "stripping %s", to_name);
440 }
441
442 /*
443 * Set owner, group, mode for target; do the chown first,
444 * chown may lose the setuid bits.
445 */
446 if ((gid != -1 || uid != -1) && fchown(to_fd, uid, gid)) {
447 serrno = errno;
448 (void)unlink(to_name);
449 errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
450 }
451 if (fchmod(to_fd, mode)) {
452 serrno = errno;
453 (void)unlink(to_name);
454 errx(1, "%s: chmod: %s", to_name, strerror(serrno));
455 }
456
457 /*
458 * If provided a set of flags, set them, otherwise, preserve the
459 * flags, except for the dump flag.
460 */
461 if (fchflags(to_fd,
462 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
463 if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0)
464 warn("%s: chflags", to_name);
465 }
466
467 /*
468 * Preserve the date of the source file.
469 */
470 if (dopreserve) {
471 struct timeval tv[2];
472
473 #ifdef BSD4_4
474 TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
475 TIMESPEC_TO_TIMEVAL(&tv[1], &from_sb.st_mtimespec);
476 #else
477 tv[0].tv_sec = from_sb.st_atime;
478 tv[0].tv_usec = 0;
479 tv[1].tv_sec = from_sb.st_mtime;
480 tv[1].tv_usec = 0;
481 #endif
482 if (futimes(to_fd, tv) == -1)
483 warn("%s: futimes", to_name);
484 }
485
486 (void)close(to_fd);
487
488 if (dorename)
489 if (rename(to_name, oto_name) == -1)
490 err(1, "%s: rename", to_name);
491
492 if (!docopy && !devnull && unlink(from_name))
493 err(1, "%s", from_name);
494 }
495
496 /*
497 * copy --
498 * copy from one file to another
499 */
500 void
501 copy(from_fd, from_name, to_fd, to_name, size)
502 int from_fd, to_fd;
503 char *from_name, *to_name;
504 off_t size;
505 {
506 ssize_t nr, nw;
507 int serrno;
508 char *p;
509 char buf[MAXBSIZE];
510
511 /*
512 * There's no reason to do anything other than close the file
513 * now if it's empty, so let's not bother.
514 */
515 if (size > 0) {
516 /*
517 * Mmap and write if less than 8M (the limit is so we don't totally
518 * trash memory on big files). This is really a minor hack, but it
519 * wins some CPU back.
520 */
521 if (size <= 8 * 1048576) {
522 if ((p = mmap(NULL, (size_t)size, PROT_READ,
523 MAP_FILE|MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
524 serrno = errno;
525 (void)unlink(to_name);
526 errx(1, "%s: %s", from_name, strerror(serrno));
527 }
528 if (madvise(p, (size_t)size, MADV_SEQUENTIAL) == -1) {
529 serrno = errno;
530 (void)unlink(to_name);
531 errx(1, "madvise: %s", strerror(serrno));
532 }
533 if (write(to_fd, p, size) != size) {
534 serrno = errno;
535 (void)unlink(to_name);
536 errx(1, "%s: %s",
537 to_name, strerror(serrno));
538 }
539 } else {
540 while ((nr = read(from_fd, buf, sizeof(buf))) > 0) {
541 if ((nw = write(to_fd, buf, nr)) != nr) {
542 serrno = errno;
543 (void)unlink(to_name);
544 errx(1, "%s: %s",
545 to_name, strerror(nw > 0 ? EIO : serrno));
546 }
547 }
548 if (nr != 0) {
549 serrno = errno;
550 (void)unlink(to_name);
551 errx(1, "%s: %s", from_name, strerror(serrno));
552 }
553 }
554 }
555 }
556
557 /*
558 * strip --
559 * use strip(1) to strip the target file
560 */
561 void
562 strip(to_name)
563 char *to_name;
564 {
565 int serrno, status;
566 char *stripprog;
567
568 switch (vfork()) {
569 case -1:
570 serrno = errno;
571 (void)unlink(to_name);
572 errx(1, "vfork: %s", strerror(serrno));
573 case 0:
574 stripprog = getenv("STRIP");
575 if (stripprog == NULL)
576 stripprog = _PATH_STRIP;
577
578 if (stripArgs) {
579 /* build up a command line and let /bin/sh parse the arguments */
580 char* cmd = (char*)malloc(sizeof(char)*
581 (3+strlen(stripprog)+
582 strlen(stripArgs)+
583 strlen(to_name)));
584
585 sprintf(cmd, "%s %s %s", stripprog, stripArgs, to_name);
586
587 execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
588 } else
589 execl(stripprog, "strip", to_name, NULL);
590
591 warn("%s", stripprog);
592 _exit(1);
593 default:
594 if (wait(&status) == -1 || status)
595 (void)unlink(to_name);
596 }
597 }
598
599 /*
600 * backup file "to_name" to to_name.suffix
601 * if suffix contains a "%", it's taken as a printf(3) pattern
602 * used for a numbered backup.
603 */
604 void
605 backup(to_name)
606 const char *to_name;
607 {
608 char backup[FILENAME_MAX];
609
610 if (numberedbackup) {
611 /* Do numbered backup */
612 int cnt;
613 char suffix_expanded[FILENAME_MAX];
614
615 cnt=0;
616 do {
617 (void)snprintf(suffix_expanded, FILENAME_MAX, suffix, cnt);
618 (void)snprintf(backup, FILENAME_MAX, "%s%s",
619 to_name, suffix_expanded);
620 cnt++;
621 } while (access(backup, F_OK)==0);
622 } else {
623 /* Do simple backup */
624 (void)snprintf(backup, FILENAME_MAX, "%s%s", to_name, suffix);
625 }
626
627 (void)rename(to_name, backup);
628 }
629
630 /*
631 * install_dir --
632 * build directory heirarchy
633 */
634 void
635 install_dir(path)
636 char *path;
637 {
638 char *p;
639 struct stat sb;
640 int ch;
641
642 for (p = path;; ++p)
643 if (!*p || (p != path && *p == '/')) {
644 ch = *p;
645 *p = '\0';
646 if (stat(path, &sb)) {
647 if (errno != ENOENT || mkdir(path, 0777) < 0) {
648 err(1, "%s", path);
649 /* NOTREACHED */
650 }
651 }
652 if (!(*p = ch))
653 break;
654 }
655
656 if (((gid != -1 || uid != -1) && chown(path, uid, gid)) ||
657 chmod(path, mode)) {
658 warn("%s", path);
659 }
660 }
661
662 /*
663 * usage --
664 * print a usage message and die
665 */
666 void
667 usage()
668 {
669 (void)fprintf(stderr, "\
670 usage: install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 file2\n\
671 install [-bcps] [-B suffix] [-f flags] [-m mode] [-o owner] [-g group] [-l linkflags] [-S stripflags] file1 ... fileN directory\n\
672 install -pd [-m mode] [-o owner] [-g group] directory ...\n");
673 exit(1);
674 }
675