s/qmail 4.3.17
Next generation secure email transport
Loading...
Searching...
No Matches
preline.c
Go to the documentation of this file.
1#include <unistd.h>
2#include "fd.h"
3#include "buffer.h"
4#include "exit.h"
5#include "wait.h"
6#include "env.h"
7#include "sig.h"
8#include "getoptb.h"
9#include "logmsg.h"
10#include "qmail.h"
11
12#define WHO "preline"
13
14static void die_usage()
15{
16 logmsg(WHO,100,USAGE,"preline cmd [ arg ... ]");
17}
18
19int flagufline = 1; char *ufline;
20int flagrpline = 1; char *rpline;
21int flagdtline = 1; char *dtline;
22
24buffer bo = BUFFER_INIT(buffer_unixwrite,1,outbuf,sizeof(outbuf));
26buffer bi = BUFFER_INIT(buffer_unixread,0,inbuf,sizeof(inbuf));
27
28int main(int argc,char * const *argv)
29{
30 int opt;
31 int pi[2];
32 int pid;
33 int wstat;
34
35 sig_pipeignore();
36
37 if (!(ufline = env_get("UFLINE"))) die_usage();
38 if (!(rpline = env_get("RPLINE"))) die_usage();
39 if (!(dtline = env_get("DTLINE"))) die_usage();
40
41 while ((opt = getoptb(argc,(char **)argv,"frdFRD")) != opteof)
42 switch (opt) {
43 case 'f': flagufline = 0; break;
44 case 'r': flagrpline = 0; break;
45 case 'd': flagdtline = 0; break;
46 case 'F': flagufline = 1; break;
47 case 'R': flagrpline = 1; break;
48 case 'D': flagdtline = 1; break;
49 default: die_usage();
50 }
51 argc -= optind;
52 argv += optind;
53 if (!*argv) die_usage();
54
55 if (pipe(pi) == -1)
56 logmsg(WHO,111,FATAL,"unable to create pipe");
57
58 pid = fork();
59 if (pid == -1)
60 logmsg(WHO,111,FATAL,"unable to fork");
61
62 if (pid == 0) {
63 close(pi[1]);
64 if (fd_move(0,pi[0]) == -1)
65 logmsg(WHO,111,FATAL,"unable to set up fds");
66 sig_pipedefault();
67 execvp(*argv,argv);
68 logmsg(WHO,errno,FATAL,B("unable to run: ",*argv));
69 }
70 close(pi[0]);
71 if (fd_move(1,pi[1]) == -1)
72 logmsg(WHO,111,FATAL,"unable to set up fds");
73
74 if (flagufline) buffer_puts(&bo,ufline);
75 if (flagrpline) buffer_puts(&bo,rpline);
76 if (flagdtline) buffer_puts(&bo,dtline);
77 if (buffer_copy(&bo,&bi) != 0)
78 logmsg(WHO,111,FATAL,"unable to copy input");
79 buffer_flush(&bo);
80 close(1);
81
82 if (wait_pid(&wstat,pid) == -1)
83 logmsg(WHO,111,FATAL,"wait failed");
84 if (wait_crashed(wstat))
85 logmsg(WHO,111,FATAL,"child crashed");
86 _exit(wait_exitcode(wstat));
87}
int main()
Definition: chkshsgr.c:6
void _exit(int)
int flagufline
Definition: preline.c:19
char * dtline
Definition: preline.c:21
char * rpline
Definition: preline.c:20
int flagrpline
Definition: preline.c:20
char outbuf[BUFSIZE_LINE]
Definition: preline.c:23
buffer bi
Definition: preline.c:26
char * ufline
Definition: preline.c:19
int flagdtline
Definition: preline.c:21
buffer bo
Definition: preline.c:24
#define WHO
Definition: preline.c:12
char inbuf[BUFSIZE_LINE]
Definition: preline.c:25
#define BUFSIZE_LINE
Definition: qmail.h:8