source: trunk/minix/commands/simple/install.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 12.5 KB
Line 
1/* install 1.11 - install files. Author: Kees J. Bot
2 * 21 Feb 1993
3 */
4#define nil 0
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <sys/wait.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <fcntl.h>
12#include <string.h>
13#include <errno.h>
14#include <a.out.h>
15#include <limits.h>
16#include <pwd.h>
17#include <grp.h>
18#include <utime.h>
19#include <signal.h>
20
21/* First line used on a self-decompressing executable. */
22char ZCAT[]= "#!/usr/bin/zexec /usr/bin/zcat\n";
23char GZCAT[]= "#!/usr/bin/zexec /usr/bin/gzcat\n";
24
25/* Compression filters. */
26char *COMPRESS[]= { "compress", nil };
27char *GZIP[]= { "gzip", "-#", nil };
28
29int excode= 0; /* Exit code. */
30
31void report(char *label)
32{
33 if (label == nil || label[0] == 0)
34 fprintf(stderr, "install: %s\n", strerror(errno));
35 else
36 fprintf(stderr, "install: %s: %s\n", label, strerror(errno));
37 excode= 1;
38}
39
40void fatal(char *label)
41{
42 report(label);
43 exit(1);
44}
45
46void *allocate(void *mem, size_t size)
47/* Safe malloc/realloc. */
48{
49 mem= mem == nil ? malloc(size) : realloc(mem, size);
50
51 if (mem == nil) fatal(nil);
52 return mem;
53}
54
55void deallocate(void *mem)
56{
57 if (mem != nil) free(mem);
58}
59
60int lflag= 0; /* Make a hard link if possible. */
61int cflag= 0; /* Copy if you can't link, otherwise symlink. */
62int dflag= 0; /* Create a directory. */
63int strip= 0; /* Strip the copy. */
64char **compress= nil; /* Compress utility to make a compressed executable. */
65char *zcat= nil; /* Line one to decompress. */
66
67long stack= -1; /* Amount of heap + stack. */
68int wordpow= 1; /* Must be multiplied with wordsize ** wordpow */
69 /* So 8kb for an 8086 and 16kb for the rest. */
70
71pid_t filter(int fd, char **command)
72/* Let a command filter the output to fd. */
73{
74 pid_t pid;
75 int pfd[2];
76
77 if (pipe(pfd) < 0) {
78 report("pipe()");
79 return -1;
80 }
81
82 switch ((pid= fork())) {
83 case -1:
84 report("fork()");
85 return -1;
86 case 0:
87 /* Run the filter. */
88 dup2(pfd[0], 0);
89 dup2(fd, 1);
90 close(pfd[0]);
91 close(pfd[1]);
92 close(fd);
93 signal(SIGPIPE, SIG_DFL);
94 execvp(command[0], command);
95 fatal(command[0]);
96 }
97 /* Connect fd to the pipe. */
98 dup2(pfd[1], fd);
99 close(pfd[0]);
100 close(pfd[1]);
101 return pid;
102}
103
104int mkdirp(char *dir, int mode, int owner, int group)
105/* mkdir -p dir */
106{
107 int keep;
108 char *sep, *pref;
109
110 sep= dir;
111 while (*sep == '/') sep++;
112
113 if (*sep == 0) {
114 errno= EINVAL;
115 return -1;
116 }
117
118 do {
119 while (*sep != '/' && *sep != 0) sep++;
120 pref= sep;
121 while (*sep == '/') sep++;
122
123 keep= *pref; *pref= 0;
124
125 if (strcmp(dir, ".") == 0 || strcmp(dir, "..") == 0) continue;
126
127 if (mkdir(dir, mode) < 0) {
128 if (errno != EEXIST || *sep == 0) {
129 /* On purpose not doing: *pref= keep; */
130 return -1;
131 }
132 } else {
133 if (chown(dir, owner, group) < 0 && errno != EPERM)
134 return -1;
135 }
136 } while (*pref= keep, *sep != 0);
137 return 0;
138}
139
140void makedir(char *dir, int mode, int owner, int group)
141/* Install a directory, and set it's modes. */
142{
143 struct stat st;
144
145 if (stat(dir, &st) < 0) {
146 if (errno != ENOENT) { report(dir); return; }
147
148 /* The target doesn't exist, make it. */
149 if (mode == -1) mode= 0755;
150 if (owner == -1) owner= getuid();
151 if (group == -1) group= getgid();
152
153 if (mkdirp(dir, mode, owner, group) < 0) {
154 report(dir); return;
155 }
156 } else {
157 /* The target does exist, change mode and ownership. */
158 if (mode == -1) mode= (st.st_mode & 07777) | 0555;
159
160 if ((st.st_mode & 07777) != mode) {
161 if (chmod(dir, mode) < 0) { report(dir); return; }
162 }
163 if (owner == -1) owner= st.st_uid;
164 if (group == -1) group= st.st_gid;
165 if (st.st_uid != owner || st.st_gid != group) {
166 if (chown(dir, owner, group) < 0 && errno != EPERM) {
167 report(dir); return;
168 }
169 /* Set the mode again, chown may have wrecked it. */
170 (void) chmod(dir, mode);
171 }
172 }
173}
174
175int setstack(struct exec *hdr)
176/* Set the stack size in a header. Return true if something changed. */
177{
178 long total;
179
180 total= stack;
181 while (wordpow > 0) {
182 total *= hdr->a_cpu == A_I8086 ? 2 : 4;
183 wordpow--;
184 }
185 total+= hdr->a_data + hdr->a_bss;
186
187 if (!(hdr->a_flags & A_SEP)) {
188 total+= hdr->a_text;
189#ifdef A_PAL
190 if (hdr->a_flags & A_PAL) total+= hdr->a_hdrlen;
191#endif
192 }
193 if (hdr->a_cpu == A_I8086 && total > 0x10000L)
194 total= 0x10000L;
195
196 if (hdr->a_total != total) {
197 /* Need to change stack allocation. */
198 hdr->a_total= total;
199
200 return 1;
201 }
202 return 0;
203}
204
205void copylink(char *source, char *dest, int mode, int owner, int group)
206{
207 struct stat sst, dst;
208 int sfd, dfd, n;
209 int r, same= 0, change= 0, docopy= 1;
210 char buf[4096];
211# define hdr ((struct exec *) buf)
212 pid_t pid = 0;
213 int status = 0;
214
215 /* Source must exist as a plain file, dest may exist as a plain file. */
216
217 if (stat(source, &sst) < 0) { report(source); return; }
218
219 if (mode == -1) {
220 mode= sst.st_mode & 07777;
221 if (!lflag || cflag) {
222 mode|= 0444;
223 if (mode & 0111) mode|= 0111;
224 }
225 }
226 if (owner == -1) owner= sst.st_uid;
227 if (group == -1) group= sst.st_gid;
228
229 if (!S_ISREG(sst.st_mode)) {
230 fprintf(stderr, "install: %s is not a regular file\n", source);
231 excode= 1;
232 return;
233 }
234 r= stat(dest, &dst);
235 if (r < 0) {
236 if (errno != ENOENT) { report(dest); return; }
237 } else {
238 if (!S_ISREG(dst.st_mode)) {
239 fprintf(stderr, "install: %s is not a regular file\n",
240 dest);
241 excode= 1;
242 return;
243 }
244
245 /* Are the files the same? */
246 if (sst.st_dev == dst.st_dev && sst.st_ino == dst.st_ino) {
247 if (!lflag && cflag) {
248 fprintf(stderr,
249 "install: %s and %s are the same, can't copy\n",
250 source, dest);
251 excode= 1;
252 return;
253 }
254 same= 1;
255 }
256 }
257
258 if (lflag && !same) {
259 /* Try to link the files. */
260
261 if (r >= 0 && unlink(dest) < 0) {
262 report(dest); return;
263 }
264
265 if (link(source, dest) >= 0) {
266 docopy= 0;
267 } else {
268 if (!cflag || errno != EXDEV) {
269 fprintf(stderr,
270 "install: can't link %s to %s: %s\n",
271 source, dest, strerror(errno));
272 excode= 1;
273 return;
274 }
275 }
276 }
277
278 if (docopy && !same) {
279 /* Copy the files, stripping if necessary. */
280 long count= LONG_MAX;
281 int first= 1;
282
283 if ((sfd= open(source, O_RDONLY)) < 0) {
284 report(source); return;
285 }
286
287 /* Open for write is less simple, its mode may be 444. */
288 dfd= open(dest, O_WRONLY|O_CREAT|O_TRUNC, mode | 0600);
289 if (dfd < 0 && errno == EACCES) {
290 (void) chmod(dest, mode | 0600);
291 dfd= open(dest, O_WRONLY|O_TRUNC);
292 }
293 if (dfd < 0) {
294 report(dest);
295 close(sfd);
296 return;
297 }
298
299 pid= 0;
300 while (count > 0 && (n= read(sfd, buf, sizeof(buf))) > 0) {
301 if (first && n >= A_MINHDR && !BADMAG(*hdr)) {
302 if (strip) {
303 count= hdr->a_hdrlen
304 + hdr->a_text + hdr->a_data;
305#ifdef A_NSYM
306 hdr->a_flags &= ~A_NSYM;
307#endif
308 hdr->a_syms= 0;
309 }
310 if (stack != -1 && setstack(hdr)) change= 1;
311
312 if (compress != nil) {
313 /* Write first #! line. */
314 (void) write(dfd, zcat, strlen(zcat));
315
316 /* Put a compressor in between. */
317 if ((pid= filter(dfd, compress)) < 0) {
318 close(sfd);
319 close(dfd);
320 return;
321 }
322 change= 1;
323 }
324 }
325 if (count < n) n= count;
326
327 if (write(dfd, buf, n) < 0) {
328 report(dest);
329 close(sfd);
330 close(dfd);
331 if (pid != 0) (void) waitpid(pid, nil, 0);
332 return;
333 }
334 count-= n;
335 first= 0;
336 }
337 if (n < 0) report(source);
338 close(sfd);
339 close(dfd);
340 if (pid != 0 && waitpid(pid, &status, 0) < 0 || status != 0) {
341 excode= 1;
342 return;
343 }
344 if (n < 0) return;
345 } else {
346 if (stack != -1) {
347 /* The file has been linked into place. Set the
348 * stack size.
349 */
350 if ((dfd= open(dest, O_RDWR)) < 0) {
351 report(dest);
352 return;
353 }
354
355 if ((n= read(dfd, buf, sizeof(*hdr))) < 0) {
356 report(dest); return;
357 }
358
359 if (n >= A_MINHDR && !BADMAG(*hdr) && setstack(hdr)) {
360 if (lseek(dfd, (off_t) 0, SEEK_SET) == -1
361 || write(dfd, buf, n) < 0
362 ) {
363 report(dest);
364 close(dfd);
365 return;
366 }
367 change= 1;
368 }
369 close(dfd);
370 }
371 }
372
373 if (stat(dest, &dst) < 0) { report(dest); return; }
374
375 if ((dst.st_mode & 07777) != mode) {
376 if (chmod(dest, mode) < 0) { report(dest); return; }
377 }
378 if (dst.st_uid != owner || dst.st_gid != group) {
379 if (chown(dest, owner, group) < 0 && errno != EPERM) {
380 report(dest); return;
381 }
382 /* Set the mode again, chown may have wrecked it. */
383 (void) chmod(dest, mode);
384 }
385 if (!change) {
386 struct utimbuf ubuf;
387
388 ubuf.actime= dst.st_atime;
389 ubuf.modtime= sst.st_mtime;
390
391 if (utime(dest, &ubuf) < 0 && errno != EPERM) {
392 report(dest); return;
393 }
394 }
395}
396
397void usage(void)
398{
399 fprintf(stderr, "\
400Usage:\n\
401 install [-lcsz#] [-o owner] [-g group] [-m mode] [-S stack] [file1] file2\n\
402 install [-lcsz#] [-o owner] [-g group] [-m mode] [-S stack] file ... dir\n\
403 install -d [-o owner] [-g group] [-m mode] directory\n");
404 exit(1);
405}
406
407void main(int argc, char **argv)
408{
409 int i= 1;
410 int mode= -1; /* Mode of target. */
411 int owner= -1; /* Owner. */
412 int group= -1; /* Group. */
413 int super = 0;
414#if NGROUPS_MAX > 0
415 gid_t groups[NGROUPS_MAX];
416 int ngroups;
417 int g;
418#endif
419
420 /* Only those in group 0 are allowed to set owner and group. */
421 if (getgid() == 0) super = 1;
422#if NGROUPS_MAX > 0
423 ngroups= getgroups(NGROUPS_MAX, groups);
424 for (g= 0; g < ngroups; g++) if (groups[g] == 0) super= 1;
425#endif
426 if (!super) {
427 setgid(getgid());
428 setuid(getuid());
429 }
430
431 /* May use a filter. */
432 signal(SIGPIPE, SIG_IGN);
433
434 while (i < argc && argv[i][0] == '-') {
435 char *p= argv[i++]+1;
436 char *end;
437 unsigned long num;
438 int wp;
439 struct passwd *pw;
440 struct group *gr;
441
442 if (strcmp(p, "-") == 0) break;
443
444 while (*p != 0) {
445 switch (*p++) {
446 case 'l': lflag= 1; break;
447 case 'c': cflag= 1; break;
448 case 's': strip= 1; break;
449 case 'd': dflag= 1; break;
450 case 'z':
451 if (compress == nil) {
452 compress= COMPRESS;
453 zcat= ZCAT;
454 }
455 break;
456 case 'o':
457 if (*p == 0) {
458 if (i == argc) usage();
459 p= argv[i++];
460 if (*p == 0) usage();
461 }
462 num= strtoul(p, &end, 10);
463 if (*end == 0) {
464 if ((uid_t) num != num) usage();
465 owner= num;
466 } else {
467 if ((pw= getpwnam(p)) == nil) {
468 fprintf(stderr,
469 "install: %s: unknown user\n",
470 p);
471 exit(1);
472 }
473 owner= pw->pw_uid;
474 }
475 p= "";
476 break;
477 case 'g':
478 if (*p == 0) {
479 if (i == argc) usage();
480 p= argv[i++];
481 if (*p == 0) usage();
482 }
483 num= strtoul(p, &end, 10);
484 if (*end == 0) {
485 if ((gid_t) num != num) usage();
486 group= num;
487 } else {
488 if ((gr= getgrnam(p)) == nil) {
489 fprintf(stderr,
490 "install: %s: unknown user\n",
491 p);
492 exit(1);
493 }
494 group= gr->gr_gid;
495 }
496 p= "";
497 break;
498 case 'm':
499 if (*p == 0) {
500 if (i == argc) usage();
501 p= argv[i++];
502 if (*p == 0) usage();
503 }
504 num= strtoul(p, &end, 010);
505 if (*end != 0 || (num & 07777) != num) usage();
506 mode= num;
507 if ((mode & S_ISUID) && super && owner == -1) {
508 /* Setuid what? Root most likely. */
509 owner= 0;
510 }
511 if ((mode & S_ISGID) && super && group == -1) {
512 group= 0;
513 }
514 p= "";
515 break;
516 case 'S':
517 if (*p == 0) {
518 if (i == argc) usage();
519 p= argv[i++];
520 if (*p == 0) usage();
521 }
522 stack= strtol(p, &end, 0);
523 wp= 0;
524 if (end == p || stack < 0) usage();
525 p= end;
526 while (*p != 0) {
527 switch (*p++) {
528 case 'm':
529 case 'M': num= 1024 * 1024L; break;
530 case 'k':
531 case 'K': num= 1024; break;
532 case 'w':
533 case 'W': num= 4; wp++; break;
534 case 'b':
535 case 'B': num= 1; break;
536 default: usage();
537 }
538 if (stack > LONG_MAX / num) usage();
539 stack*= num;
540 }
541 wordpow= 0;
542 while (wp > 0) { stack /= 4; wordpow++; wp--; }
543 break;
544 default:
545 if ((unsigned) (p[-1] - '1') <= ('9' - '1')) {
546 compress= GZIP;
547 GZIP[1][1]= p[-1];
548 zcat= GZCAT;
549 break;
550 }
551 usage();
552 }
553 }
554 }
555 /* Some options don't mix. */
556 if (dflag && (cflag || lflag || strip)) usage();
557
558 /* Don't let the user umask interfere. */
559 umask(000);
560
561 if (dflag) {
562 /* install directory */
563 if ((argc - i) != 1) usage();
564
565 makedir(argv[i], mode, owner, group);
566 } else {
567 struct stat st;
568
569 if ((argc - i) < 1) usage();
570 if ((lflag || cflag) && (argc - i) == 1) usage();
571
572 if (stat(argv[argc-1], &st) >= 0 && S_ISDIR(st.st_mode)) {
573 /* install file ... dir */
574 char *target= nil;
575 char *base;
576
577 if ((argc - i) == 1) usage();
578
579 while (i < argc-1) {
580 if ((base= strrchr(argv[i], '/')) == nil)
581 base= argv[i];
582 else
583 base++;
584 target= allocate(target, strlen(argv[argc-1])
585 + 1 + strlen(base) + 1);
586 strcpy(target, argv[argc-1]);
587 strcat(target, "/");
588 strcat(target, base);
589
590 copylink(argv[i++], target, mode, owner, group);
591 }
592 } else {
593 /* install [file1] file2 */
594
595 copylink(argv[i], argv[argc-1], mode, owner, group);
596 }
597 }
598 exit(excode);
599}
Note: See TracBrowser for help on using the repository browser.