File: expipe.c

package info (click to toggle)
ifmail 2.14tx8.10-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: ansic: 30,328; perl: 4,955; yacc: 839; makefile: 716; sh: 424; cpp: 235; lex: 206; awk: 24
file content (137 lines) | stat: -rw-r--r-- 2,956 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
#include "lutil.h"

static struct _fppid {
	FILE *fp;
	int pid;
} fppid[] = {
	{NULL,0},
	{NULL,0},
	{NULL,0}
};
#define maxfppid 2

FILE *expipe(cmd,from,to)
char *cmd,*from,*to;
{
	char buf[256],*buflimit;
	char *vector[16];
	int i,rc;
	char *p,*q,*f=from,*t=to;
	int pipedes[2];
	FILE *fp;
	int pid,slot;

	buflimit=buf+sizeof(buf)-1-
		(f&&t&&(strlen(f)>strlen(t))?strlen(f):t?strlen(t):0);

	for (slot=0;slot<=maxfppid;slot++) {
		if (fppid[slot].fp == NULL) break;
	}
	if (slot > maxfppid) {
		logerr("attemt to pipe more than %d processes",maxfppid + 1);
		return NULL;
	}

	for (p=cmd,q=buf;(*p);p++) {
		if (q > buflimit) {
			logerr("attemt to pipe too long command");
			return NULL;
		}
		switch (*p) {
		case '$':	switch (*(++p))
				{
				case 'f':
				case 'F': if ((f)) while (*f) *(q++)=*(f++);
					f=from; break;
				case 't':
				case 'T': if ((t)) while (*t) *(q++)=*(t++);
					t=to; break;
				default: *(q++)='$'; *(q++)=*p; break;
				}
				break;
		case '\\':	*(q++)=*(++p); break;
		default: *(q++)=*p; break;
		}
	}
	*q='\0';
	loginf("Expipe: %s",buf);
	i=0;
	vector[i++]=strtok(buf," \t\n");
	while ((vector[i++]=strtok(NULL," \t\n")) && (i<16));
	vector[15]=NULL;
	fflush(stdout);
	fflush(stderr);
	if (pipe(pipedes) != 0) {
		logerr("$pipe failed for command \"%s\"",S(vector[0]));
		return NULL;
	}
	debug(2,"pipe() returned %d,%d",pipedes[0],pipedes[1]);
	if ((pid=fork()) == 0) {
		close(pipedes[1]);
		close(0);
		if (dup(pipedes[0]) != 0) {
			logerr("$Reopen of stdin for command %s failed",
				S(vector[0]));
			exit(-1);
		}
		rc=execv(vector[0],vector);
		logerr("$Exec \"%s\" returned %d",S(vector[0]),rc);
		exit(-1);
	}
	close(pipedes[0]);
	if ((fp=fdopen(pipedes[1],"w")) == NULL) {
		logerr("$fdopen failed for pipe to command \"%s\"",
			S(vector[0]));
	}
	fppid[slot].fp=fp;
	fppid[slot].pid=pid;
	return fp;
}

int exclose(fp)
FILE *fp;
{
	int status,rc;
	int pid,slot,sverr;

	for (slot=0;slot<=maxfppid;slot++) {
		if (fppid[slot].fp == fp) break;
	}
	if (slot > maxfppid) {
		logerr("attempt to close unopened pipe");
		return -1;
	}
	pid=fppid[slot].pid;
	fppid[slot].fp=NULL;
	fppid[slot].pid=0;

	debug(2,"closing pipe to the child process %d",pid);
	if ((rc=fclose(fp)) != 0) {
		logerr("$error closing pipe to transport (rc=%d)",rc);
		if ((rc=kill(pid,SIGKILL)) != 0)
			logerr("$kill for pid %d returned %d",pid,rc);
	}
	debug(2,"waiting for process %d to finish",pid);
	do {
		rc=wait(&status);
		sverr=errno;
		debug(2,"$Wait returned %d, status %d,%d",
			rc,status>>8,status&0xff);
	} while (((rc > 0) && (rc != pid)) || ((rc == -1) && (sverr == EINTR)));
	if (rc == -1) {
		logerr("$Wait returned %d, status %d,%d",
			rc,status>>8,status&0xff);
		return -1;
	}
	debug(2,"status=%d",status);
	return status;
}