xinstall.c revision 1.6 1 /* $NetBSD: xinstall.c,v 1.6 1994/12/18 22:05:40 jtc 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 #ifndef lint
37 static char copyright[] =
38 "@(#) 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 #endif
46 static char rcsid[] = "$NetBSD: xinstall.c,v 1.6 1994/12/18 22:05:40 jtc Exp $";
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/wait.h>
51 #include <sys/mman.h>
52 #include <sys/stat.h>
53
54 #include <ctype.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <grp.h>
58 #include <paths.h>
59 #include <pwd.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <err.h>
65
66 #include "pathnames.h"
67
68 struct passwd *pp;
69 struct group *gp;
70 int docopy, dodir, dostrip;
71 int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
72 char *group, *owner, pathbuf[MAXPATHLEN];
73
74 #define DIRECTORY 0x01 /* Tell install it's a directory. */
75 #define SETFLAGS 0x02 /* Tell install to set flags. */
76
77 void copy __P((int, char *, int, char *, off_t));
78 void install __P((char *, char *, u_long, u_int));
79 u_long string_to_flags __P((char **, u_long *, u_long *));
80 void strip __P((char *));
81 void usage __P((void));
82
83 int
84 main(argc, argv)
85 int argc;
86 char *argv[];
87 {
88 struct stat from_sb, to_sb;
89 mode_t *set;
90 u_long fset;
91 u_int iflags;
92 int ch, no_target;
93 char *flags, *to_name;
94
95 iflags = 0;
96 while ((ch = getopt(argc, argv, "cf:g:m:o:sd")) != EOF)
97 switch((char)ch) {
98 case 'c':
99 docopy = 1;
100 break;
101 case 'f':
102 flags = optarg;
103 if (string_to_flags(&flags, &fset, NULL))
104 errx(1, "%s: invalid flag", flags);
105 iflags |= SETFLAGS;
106 break;
107 case 'g':
108 group = optarg;
109 break;
110 case 'm':
111 if (!(set = setmode(optarg)))
112 errx(1, "%s: invalid file mode", optarg);
113 mode = getmode(set, 0);
114 break;
115 case 'o':
116 owner = optarg;
117 break;
118 case 's':
119 dostrip = 1;
120 break;
121 case 'd':
122 dodir = 1;
123 break;
124 case '?':
125 default:
126 usage();
127 }
128 argc -= optind;
129 argv += optind;
130
131 /* copy and strip options make no sense when creating directories */
132 if ((docopy || dostrip) && dodir)
133 usage();
134
135 /* must have at least two arguments, except when creating directories */
136 if (argc < 2 && !dodir)
137 usage();
138
139 /* get group and owner id's */
140 if (group && !(gp = getgrnam(group)))
141 errx(1, "unknown group %s", group);
142 if (owner && !(pp = getpwnam(owner)))
143 errx(1, "unknown user %s", owner);
144
145 if (dodir) {
146 for (; *argv != NULL; ++argv)
147 install_dir(*argv);
148 exit (0);
149 /* NOTREACHED */
150 }
151
152 no_target = stat(to_name = argv[argc - 1], &to_sb);
153 if (!no_target && S_ISDIR(to_sb.st_mode)) {
154 for (; *argv != to_name; ++argv)
155 install(*argv, to_name, fset, iflags | DIRECTORY);
156 exit(0);
157 }
158
159 /* can't do file1 file2 directory/file */
160 if (argc != 2)
161 usage();
162
163 if (!no_target) {
164 if (stat(*argv, &from_sb))
165 err(1, "%s", *argv);
166 if (!S_ISREG(to_sb.st_mode))
167 errx(1, "%s: %s", to_name, strerror(EFTYPE));
168 if (to_sb.st_dev == from_sb.st_dev &&
169 to_sb.st_ino == from_sb.st_ino)
170 errx(1, "%s and %s are the same file", *argv, to_name);
171 /*
172 * Unlink now... avoid ETXTBSY errors later. Try and turn
173 * off the append/immutable bits -- if we fail, go ahead,
174 * it might work.
175 */
176 #define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND)
177 if (to_sb.st_flags & NOCHANGEBITS)
178 (void)chflags(to_name,
179 to_sb.st_flags & ~(NOCHANGEBITS));
180 (void)unlink(to_name);
181 }
182 install(*argv, to_name, fset, iflags);
183 exit(0);
184 }
185
186 /*
187 * install --
188 * build a path name and install the file
189 */
190 void
191 install(from_name, to_name, fset, flags)
192 char *from_name, *to_name;
193 u_long fset;
194 u_int flags;
195 {
196 struct stat from_sb, to_sb;
197 int devnull, from_fd, to_fd, serrno;
198 char *p;
199
200 /* If try to install NULL file to a directory, fails. */
201 if (flags & DIRECTORY || strcmp(from_name, _PATH_DEVNULL)) {
202 if (stat(from_name, &from_sb))
203 err(1, "%s", from_name);
204 if (!S_ISREG(from_sb.st_mode))
205 errx(1, "%s: %s", from_name, strerror(EFTYPE));
206 /* Build the target path. */
207 if (flags & DIRECTORY) {
208 (void)snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
209 to_name,
210 (p = rindex(from_name, '/')) ? ++p : from_name);
211 to_name = pathbuf;
212 }
213 devnull = 0;
214 } else {
215 from_sb.st_flags = 0; /* XXX */
216 devnull = 1;
217 }
218
219 /*
220 * Unlink now... avoid ETXTBSY errors later. Try and turn
221 * off the append/immutable bits -- if we fail, go ahead,
222 * it might work.
223 */
224 if (stat(to_name, &to_sb) == 0 &&
225 to_sb.st_flags & (NOCHANGEBITS))
226 (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS));
227 (void)unlink(to_name);
228
229 /* Create target. */
230 if ((to_fd = open(to_name,
231 O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
232 err(1, "%s", to_name);
233 if (!devnull) {
234 if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
235 (void)unlink(to_name);
236 err(1, "%s", from_name);
237 }
238 copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
239 (void)close(from_fd);
240 }
241 if (dostrip)
242 strip(to_name);
243 /*
244 * Set owner, group, mode for target; do the chown first,
245 * chown may lose the setuid bits.
246 */
247 if ((group || owner) &&
248 fchown(to_fd, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1)) {
249 serrno = errno;
250 (void)unlink(to_name);
251 errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
252 }
253 if (fchmod(to_fd, mode)) {
254 serrno = errno;
255 (void)unlink(to_name);
256 errx(1, "%s: chmod: %s", to_name, strerror(serrno));
257 }
258
259 /*
260 * If provided a set of flags, set them, otherwise, preserve the
261 * flags, except for the dump flag.
262 */
263 if (fchflags(to_fd,
264 flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) {
265 serrno = errno;
266 (void)unlink(to_name);
267 errx(1, "%s: chflags: %s", to_name, strerror(serrno));
268 }
269
270 (void)close(to_fd);
271 if (!docopy && !devnull && unlink(from_name))
272 err(1, "%s", from_name);
273 }
274
275 /*
276 * copy --
277 * copy from one file to another
278 */
279 void
280 copy(from_fd, from_name, to_fd, to_name, size)
281 register int from_fd, to_fd;
282 char *from_name, *to_name;
283 off_t size;
284 {
285 register int nr, nw;
286 int serrno;
287 char *p, buf[MAXBSIZE];
288
289 /*
290 * Mmap and write if less than 8M (the limit is so we don't totally
291 * trash memory on big files. This is really a minor hack, but it
292 * wins some CPU back.
293 */
294 if (size <= 8 * 1048576) {
295 if ((p = mmap(NULL, (size_t)size, PROT_READ,
296 0, from_fd, (off_t)0)) == (char *)-1)
297 err(1, "%s", from_name);
298 if (write(to_fd, p, size) != size)
299 err(1, "%s", to_name);
300 } else {
301 while ((nr = read(from_fd, buf, sizeof(buf))) > 0)
302 if ((nw = write(to_fd, buf, nr)) != nr) {
303 serrno = errno;
304 (void)unlink(to_name);
305 errx(1, "%s: %s",
306 to_name, strerror(nw > 0 ? EIO : serrno));
307 }
308 if (nr != 0) {
309 serrno = errno;
310 (void)unlink(to_name);
311 errx(1, "%s: %s", from_name, strerror(serrno));
312 }
313 }
314 }
315
316 /*
317 * strip --
318 * use strip(1) to strip the target file
319 */
320 void
321 strip(to_name)
322 char *to_name;
323 {
324 int serrno, status;
325
326 switch (vfork()) {
327 case -1:
328 serrno = errno;
329 (void)unlink(to_name);
330 errx(1, "forks: %s", strerror(errno));
331 case 0:
332 execl(_PATH_STRIP, "strip", to_name, NULL);
333 err(1, "%s", _PATH_STRIP);
334 default:
335 if (wait(&status) == -1 || status)
336 (void)unlink(to_name);
337 }
338 }
339
340 /*
341 * install_dir --
342 * build directory heirarchy
343 */
344 int
345 install_dir(path)
346 char *path;
347 {
348 register char *p;
349 struct stat sb;
350 int ch;
351
352 for (p = path;; ++p)
353 if (!*p || (p != path && *p == '/')) {
354 ch = *p;
355 *p = '\0';
356 if (stat(path, &sb)) {
357 if (errno != ENOENT || mkdir(path, 0777) < 0) {
358 warn("%s", path);
359 return(1);
360 }
361 }
362 if (!(*p = ch))
363 break;
364 }
365
366 if ((group || owner) &&
367 chown(path, owner ? pp->pw_uid : -1, group ? gp->gr_gid : -1) ||
368 chmod(path, mode)) {
369 warn("%s", path);
370 }
371
372 return(0);
373 }
374
375 /*
376 * usage --
377 * print a usage message and die
378 */
379 void
380 usage()
381 {
382 (void)fprintf(stderr, "\
383 usage: install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 file2\n\
384 install [-cs] [-f flags] [-g group] [-m mode] [-o owner] file1 ... fileN directory\n\
385 install -d [-g group] [-m mode] [-o owner] directory ...\n");
386 exit(1);
387 }
388