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
|
#include <sys/types.h>
#include <signal.h>
#include <pwd.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <fcntl.h>
#include "dat.h"
#include "fns.h"
#include "error.h"
enum
{
Debug = 0
};
/*
* os-specific devcmd support.
* this version should be reasonably portable across Unix systems.
*/
typedef struct Targ Targ;
struct Targ
{
int fd[3]; /* fd[0] is standard input, fd[1] is standard output, fd[2] is standard error */
char** args;
char* dir;
int pid;
int wfd; /* child writes errors that occur after the fork or on exec */
int uid;
int gid;
};
extern int gidnobody;
extern int uidnobody;
static int
childproc(Targ *t)
{
#if 0 // in the FreeRTOS no any "os" cmds
int i, nfd;
if(Debug)
print("devcmd: '%s'", t->args[0]);
nfd = getdtablesize();
for(i = 0; i < nfd; i++)
if(i != t->fd[0] && i != t->fd[1] && i != t->fd[2] && i != t->wfd)
close(i);
dup2(t->fd[0], 0);
dup2(t->fd[1], 1);
dup2(t->fd[2], 2);
close(t->fd[0]);
close(t->fd[1]);
close(t->fd[2]);
/* should have an auth file to do host-specific authorisation? */
if(t->gid != -1){
if(setgid(t->gid) < 0 && getegid() == 0){
fprint(t->wfd, "can't set gid %d: %s", t->gid, strerror(errno));
_exit(1);
}
}
if(t->uid != -1){
if(setuid(t->uid) < 0 && geteuid() == 0){
fprint(t->wfd, "can't set uid %d: %s", t->uid, strerror(errno));
_exit(1);
}
}
if(t->dir != nil && chdir(t->dir) < 0){
fprint(t->wfd, "can't chdir to %s: %s", t->dir, strerror(errno));
_exit(1);
}
signal(SIGPIPE, SIG_DFL);
execvp(t->args[0], t->args);
if(Debug)
print("execvp: %s\n",strerror(errno));
fprint(t->wfd, "exec failed: %s", strerror(errno));
_exit(1);
#endif
return 0;
}
void*
oscmd(char **args, int nice, char *dir, int *fd)
{
#if 0 // in the FreeRTOS no any "os" cmds
Targ *t;
int r, fd0[2], fd1[2], fd2[2], wfd[2], n, pid;
t = mallocz(sizeof(*t), 1);
if(t == nil)
return nil;
fd0[0] = fd0[1] = -1;
fd1[0] = fd1[1] = -1;
fd2[0] = fd2[1] = -1;
wfd[0] = wfd[1] = -1;
if(pipe(fd0) < 0 || pipe(fd1) < 0 || pipe(fd2) < 0 || pipe(wfd) < 0)
goto Error;
if(fcntl(wfd[1], F_SETFD, FD_CLOEXEC) < 0) /* close on exec to give end of file on success */
goto Error;
t->fd[0] = fd0[0];
t->fd[1] = fd1[1];
t->fd[2] = fd2[1];
t->wfd = wfd[1];
t->args = args;
t->dir = dir;
t->gid = up->env->gid;
if(t->gid == -1)
t->gid = gidnobody;
t->uid = up->env->uid;
if(t->uid == -1)
t->uid = uidnobody;
signal(SIGCHLD, SIG_DFL);
switch(pid = fork()) {
case -1:
goto Error;
case 0:
setpgrp();
// if(nice)
// setpriority(PRIO_PROCESS, 0, 19);
childproc(t);
_exit(1);
default:
t->pid = pid;
if(Debug)
print("cmd pid %d\n", t->pid);
break;
}
close(fd0[0]);
close(fd1[1]);
close(fd2[1]);
close(wfd[1]);
n = read(wfd[0], up->genbuf, sizeof(up->genbuf)-1);
close(wfd[0]);
if(n > 0){
close(fd0[1]);
close(fd1[0]);
close(fd2[0]);
free(t);
up->genbuf[n] = 0;
if(Debug)
print("oscmd: bad exec: %q\n", up->genbuf);
error(up->genbuf);
return nil;
}
fd[0] = fd0[1];
fd[1] = fd1[0];
fd[2] = fd2[0];
return t;
Error:
r = errno;
if(Debug)
print("oscmd: %q\n",strerror(r));
close(fd0[0]);
close(fd0[1]);
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
close(wfd[0]);
close(wfd[1]);
error(strerror(r));
#endif
return nil;
}
int
oscmdkill(void *a)
{
#if 0 // in the FreeRTOS no any "os" cmds
Targ *t = a;
if(Debug)
print("kill: %d\n", t->pid);
return kill(-t->pid, SIGTERM);
#endif
return 0;
}
int
oscmdwait(void *a, char *buf, int n)
{
#if 0 // in the FreeRTOS no any "os" cmds
Targ *t = a;
int s;
if(waitpid(t->pid, &s, 0) == -1){
if(Debug)
print("wait error: %d [in %d] %q\n", t->pid, getpid(), strerror(errno));
return -1;
}
if(WIFEXITED(s)){
if(WEXITSTATUS(s) == 0)
return snprint(buf, n, "%d 0 0 0 ''", t->pid);
return snprint(buf, n, "%d 0 0 0 'exit: %d'", t->pid, WEXITSTATUS(s));
}
if(WIFSIGNALED(s)){
if(WTERMSIG(s) == SIGTERM || WTERMSIG(s) == SIGKILL)
return snprint(buf, n, "%d 0 0 0 killed", t->pid);
return snprint(buf, n, "%d 0 0 0 'signal: %d'", t->pid, WTERMSIG(s));
}
return snprint(buf, n, "%d 0 0 0 'odd status: 0x%x'", t->pid, s);
#endif
return 0;
}
void
oscmdfree(void *a)
{
free(a);
}
|