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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*
* notion/mainloop/exec.c
*
* Copyright (c) the Notion team 2013.
* Copyright (c) Tuomo Valkonen 1999-2007.
*
* See the included file LICENSE for details.
*/
#include <limits.h>
#include <sys/types.h>
#include <sys/signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <libtu/output.h>
#include <libtu/misc.h>
#include <libtu/locale.h>
#include <libtu/types.h>
#include "select.h"
#include "exec.h"
/*{{{ Exec/spawn/fork */
#define SHELL_PATH "/bin/sh"
#define SHELL_NAME "sh"
#define SHELL_ARG "-c"
void mainloop_do_exec(const char *cmd)
{
char *argv[4];
argv[0]=SHELL_NAME;
argv[1]=SHELL_ARG;
argv[2]=(char*)cmd; /* stupid execve... */
argv[3]=NULL;
execvp(SHELL_PATH, argv);
}
static int mypipe(int *fds)
{
int r=pipe(fds);
if(r==0){
cloexec_braindamage_fix(fds[0]);
cloexec_braindamage_fix(fds[1]);
}else{
warn_err_obj("pipe()");
}
return r;
}
static bool unblock(int fd)
{
int fl=fcntl(fd, F_GETFL);
if(fl!=-1)
fl=fcntl(fd, F_SETFL, fl|O_NONBLOCK);
return (fd!=-1);
}
static void duppipe(int fd, int idx, int *fds)
{
close(fd);
ignore_value(dup(fds[idx]));
close(fds[0]);
close(fds[1]);
}
pid_t mainloop_fork(void (*fn)(void *p), void *fnp,
int *infd, int *outfd, int *errfd)
{
int pid;
int infds[2];
int outfds[2];
int errfds[2];
if(infd!=NULL){
if(mypipe(infds)!=0)
return -1;
}
if(outfd!=NULL){
if(mypipe(outfds)!=0)
goto err1;
}
if(errfd!=NULL){
if(mypipe(errfds)!=0)
goto err2;
}
pid=fork();
if(pid<0)
goto err3;
if(pid!=0){
/* We're the parent */
if(outfd!=NULL){
if(!unblock(outfds[0]))
goto err3;
*outfd=outfds[0];
close(outfds[1]);
}
if(errfd!=NULL){
if(!unblock(errfds[0]))
goto err3;
*errfd=errfds[0];
close(errfds[1]);
}
if(infd!=NULL){
*infd=infds[1];
close(infds[0]);
}
return pid;
} else {
/* We're the child */
if(infd!=NULL)
duppipe(0, 0, infds);
if(outfd!=NULL)
duppipe(1, 1, outfds);
if(errfd!=NULL)
duppipe(2, 1, errfds);
fn(fnp);
abort();
}
err3:
warn_err();
if(errfd!=NULL){
close(errfds[0]);
close(errfds[1]);
}
err2:
if(outfd!=NULL){
close(outfds[0]);
close(outfds[1]);
}
err1:
if(infd!=NULL){
close(infds[0]);
close(infds[1]);
}
return -1;
}
typedef struct{
const char *cmd;
void (*initenv)(void *p);
void *initenvp;
} SpawnP;
static void do_spawn(void *spawnp)
{
SpawnP *p=(SpawnP*)spawnp;
if(p->initenv)
p->initenv(p->initenvp);
mainloop_do_exec(p->cmd);
}
pid_t mainloop_do_spawn(const char *cmd,
void (*initenv)(void *p), void *p,
int *infd, int *outfd, int *errfd)
{
SpawnP spawnp;
spawnp.cmd=cmd;
spawnp.initenv=initenv;
spawnp.initenvp=p;
return mainloop_fork(do_spawn, (void*)&spawnp, infd, outfd, errfd);
}
pid_t mainloop_spawn(const char *cmd)
{
return mainloop_do_spawn(cmd, NULL, NULL, NULL, NULL, NULL);
}
/*}}}*/
/*{{{ popen_bgread */
#define BL 1024
bool mainloop_process_pipe_extlfn(int fd, ExtlFn fn)
{
char buf[BL];
int n;
n=read(fd, buf, BL-1);
if(n<0){
if(errno==EAGAIN || errno==EINTR)
return TRUE;
n=0;
warn_err_obj(TR("reading a pipe"));
return FALSE;
}else if(n>0){
buf[n]='\0';
extl_call(fn, "s", NULL, &buf);
return TRUE;
}else/* if(n==0)*/{
/* Call with no argument/NULL string to signify EOF */
extl_call(fn, NULL, NULL);
return FALSE;
}
}
static void process_pipe(int fd, void *p)
{
if(!mainloop_process_pipe_extlfn(fd, *(ExtlFn*)p)){
/* We get here on EOL or if the handler failed */
mainloop_unregister_input_fd(fd);
close(fd);
extl_unref_fn(*(ExtlFn*)p);
free(p);
}
}
bool mainloop_register_input_fd_extlfn(int fd, ExtlFn fn)
{
ExtlFn *p=ALLOC(ExtlFn);
if(p!=NULL){
*(ExtlFn*)p=extl_ref_fn(fn);
if(mainloop_register_input_fd(fd, p, process_pipe))
return TRUE;
extl_unref_fn(*(ExtlFn*)p);
free(p);
}
return FALSE;
}
pid_t mainloop_popen_bgread(const char *cmd,
void (*initenv)(void *p), void *p,
ExtlFn handler, ExtlFn errhandler)
{
pid_t pid=-1;
int fd=-1, errfd=-1;
ExtlFn none=extl_fn_none();
pid=mainloop_do_spawn(cmd, initenv, p, NULL,
(handler!=none ? &fd : NULL),
(errhandler!=none ? &errfd : NULL));
if(pid>0){
if(handler!=none){
if(!mainloop_register_input_fd_extlfn(fd, handler))
goto err;
}
if(errhandler!=extl_fn_none()){
if(!mainloop_register_input_fd_extlfn(errfd, errhandler))
goto err;
}
}
return pid;
err:
if(fd>=0)
close(fd);
if(errfd>=0)
close(errfd);
return -1;
}
/*}}}*/
/*{{{ Misc. */
void cloexec_braindamage_fix(int fd)
{
fcntl(fd, F_SETFD, FD_CLOEXEC);
}
/*}}}*/
|