summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/sfio/sfpopen.c
blob: d496c13e7b1b576e396dd81ffa7f6972d23feb52 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#include	"sfhdr.h"

/*	Create a coprocess.
**	Written by Kiem-Phong Vo.
*/

#if _PACKAGE_ast
#include	<proc.h>
#else

#define EXIT_NOTFOUND	127

#define READ		0
#define WRITE		1

#ifndef CHAR_BIT
#define CHAR_BIT	8
#endif
static char	Meta[1<<CHAR_BIT], **Path;

/* execute command directly if possible; else use the shell */
#if __STD_C
static void execute(const char* argcmd)
#else
static void execute(argcmd)
char*	argcmd;
#endif
{
	reg char	*s, *cmd, **argv, **p, *interp;
	reg int		n;

	/* define interpreter */
	if(!(interp = getenv("SHELL")) || !interp[0])
		interp = "/bin/sh";

	if(strcmp(interp,"/bin/sh") != 0 && strcmp(interp,"/bin/ksh") != 0 )
	{	if(access(interp,X_OK) == 0)
			goto do_interp;
		else	interp = "/bin/sh";
	}

	/* if there is a meta character, let the shell do it */
	for(s = (char*)argcmd; *s; ++s)
		if(Meta[(uchar)s[0]])
			goto do_interp;

	/* try to construct argv */
	if(!(cmd = (char*)malloc(strlen(argcmd)+1)) )
		goto do_interp;
	strcpy(cmd,argcmd);
	if(!(argv = (char**)malloc(16*sizeof(char*))) )
		goto do_interp;
	for(n = 0, s = cmd;; )
	{	while(isspace(s[0]))
			s += 1;
		if(s[0] == 0)
			break;

		/* new argument */
		argv[n++] = s;
		if((n%16) == 0 && !(argv = (char**)realloc(argv,(n+16)*sizeof(char*))) )
			goto do_interp;

		/* make this into a C string */
		while(s[0] && !isspace(s[0]))
			s += 1;
		if(!s[0])
			*s++ = 0;
	}
	if(n == 0)
		goto do_interp;
	argv[n] = NIL(char*);

	/* get the command name */
	cmd = argv[0];
	for(s = cmd+strlen(cmd)-1; s >= cmd; --s)
		if(*s == '/')
			break;
	argv[0] = s+1;

	/* Non-standard pathnames as in nDFS should be handled by the shell */
	for(s = cmd+strlen(cmd)-1; s >= cmd+2; --s)
		if(s[0] == '.' && s[-1] == '.' && s[-2] == '.')
			goto do_interp;

	if(cmd[0] == '/' ||
	   (cmd[0] == '.' && cmd[1] == '/') ||
	   (cmd[0] == '.' && cmd[1] == '.' && cmd[2] == '/') )
	{	if(access(cmd,X_OK) != 0)
			goto do_interp;
		else	execv(cmd,argv);
	}
	else
	{	for(p = Path; *p; ++p)
		{	s = sfprints("%s/%s", *p, cmd);
			if(access(s,X_OK) == 0)
				execv(s,argv);
		}
	}

	/* if get here, let the interpreter do it */
do_interp: 
	for(s = interp+strlen(interp)-1; s >= interp; --s)
		if(*s == '/')
			break;
	execl(interp, s+1, "-c", argcmd, NIL(char*));
	_exit(EXIT_NOTFOUND);
}

#endif /*_PACKAGE_ast*/

#if __STD_C
Sfio_t*	sfpopen(Sfio_t* f, const char* command, const char* mode)
#else
Sfio_t*	sfpopen(f,command,mode)
Sfio_t*	f;
char*	command;	/* command to execute */
char*	mode;		/* mode of the stream */
#endif
{
#if _PACKAGE_ast
	reg Proc_t*	proc;
	reg int		sflags;
	reg long	flags;
	reg int		pflags;
	char*		av[4];

	if (!command || !command[0] || !mode)
		return 0;
	sflags = _sftype(mode, NiL, NiL);

	if(f == (Sfio_t*)(-1))
	{	/* stdio compatibility mode */
		f = NIL(Sfio_t*);
		pflags = 1;
	}
	else	pflags = 0;

	flags = 0;
	if (sflags & SF_READ)
		flags |= PROC_READ;
	if (sflags & SF_WRITE)
		flags |= PROC_WRITE;
	av[0] = "sh";
	av[1] = "-c";
	av[2] = (char*)command;
	av[3] = 0;
	if (!(proc = procopen(0, av, 0, 0, flags)))
		return 0;
	if (!(f = sfnew(f, NIL(Void_t*), (size_t)SF_UNBOUND,
	       		(sflags&SF_READ) ? proc->rfd : proc->wfd, sflags|((sflags&SF_RDWR)?0:SF_READ))) ||
	    _sfpopen(f, (sflags&SF_READ) ? proc->wfd : -1, proc->pid, pflags) < 0)
	{
		if (f) sfclose(f);
		procclose(proc);
		return 0;
	}
	procfree(proc);
	return f;
#else
	reg int		pid, fd, pkeep, ckeep, sflags;
	int		stdio, parent[2], child[2];
	Sfio_t		sf;

	/* set shell meta characters */
	if(Meta[0] == 0)
	{	reg char*	s;
		Meta[0] = 1;
		for(s = "!@#$%&*(){}[]:;<>~`'|\"\\"; *s; ++s)
			Meta[(uchar)s[0]] = 1;
	}
	if(!Path)
		Path = _sfgetpath("PATH");

	/* sanity check */
	if(!command || !command[0] || !mode)
		return NIL(Sfio_t*);
	sflags = _sftype(mode,NIL(int*),NIL(int*));

	/* make pipes */
	parent[0] = parent[1] = child[0] = child[1] = -1;
	if(sflags&SF_RDWR)
	{	if(syspipef(parent) < 0)
			goto error;
		if((sflags&SF_RDWR) == SF_RDWR && syspipef(child) < 0)
			goto error;
	}

	switch((pid = fork()) )
	{
	default :	/* in parent process */
		if(sflags&SF_READ)
			{ pkeep = READ; ckeep = WRITE; }
		else	{ pkeep = WRITE; ckeep = READ; }

		if(f == (Sfio_t*)(-1))
		{	/* stdio compatibility mode */
			f = NIL(Sfio_t*);
			stdio = 1;
		}
		else	stdio = 0;

		/* make the streams */
		if(!(f = sfnew(f,NIL(Void_t*),(size_t)SF_UNBOUND,parent[pkeep],sflags|((sflags&SF_RDWR)?0:SF_READ))))
			goto error;
		if(sflags&SF_RDWR)
		{	CLOSE(parent[!pkeep]);
			SETCLOEXEC(parent[pkeep]);
			if((sflags&SF_RDWR) == SF_RDWR)
			{	CLOSE(child[!ckeep]);
				SETCLOEXEC(child[ckeep]);
			}
		}

		/* save process info */
		fd = (sflags&SF_RDWR) == SF_RDWR ? child[ckeep] : -1;
		if(_sfpopen(f,fd,pid,stdio) < 0)
		{	(void)sfclose(f);
			goto error;
		}

		return f;

	case 0 :	/* in child process */
		/* determine what to keep */
		if(sflags&SF_READ)
			{ pkeep = WRITE; ckeep = READ; }
		else	{ pkeep = READ; ckeep = WRITE; }

		/* zap fd that we don't need */
		if(sflags&SF_RDWR)
		{	CLOSE(parent[!pkeep]);
			if((sflags&SF_RDWR) == SF_RDWR)
				CLOSE(child[!ckeep]);
		}

		/* use sfsetfd to make these descriptors the std-ones */
		SFCLEAR(&sf,NIL(Vtmutex_t*));

		/* must be careful so not to close something useful */
		if((sflags&SF_RDWR) == SF_RDWR && pkeep == child[ckeep])
			if((child[ckeep] = sysdupf(pkeep)) < 0)
				_exit(EXIT_NOTFOUND);

		if(sflags&SF_RDWR)
		{	if (parent[pkeep] != pkeep)
			{	sf.file = parent[pkeep];
				CLOSE(pkeep);
				if(sfsetfd(&sf,pkeep) != pkeep)
					_exit(EXIT_NOTFOUND);
			}
			if((sflags&SF_RDWR) == SF_RDWR && child[ckeep] != ckeep)
			{	sf.file = child[ckeep];
				CLOSE(ckeep);
				if(sfsetfd(&sf,ckeep) != ckeep)
					_exit(EXIT_NOTFOUND);
			}
		}

		execute(command);
		return NIL(Sfio_t*);

	case -1 :	/* error */
	error:
		if(parent[0] >= 0)
			{ CLOSE(parent[0]); CLOSE(parent[1]); }
		if(child[0] >= 0)
			{ CLOSE(child[0]); CLOSE(child[1]); }
		return NIL(Sfio_t*);
	}
#endif /*_PACKAGE_ast*/
}