run_editor(): also compare size when deciding "has changed"
[s-mailx.git] / popen.c
blobc42f1d3cebc21fe8703b86b4b71f7c1e259475f8
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Handling of pipes, child processes, temporary files, file enwrapping.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 #include <sys/wait.h>
46 #include <fcntl.h>
48 #define READ 0
49 #define WRITE 1
51 #ifndef O_CLOEXEC
52 # define _OUR_CLOEXEC
53 # define O_CLOEXEC 0
54 # define _SET_CLOEXEC(FD) do fcntl((FD), F_SETFD, FD_CLOEXEC); while (0)
55 #else
56 # define _SET_CLOEXEC(FD) do {} while (0)
57 #endif
59 struct fp {
60 FILE *fp;
61 struct fp *link;
62 char *realfile;
63 long offset;
64 int omode;
65 int pipe;
66 int pid;
67 enum {
68 FP_RAW = 0,
69 FP_GZIP = 1<<0,
70 FP_XZ = 1<<1,
71 FP_BZIP2 = 1<<2,
72 FP_IMAP = 1<<3,
73 FP_MAILDIR = 1<<4,
74 FP_MASK = (1<<5) - 1,
75 FP_READONLY = 1<<5
76 } compressed;
79 struct child {
80 int pid;
81 char done;
82 char free;
83 int status;
84 struct child *link;
87 static struct fp *fp_head;
88 static struct child *_popen_child;
90 static int scan_mode(char const *mode, int *omode);
91 static void register_file(FILE *fp, int omode, int ispipe, int pid,
92 int compressed, char const *realfile, long offset);
93 static enum okay _compress(struct fp *fpp);
94 static int _decompress(int compression, int infd, int outfd);
95 static enum okay unregister_file(FILE *fp);
96 static int file_pid(FILE *fp);
98 /* Handle SIGCHLD */
99 static void _sigchld(int signo);
101 static int wait_command(int pid);
102 static struct child *findchild(int pid);
103 static void delchild(struct child *cp);
105 static int
106 scan_mode(char const *mode, int *omode)
108 static struct {
109 char const mode[4];
110 int omode;
111 } const maps[] = {
112 {"r", O_RDONLY},
113 {"w", O_WRONLY | O_CREAT | O_TRUNC},
114 {"wx", O_WRONLY | O_CREAT | O_EXCL},
115 {"a", O_WRONLY | O_APPEND | O_CREAT},
116 {"a+", O_RDWR | O_APPEND},
117 {"r+", O_RDWR},
118 {"w+", O_RDWR | O_CREAT | O_EXCL}
121 int i;
122 NYD_ENTER;
124 for (i = 0; UICMP(z, i, <, NELEM(maps)); ++i)
125 if (!strcmp(maps[i].mode, mode)) {
126 *omode = maps[i].omode;
127 i = 0;
128 goto jleave;
131 alert(_("Internal error: bad stdio open mode %s\n"), mode);
132 errno = EINVAL;
133 *omode = 0; /* (silence CC) */
134 i = -1;
135 jleave:
136 NYD_LEAVE;
137 return i;
140 static void
141 register_file(FILE *fp, int omode, int ispipe, int pid, int compressed,
142 char const *realfile, long offset)
144 struct fp *fpp;
145 NYD_ENTER;
147 fpp = smalloc(sizeof *fpp);
148 fpp->fp = fp;
149 fpp->omode = omode;
150 fpp->pipe = ispipe;
151 fpp->pid = pid;
152 fpp->link = fp_head;
153 fpp->compressed = compressed;
154 fpp->realfile = (realfile != NULL) ? sstrdup(realfile) : NULL;
155 fpp->offset = offset;
156 fp_head = fpp;
157 NYD_LEAVE;
160 static enum okay
161 _compress(struct fp *fpp)
163 char const *cmd[2];
164 int outfd;
165 enum okay rv;
166 NYD_ENTER;
168 if (fpp->omode == O_RDONLY) {
169 rv = OKAY;
170 goto jleave;
172 rv = STOP;
174 fflush(fpp->fp);
175 clearerr(fpp->fp);
176 if (fseek(fpp->fp, fpp->offset, SEEK_SET) == -1)
177 goto jleave;
179 #ifdef HAVE_IMAP
180 if ((fpp->compressed & FP_MASK) == FP_IMAP) {
181 rv = imap_append(fpp->realfile, fpp->fp);
182 goto jleave;
184 #endif
185 if ((fpp->compressed & FP_MASK) == FP_MAILDIR) {
186 rv = maildir_append(fpp->realfile, fpp->fp);
187 goto jleave;
190 outfd = open(fpp->realfile, (fpp->omode | O_CREAT) & ~O_EXCL, 0666);
191 if (outfd == -1) {
192 fprintf(stderr, "Fatal: cannot create ");
193 perror(fpp->realfile);
194 goto jleave;
196 if (!(fpp->omode & O_APPEND))
197 ftruncate(outfd, 0);
198 switch (fpp->compressed & FP_MASK) {
199 case FP_GZIP:
200 cmd[0] = "gzip"; cmd[1] = "-c"; break;
201 case FP_BZIP2:
202 cmd[0] = "bzip2"; cmd[1] = "-c"; break;
203 case FP_XZ:
204 cmd[0] = "xz"; cmd[1] = "-c"; break;
205 default:
206 cmd[0] = "cat"; cmd[1] = NULL; break;
208 if (run_command(cmd[0], 0, fileno(fpp->fp), outfd, cmd[1], NULL, NULL) >= 0)
209 rv = OKAY;
210 close(outfd);
211 jleave:
212 NYD_LEAVE;
213 return rv;
216 static int
217 _decompress(int compression, int infd, int outfd)
219 char const *cmd[2];
220 int rv;
221 NYD_ENTER;
223 switch (compression & FP_MASK) {
224 case FP_GZIP: cmd[0] = "gzip"; cmd[1] = "-cd"; break;
225 case FP_BZIP2: cmd[0] = "bzip2"; cmd[1] = "-cd"; break;
226 case FP_XZ: cmd[0] = "xz"; cmd[1] = "-cd"; break;
227 default: cmd[0] = "cat"; cmd[1] = NULL; break;
228 case FP_IMAP:
229 case FP_MAILDIR:
230 rv = 0;
231 goto jleave;
233 rv = run_command(cmd[0], 0, infd, outfd, cmd[1], NULL, NULL);
234 jleave:
235 NYD_LEAVE;
236 return rv;
239 static enum okay
240 unregister_file(FILE *fp)
242 struct fp **pp, *p;
243 enum okay rv = OKAY;
244 NYD_ENTER;
246 for (pp = &fp_head; (p = *pp) != NULL; pp = &p->link)
247 if (p->fp == fp) {
248 if ((p->compressed & FP_MASK) != FP_RAW) /* TODO ;} */
249 rv = _compress(p);
250 *pp = p->link;
251 if (p->realfile != NULL)
252 free(p->realfile);
253 free(p);
254 goto jleave;
256 #ifdef HAVE_DEBUG
257 panic
258 #else
259 alert
260 #endif
261 (_("Invalid file pointer"));
262 rv = STOP;
263 jleave:
264 NYD_LEAVE;
265 return rv;
268 static int
269 file_pid(FILE *fp)
271 int rv;
272 struct fp *p;
273 NYD_ENTER;
275 rv = -1;
276 for (p = fp_head; p; p = p->link)
277 if (p->fp == fp) {
278 rv = p->pid;
279 break;
281 NYD_LEAVE;
282 return rv;
285 static void
286 _sigchld(int signo)
288 int pid, status;
289 struct child *cp;
290 NYD_X; /* Signal handler */
291 UNUSED(signo);
293 for (;;) {
294 pid = (int)waitpid((pid_t)-1, &status, WNOHANG);
295 if (pid <= 0) {
296 if (pid == -1 && errno == EINTR)
297 continue;
298 break;
300 cp = findchild(pid);
301 if (cp->free)
302 delchild(cp);
303 else {
304 cp->done = 1;
305 cp->status = status;
310 static int
311 wait_command(int pid)
313 int rv = 0;
314 NYD_ENTER;
316 if (!wait_child(pid, NULL)) {
317 if (ok_blook(bsdcompat) || ok_blook(bsdmsgs))
318 fprintf(stderr, _("Fatal error in process.\n"));
319 rv = -1;
321 NYD_LEAVE;
322 return rv;
325 static struct child *
326 findchild(int pid)
328 struct child **cpp;
329 NYD_ENTER;
331 for (cpp = &_popen_child; *cpp != NULL && (*cpp)->pid != pid;
332 cpp = &(*cpp)->link)
334 if (*cpp == NULL) {
335 *cpp = smalloc(sizeof **cpp);
336 (*cpp)->pid = pid;
337 (*cpp)->done = (*cpp)->free = 0;
338 (*cpp)->link = NULL;
340 NYD_LEAVE;
341 return *cpp;
344 static void
345 delchild(struct child *cp)
347 struct child **cpp;
348 NYD_ENTER;
350 for (cpp = &_popen_child; *cpp != cp; cpp = &(*cpp)->link)
352 *cpp = cp->link;
353 free(cp);
354 NYD_LEAVE;
357 FL void
358 command_manager_start(void)
360 struct sigaction nact, oact;
361 NYD_ENTER;
363 nact.sa_handler = &_sigchld;
364 sigemptyset(&nact.sa_mask);
365 nact.sa_flags = 0
366 #ifdef SA_RESTART
367 | SA_RESTART
368 #endif
369 #ifdef SA_NOCLDSTOP
370 | SA_NOCLDSTOP
371 #endif
373 if (sigaction(SIGCHLD, &nact, &oact) != 0)
374 panic("Cannot install signal handler for child process management");
375 NYD_LEAVE;
378 FL FILE *
379 safe_fopen(char const *file, char const *oflags, int *xflags)
381 int osflags, fd;
382 FILE *fp = NULL;
383 NYD_ENTER;
385 if (scan_mode(oflags, &osflags) < 0)
386 goto jleave;
387 osflags |= O_CLOEXEC;
388 if (xflags != NULL)
389 *xflags = osflags;
391 if ((fd = open(file, osflags, 0666)) == -1)
392 goto jleave;
393 _SET_CLOEXEC(fd);
395 fp = fdopen(fd, oflags);
396 jleave:
397 NYD_LEAVE;
398 return fp;
401 FL FILE *
402 Fopen(char const *file, char const *oflags)
404 FILE *fp;
405 int osflags;
406 NYD_ENTER;
408 if ((fp = safe_fopen(file, oflags, &osflags)) != NULL)
409 register_file(fp, osflags, 0, 0, FP_RAW, NULL, 0L);
410 NYD_LEAVE;
411 return fp;
414 FL FILE *
415 Fdopen(int fd, char const *oflags)
417 FILE *fp;
418 int osflags;
419 NYD_ENTER;
421 scan_mode(oflags, &osflags);
422 osflags |= O_CLOEXEC;
424 if ((fp = fdopen(fd, oflags)) != NULL)
425 register_file(fp, osflags, 0, 0, FP_RAW, NULL, 0L);
426 NYD_LEAVE;
427 return fp;
430 FL int
431 Fclose(FILE *fp)
433 int i = 0;
434 NYD_ENTER;
436 if (unregister_file(fp) == OKAY)
437 i |= 1;
438 if (fclose(fp) == 0)
439 i |= 2;
440 NYD_LEAVE;
441 return (i == 3 ? 0 : EOF);
444 FL FILE *
445 Zopen(char const *file, char const *oflags, int *compression) /* FIXME MESS! */
447 FILE *rv = NULL;
448 int _compression, osflags, mode, infd;
449 enum oflags rof;
450 long offset;
451 enum protocol p;
452 NYD_ENTER;
454 if (compression == NULL)
455 compression = &_compression;
457 if (scan_mode(oflags, &osflags) < 0)
458 goto jleave;
459 rof = OF_RDWR | OF_UNLINK;
460 if (osflags & O_APPEND)
461 rof |= OF_APPEND;
462 if (osflags == O_RDONLY) {
463 mode = R_OK;
464 *compression = FP_READONLY;
465 } else {
466 mode = R_OK | W_OK;
467 *compression = 0;
470 /* TODO ???? */
471 if ((osflags & O_APPEND) && ((p = which_protocol(file)) == PROTO_IMAP ||
472 p == PROTO_MAILDIR)) {
473 *compression |= (p == PROTO_IMAP) ? FP_IMAP : FP_MAILDIR;
474 osflags = O_RDWR | O_APPEND | O_CREAT;
475 infd = -1;
476 } else {
477 char const *ext;
479 if ((ext = strrchr(file, '.')) != NULL) {
480 if (!strcmp(ext, ".gz"))
481 *compression |= FP_GZIP;
482 else if (!strcmp(ext, ".xz"))
483 *compression |= FP_XZ;
484 else if (!strcmp(ext, ".bz2"))
485 *compression |= FP_BZIP2;
486 else
487 goto jraw;
488 } else {
489 jraw:
490 *compression |= FP_RAW;
491 rv = Fopen(file, oflags);
492 goto jleave;
494 if ((infd = open(file, (mode & W_OK) ? O_RDWR : O_RDONLY)) == -1 &&
495 (!(osflags & O_CREAT) || errno != ENOENT))
496 goto jleave;
499 if ((rv = Ftmp(NULL, "zopen", rof, 0600)) == NULL) {
500 perror(_("tmpfile"));
501 goto jerr;
503 if (infd >= 0 || (*compression & FP_MASK) == FP_IMAP ||
504 (*compression & FP_MASK) == FP_MAILDIR) {
505 if (_decompress(*compression, infd, fileno(rv)) < 0) {
506 jerr:
507 if (rv != NULL)
508 Fclose(rv);
509 rv = NULL;
510 if (infd >= 0)
511 close(infd);
512 goto jleave;
514 } else {
515 if ((infd = creat(file, 0666)) == -1) {
516 Fclose(rv);
517 rv = NULL;
518 goto jleave;
521 if (infd >= 0)
522 close(infd);
523 fflush(rv);
525 if (!(osflags & O_APPEND))
526 rewind(rv);
527 if ((offset = ftell(rv)) == -1) {
528 Fclose(rv);
529 rv = NULL;
530 goto jleave;
532 register_file(rv, osflags, 0, 0, *compression, file, offset);
533 jleave:
534 NYD_LEAVE;
535 return rv;
538 FL FILE *
539 Ftmp(char **fn, char const *prefix, enum oflags oflags, int mode)
541 FILE *fp = NULL;
542 char *cp_base, *cp;
543 int fd;
544 NYD_ENTER;
546 cp_base =
547 cp = smalloc(strlen(tempdir) + 1 + sizeof("mail") + strlen(prefix) + 7 +1);
548 cp = sstpcpy(cp, tempdir);
549 *cp++ = '/';
550 cp = sstpcpy(cp, "mail");
551 if (*prefix) {
552 *cp++ = '-';
553 cp = sstpcpy(cp, prefix);
555 /* TODO Ftmp(): unroll our own creation loop with atoi(random()) */
556 sstpcpy(cp, ".XXXXXX");
558 hold_all_sigs();
559 #ifdef HAVE_MKSTEMP
560 if ((fd = mkstemp(cp_base)) == -1)
561 goto jfree;
562 if (mode != (S_IRUSR | S_IWUSR) && fchmod(fd, mode) == -1)
563 goto jclose;
564 if (oflags & OF_APPEND) {
565 int f;
567 if ((f = fcntl(fd, F_GETFL)) == -1 ||
568 fcntl(fd, F_SETFL, f | O_APPEND) == -1) {
569 jclose:
570 close(fd);
571 goto junlink;
574 if (!(oflags & OF_REGISTER))
575 fcntl(fd, F_SETFD, FD_CLOEXEC);
576 #else
577 if (mktemp(cp_base) == NULL)
578 goto jfree;
579 if ((fd = open(cp_base, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC |
580 (oflags & OF_APPEND ? O_APPEND : 0), mode)) == -1)
581 goto junlink;
582 if (!(oflags & OF_REGISTER))
583 _SET_CLOEXEC(fd);
584 #endif
586 fp = (*((oflags & OF_REGISTER) ? &Fdopen : &fdopen))(fd,
587 (oflags & OF_RDWR ? "w+" : "w"));
588 if (fp == NULL || (oflags & OF_UNLINK)) {
589 junlink:
590 unlink(cp_base);
591 goto jfree;
594 if (fn != NULL)
595 *fn = cp_base;
596 jleave:
597 if (fp == NULL || !(oflags & OF_HOLDSIGS))
598 rele_all_sigs();
599 NYD_LEAVE;
600 return fp;
601 jfree:
602 if ((cp = cp_base) != NULL)
603 free(cp);
604 goto jleave;
607 FL void
608 Ftmp_release(char **fn)
610 char *cp;
611 NYD_ENTER;
613 cp = *fn;
614 *fn = NULL;
615 if (cp != NULL) {
616 unlink(cp);
617 rele_all_sigs();
618 free(cp);
620 NYD_LEAVE;
623 FL void
624 Ftmp_free(char **fn)
626 char *cp;
627 NYD_ENTER;
629 cp = *fn;
630 *fn = NULL;
631 if (cp != NULL)
632 free(cp);
633 NYD_LEAVE;
636 FL bool_t
637 pipe_cloexec(int fd[2])
639 bool_t rv = FAL0;
640 NYD_ENTER;
642 #ifdef HAVE_PIPE2
643 if (pipe2(fd, O_CLOEXEC) == -1)
644 goto jleave;
645 #else
646 if (pipe(fd) == -1)
647 goto jleave;
648 fcntl(fd[0], F_SETFD, FD_CLOEXEC);
649 fcntl(fd[1], F_SETFD, FD_CLOEXEC);
650 #endif
651 rv = TRU1;
652 jleave:
653 NYD_LEAVE;
654 return rv;
657 FL FILE *
658 Popen(char const *cmd, char const *mode, char const *sh, char const *env_addon,
659 int newfd1)
661 int p[2], myside, hisside, fd0, fd1, pid;
662 char mod[2] = {'0', '\0'};
663 sigset_t nset;
664 FILE *rv = NULL;
665 NYD_ENTER;
667 if (!pipe_cloexec(p))
668 goto jleave;
670 if (*mode == 'r') {
671 myside = p[READ];
672 fd0 = -1;
673 hisside = fd1 = p[WRITE];
674 mod[0] = *mode;
675 } else if (*mode == 'W') {
676 myside = p[WRITE];
677 hisside = fd0 = p[READ];
678 fd1 = newfd1;
679 mod[0] = 'w';
680 } else {
681 myside = p[WRITE];
682 hisside = fd0 = p[READ];
683 fd1 = -1;
684 mod[0] = 'w';
686 sigemptyset(&nset);
687 if (sh == NULL) {
688 pid = start_command(cmd, &nset, fd0, fd1, NULL, NULL, NULL, env_addon);
689 } else {
690 pid = start_command(sh, &nset, fd0, fd1, "-c", cmd, NULL, env_addon);
692 if (pid < 0) {
693 close(p[READ]);
694 close(p[WRITE]);
695 goto jleave;
697 close(hisside);
698 if ((rv = fdopen(myside, mod)) != NULL)
699 register_file(rv, 0, 1, pid, FP_RAW, NULL, 0L);
700 jleave:
701 NYD_LEAVE;
702 return rv;
705 FL bool_t
706 Pclose(FILE *ptr, bool_t dowait)
708 sigset_t nset, oset;
709 int pid;
710 bool_t rv = FAL0;
711 NYD_ENTER;
713 pid = file_pid(ptr);
714 if (pid < 0)
715 goto jleave;
716 unregister_file(ptr);
717 fclose(ptr);
718 if (dowait) {
719 sigemptyset(&nset);
720 sigaddset(&nset, SIGINT);
721 sigaddset(&nset, SIGHUP);
722 sigprocmask(SIG_BLOCK, &nset, &oset);
723 rv = wait_child(pid, NULL);
724 sigprocmask(SIG_SETMASK, &oset, NULL);
725 } else {
726 free_child(pid);
727 rv = TRU1;
729 jleave:
730 NYD_LEAVE;
731 return rv;
734 FL void
735 close_all_files(void)
737 NYD_ENTER;
738 while (fp_head != NULL)
739 if (fp_head->pipe)
740 Pclose(fp_head->fp, TRU1);
741 else
742 Fclose(fp_head->fp);
743 NYD_LEAVE;
746 FL int
747 run_command(char const *cmd, sigset_t *mask, int infd, int outfd,
748 char const *a0, char const *a1, char const *a2)
750 int rv;
751 NYD_ENTER;
753 if ((rv = start_command(cmd, mask, infd, outfd, a0, a1, a2, NULL)) < 0)
754 rv = -1;
755 else
756 rv = wait_command(rv);
757 NYD_LEAVE;
758 return rv;
761 FL int
762 start_command(char const *cmd, sigset_t *mask, int infd, int outfd,
763 char const *a0, char const *a1, char const *a2,
764 char const *env_addon)
766 int rv;
767 NYD_ENTER;
768 UNUSED(env_addon);
770 if ((rv = fork()) == -1) {
771 perror("fork");
772 rv = -1;
773 } else if (rv == 0) {
774 char *argv[128];
775 int i;
777 if (env_addon != NULL) { /* TODO env_addon; should have struct child */
778 extern char **environ;
779 size_t e, vl;
780 char **env;
781 char const *nv;
783 /* TODO note we don't check the POSIX limit:
784 * the total space used to store the environment and the arguments to
785 * the process is limited to {ARG_MAX} bytes */
786 for (e = 0; environ[e] != NULL; ++e)
789 env = ac_alloc(sizeof(*env) * (e + 1 +1));
790 nv = strchr(env_addon, '=');
791 vl = PTR2SIZE(nv - env_addon);
793 for (e = 0; environ[e] != NULL; ++e) {
794 if (env_addon == NULL)
795 env[e] = environ[e];
796 else {
797 char const *ov = strchr(environ[e], '=');
798 if (ov != NULL) {
799 if (vl == PTR2SIZE(ov - environ[e]) &&
800 !memcmp(env_addon, environ[e], vl)) {
801 env[e] = UNCONST(env_addon);
802 env_addon = NULL;
803 continue;
806 env[e] = environ[e];
809 if (env_addon != NULL)
810 env[e++] = UNCONST(env_addon);
811 env[e] = NULL;
813 environ = env;
816 i = getrawlist(cmd, strlen(cmd), argv, NELEM(argv), 0);
818 if ((argv[i++] = UNCONST(a0)) != NULL &&
819 (argv[i++] = UNCONST(a1)) != NULL &&
820 (argv[i++] = UNCONST(a2)) != NULL)
821 argv[i] = NULL;
822 prepare_child(mask, infd, outfd);
823 execvp(argv[0], argv);
824 perror(argv[0]);
825 _exit(1);
827 NYD_LEAVE;
828 return rv;
831 FL void
832 prepare_child(sigset_t *nset, int infd, int outfd)
834 int i;
835 sigset_t fset;
836 NYD_ENTER;
838 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
839 if (infd >= 0)
840 dup2(infd, 0);
841 if (outfd >= 0)
842 dup2(outfd, 1);
844 if (nset) {
845 for (i = 1; i < NSIG; ++i)
846 if (sigismember(nset, i))
847 safe_signal(i, SIG_IGN);
848 if (!sigismember(nset, SIGINT))
849 safe_signal(SIGINT, SIG_DFL);
852 sigemptyset(&fset);
853 sigprocmask(SIG_SETMASK, &fset, NULL);
854 NYD_LEAVE;
857 FL void
858 free_child(int pid)
860 sigset_t nset, oset;
861 struct child *cp;
862 NYD_ENTER;
864 sigemptyset(&nset);
865 sigaddset(&nset, SIGCHLD);
866 sigprocmask(SIG_BLOCK, &nset, &oset);
868 cp = findchild(pid);
869 if (cp->done)
870 delchild(cp);
871 else
872 cp->free = 1;
874 sigprocmask(SIG_SETMASK, &oset, NULL);
875 NYD_LEAVE;
878 FL bool_t
879 wait_child(int pid, int *wait_status)
881 sigset_t nset, oset;
882 struct child *cp;
883 int ws;
884 bool_t rv;
885 NYD_ENTER;
887 sigemptyset(&nset);
888 sigaddset(&nset, SIGCHLD);
889 sigprocmask(SIG_BLOCK, &nset, &oset);
891 cp = findchild(pid);
892 while (!cp->done)
893 sigsuspend(&oset);
894 ws = cp->status;
895 delchild(cp);
897 sigprocmask(SIG_SETMASK, &oset, NULL);
899 if (wait_status != NULL)
900 *wait_status = ws;
901 rv = (WIFEXITED(ws) && WEXITSTATUS(ws) == 0);
902 NYD_LEAVE;
903 return rv;
906 #ifdef _OUR_CLOEXEC
907 # undef O_CLOEXEC
908 # undef _OUR_CLOEXEC
909 #endif
910 #undef _SET_CLOEXEC
912 /* vim:set fenc=utf-8:s-it-mode */