File: extcmd.c

package info (click to toggle)
node 0.3.2-7.4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 432 kB
  • ctags: 235
  • sloc: ansic: 3,244; makefile: 103; sh: 95
file content (217 lines) | stat: -rw-r--r-- 4,339 bytes parent folder | download | duplicates (4)
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <grp.h>
#include <sys/wait.h>
#include <netax25/axlib.h>
#include <netax25/axconfig.h>

#include "node.h"

#define ECMD_PIPE	1		/* Run through pipe 		*/
#define ECMD_RECONN	2		/* */

static int norm_extcmd(struct cmd *cmdp, char **argv)
{
	int pid;

	alarm(0L);
	pid = fork();
	if (pid == -1) {
		/* fork error */
		node_perror("norm_extcmd: fork", errno);
		return 0;
	}
	if (pid == 0) {
		/* child */
		setgroups(0, NULL);
		setgid(cmdp->gid);
		setuid(cmdp->uid);
		execve(cmdp->path, argv, NULL);
		node_perror("norm_extcmd: execve", errno);
		exit(1);
	}
	/* parent */
	waitpid(pid, NULL, 0);
	return 0;
}

static int pipe_extcmd(struct cmd *cmdp, char **argv)
{
	ax25io *iop;
	int pipe_in[2], pipe_out[2];
	int maxfd, pid, c;
	fd_set rdfdset, wrfdset;
	int pend_nodew = -1, pend_pipew = -1;

	if (pipe(pipe_in) == -1) {
		node_perror("pipe_extcmd: pipe_in", errno);
		return 0;
	}
	if (pipe(pipe_out) == -1) {
		node_perror("pipe_extcmd: pipe_out", errno);
		return 0;
	}

	signal(SIGCHLD, SIG_IGN);

	pid = fork();
	if (pid == -1) {	/* fork error */
		node_perror("pipe_extcmd: fork", errno);
		signal(SIGCHLD, SIG_DFL);
		return 0;
	}
	if (pid == 0) {		/* child */
		/*
		 * Redirect childs output to the pipes closing
		 * stdin/out/err as we go.
		 */
		dup2(pipe_in[0], STDIN_FILENO);
		dup2(pipe_out[1], STDOUT_FILENO);
		dup2(pipe_out[1], STDERR_FILENO);

		/* Close the other ends */
		close(pipe_in[1]);
		close(pipe_out[0]);

		/* Set uid, gid and supplementary groups */
		setgroups(0, NULL);
		setgid(cmdp->gid);
		setuid(cmdp->uid);

		execve(cmdp->path, argv, NULL);
		perror("pipe_extcmd: execve");
		exit(1);
	}

	/* parent */
	close(pipe_in[0]);
	close(pipe_out[1]);

	iop = axio_init(pipe_out[0], pipe_in[1], 1024, UNSPEC_EOL);
	if (iop == NULL) {
		node_perror("pipe_extcmd: Error initializing I/O", -1);

		signal(SIGCHLD, SIG_DFL);
		kill(pid, SIGKILL);

		return 0;
	}

	node_set_nonblock(NodeIo, 1);
	node_set_nonblock(iop, 1);

	maxfd = NodeIo->ifd;
	maxfd = MAX(maxfd, NodeIo->ofd);
	maxfd = MAX(maxfd, iop->ifd);
	maxfd = MAX(maxfd, iop->ofd);
	maxfd++;

	while (1) {
		FD_ZERO(&rdfdset);
		FD_ZERO(&wrfdset);

		FD_SET(NodeIo->ifd, &rdfdset);
		FD_SET(iop->ifd, &rdfdset);

		if (pend_nodew != -1)
			FD_SET(NodeIo->ofd, &wrfdset);
		if (pend_pipew != -1)
			FD_SET(iop->ofd, &wrfdset);

		if (select(maxfd, &rdfdset, &wrfdset, NULL, NULL) == -1) {
			node_perror("pipe_extcmd: select", errno);
			break;
		}

		/* Try to write pending char to pipe */
		if (pend_pipew != -1) {
			if (axio_putc(pend_pipew, iop) != -1)
				pend_pipew = -1;
			else if (errno != EAGAIN)
				break;
		}

		/* If nothing is pending we can try to get new data */
		if (pend_pipew == -1) {
			while ((c = axio_getc(NodeIo)) != -1) {
				alarm(ConnTimeout);
				if (axio_putc(c, iop) == -1) {
					pend_pipew = c;
					break;
				}
			}
			if (errno != EAGAIN)
				break;
		}

		/* Try to write pending char to node connection */
		if (pend_nodew != -1) {
			if (axio_putc(pend_nodew, NodeIo) != -1)
				pend_nodew = -1;
			else if (errno != EAGAIN)
				break;
		}

		/* If nothing is pending we can try to get new data */
		if (pend_nodew == -1) {
			while ((c = axio_getc(iop)) != -1) {
				alarm(ConnTimeout);
				if (axio_putc(c, NodeIo) == -1) {
					pend_nodew = c;
					break;
				}
			}
			if (errno != EAGAIN) {
                                if (errno)
                                        node_msg("%s", strerror(errno));
                                break;
			}

		}

		/* blocking flush */
		if (node_flush(NodeIo) == -1)
			break;
		if (node_flush(iop) == -1)
			break;
	}

	node_set_nonblock(NodeIo, 0);
	node_set_nonblock(iop, 0);

	axio_end(iop);

	signal(SIGCHLD, SIG_DFL);
	kill(pid, SIGKILL);

	return 0;
}

int extcmd(struct cmd *cmdp, char **argv)
{
	int ret;

	User.state = STATE_EXTCMD;
	User.dl_type = AF_UNSPEC;
	strcpy(User.dl_name, cmdp->name);
	strupr(User.dl_name);
	update_user();

	if (cmdp->flags & ECMD_PIPE)
		ret = pipe_extcmd(cmdp, argv);
	else
		ret = norm_extcmd(cmdp, argv);

	if (cmdp->flags & ECMD_RECONN)
		node_msg("Reconnected to %s", HostName);

	return ret;
}