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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
|
/*
bosh system stuff (spawning processes etc)
$Id: system.c,v 1.62 2009/03/26 17:07:51 alexsisson Exp $
(C) Copyright 2004-2009 Alex Sisson (alexsisson@gmail.com)
This file is part of bosh.
bosh is free software; you can redistribute it and/or modify
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
bosh is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with bosh; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/stat.h>
#include <ctype.h>
#include <ncurses.h>
#include "system.h"
#include "bosh.h"
#include "misc.h"
#include "rc.h"
#ifdef DEBUG
int debug_leavetmp = 1;
#endif
#ifdef LOG
int log_read = 0;
#endif
static char *logprefix = NULL;
/*
* bosh_prepare_*
*
* these generate the script, and returns the array for execv
* for a particular shell/interpreter.
*
*/
static stray_t *bosh_prepare_bash(bosh_t *bosh, char *command, FILE *script) {
int n;
stray_t *execvarg;
char *p = NULL;
#ifdef LOG
p = malloc(32);
sprintf(p,"%s[bash] ",logprefix );
#endif
// fprintf(script,"shift\n"); /* skip secret $1 argument */
bosh_fprintfl(script,p,"BOSHVARFILE=%s\n",BOSHVARFILE);
bosh_fprintfl(script,p,"BOSHCONF=\"%s\"\n",conf);
bosh_fprintfl(script,p,"BOSHPID=%d\n",getpid());
bosh_fprintfl(script,p,"BOSHPPID=%d\n",getppid());
if(BOSHPARAM)
bosh_fprintfl(script,p,"BOSHPARAM=\"%s\"\n",BOSHPARAM);
if(bosh->title)
bosh_fprintfl(script,p,"BOSHTITLE=\"%s\"\n",bosh->title);
for(n=0;n<boshuservars;n++)
if(boshuservar[n])
bosh_fprintfl(script,p,"BOSHVAR%d=\"%s\"\n",n+1,boshuservar[n]);
bosh_fprintfl(script,p,"BOSHFUNCTION() {\n");
if(bosh->common)
bosh_fprintfl(script,p,"%s\n",bosh->common);
if(*bosh->tmpfpipe)
bosh_fprintfl(script,p,"cat %s | %s\n",bosh->tmpfpipe,command);
else
bosh_fprintfl(script,p,"%s\n",command);
bosh_fprintfl(script,p,"}\n");
bosh_fprintfl(script,p,"BOSHFUNCTION \"$@\"\n");
bosh_fprintfl(script,p,"BOSHRETURN=$?\n");
bosh_fprintfl(script,p,"echo \"$BOSHERR\" > $BOSHVARFILE\n");
bosh_fprintfl(script,p,"echo \"$BOSHTITLE\" >> $BOSHVARFILE\n");
for(n=0;n<boshuservars;n++)
bosh_fprintfl(script,p,"echo \"$BOSHVAR%d\" >> $BOSHVARFILE\n",n+1);
bosh_fprintfl(script,p,"exit $BOSHRETURN\n");
/* construct execv args */
execvarg = stray_new(STRAY_EMPTY);
stray_addstr_d(execvarg,"/bin/bash");
stray_addstr_d(execvarg,"--");
stray_addstr_d(execvarg,bosh->tmpfscript);
stray_addarr_d(execvarg,childarg);
return execvarg;
}
/*
static char **bosh_prepare_ruby(bosh_t *bosh, char *cmd, FILE *script) {
int n;
char **r;
bosh_fprintfl(script,logprefix,"BOSHVARFILE='%s'\n",BOSHVARFILE);
bosh_fprintfl(script,logprefix,"BOSHCONF='%s'\n",conf);
bosh_fprintfl(script,logprefix,"BOSHPID='%d'\n",getpid());
bosh_fprintfl(script,logprefix,"BOSHPPID='%d'\n",getppid());
bosh_fprintfl(script,logprefix,"bosherr=''\n");
if(BOSHPARAM)
bosh_fprintfl(script,logprefix,"BOSHPARAM='%s'\n",BOSHPARAM);
if(bosh->title)
bosh_fprintfl(script,logprefix,"boshtitle='%s'\n",bosh->title);
for(n=0;n<boshuservars;n++)
if(boshuservar[n])
bosh_fprintfl(script,logprefix,"boshvar%d='%s'\n",n+1,boshuservar[n]);
bosh_fprintfl(script,logprefix,"END { boshvarfile = File.new(BOSHVARFILE,'w'); boshvarfile.puts(bosherr); boshvarfile.puts(boshtitle); }");
if(cmd)
return NULL;
// bosh_fprintfl(script,logprefix,"cat %s | %s\n",bosh->temp,cmd);
else
bosh_fprintfl(script,logprefix,"%s\n",bosh->command);
r = malloc(sizeof(char*)*4);
r[0] = "/usr/bin/env";
r[1] = "ruby";
// r[2] = malloc(1024); sprintf(r[2],"cat %s | ruby %s %s",bosh->script,childargs);
r[3] = 0;
return r;
} */
/* * * * * * * * * * * * * * * * * * * * * * * * * *
*
* bosh_popen
*
* this is where a lot of the business gets done.
* never called directly, from interface, but from
* bosh_{open,pipe,action}.
*
* * * * * * * * * * * * * * * * * * * * * * * * * */
int bosh_popen(bosh_t *bosh, char *cmd, int cmdtype) {
int fd[2],parent[3],child[3],n,r=-1;
pid_t pid;
if(bosh->childpid) {
// return -1;
kill(bosh->childpid,15);
#ifdef LOG
bosh_log("popen: existing child process %d. kill() returned %s\n",bosh->childpid,errstr);
#endif
usleep(500);
}
/* check that std{in,out,err} are 0,1,2 */
if(0!=STDIN_FILENO || 1!=STDOUT_FILENO || 2!=STDERR_FILENO)
return -1;
/* memset our pipes to invalid fd's */
memset(parent,-1,sizeof(int)*3);
memset(child, -1,sizeof(int)*3);
/* create a pipe for each std stream */
for(n=0;n<=2;n++) {
if(pipe(fd))
goto pipecleanup;
if(n==0) {
/* stdin */
parent[n] = fd[1];
child [n] = fd[0];
} else {
/* stdout/err */
parent[n] = fd[0];
child [n] = fd[1];
}
}
strcpy(bosh->tmpfscript,"/tmp/boshs_XXXXXX");
close(mkstemp(bosh->tmpfscript));
#ifdef LOG
bosh_log("popen: forking...\n");
#endif
pid = fork();
/* PARENT */
if(pid>0) {
#ifdef LOG
bosh_log("popen: [parent] fork success: child pid \"%d\"\n",pid);
#endif
/* close child's ends of the pipes */
for(n=0;n<=2;n++)
close(child[n]);
/* see if child has died already due to an invalid command */
usleep(BOSH_PIPE_USLEEP);
if(pid==waitpid(pid,&n,WNOHANG))
if(WIFEXITED(n))
if(WEXITSTATUS(n)==127)
goto pipecleanup;
/* clear the buffer */
list_reinit(bosh->buf);
/* save child details */
bosh->childin = parent[0];
bosh->childout = parent[1];
bosh->childerr = parent[2];
bosh->childpid = pid;
bosh->exit = BOSH_EXIT_UNKNOWN;
if(cmdtype&BOSH_POPEN_WAIT) {
#ifdef LOG
bosh_log("popen: [parent] waiting for child pid \"%d\"\n",pid);
#endif
waitpid(bosh->childpid,0,0);
bosh_close(bosh);
}
return 0;
}
/* CHILD */
if(pid==0) {
FILE *f;
stray_t *a;
pid = getpid();
#ifdef LOG
bosh_log_open("a");
logprefix = malloc(32);
sprintf(logprefix,"popen: [child:%d]",pid);
#endif
/* reset signal handlers - not sure we need to do this: i was testing something */
signal(SIGTERM,SIG_DFL);
signal(SIGHUP,SIG_DFL);
//signal(SIGSEGV,SIG_DFL);
signal(SIGINT,SIG_DFL);
signal(SIGPIPE,SIG_DFL);
/* close parent's ends of the pipes, and dup our ends onto our std streams before closing them */
for(n=0;n<=2;n++) {
close(parent[n]);
if(!(cmdtype&BOSH_POPEN_NODUP)) {
dup2(child[n],n);
close(child[n]);
}
}
if(cmdtype & BOSH_POPEN_FILTER) {
/* we're doing a pipe - preserve current buffer to tmp file */
char *p;
strcpy(bosh->tmpfpipe,"/tmp/bosht_XXXXXX");
#ifdef LOG
bosh_log("%s created tmp pipe file: \"%s\"\n",logprefix,bosh->tmpfpipe);
#endif
f = fdopen(mkstemp(bosh->tmpfpipe),"w");
list_start(bosh->buf,0);
while(1) {
p = list_next(bosh->buf);
if(!p)
break;
fprintf(f,"%s\n",p);
}
fclose(f);
} else {
*bosh->tmpfpipe = 0;
if(!cmd)
cmd = bosh->command;
}
/* create and prepare tmpscript for the command/action */
f = fopen(bosh->tmpfscript,"w");
if(!f)
return -1;
#ifdef LOG
bosh_log("%s created script: \"%s\"\n",logprefix,bosh->tmpfscript);
#endif
a = bosh_prepare_bash(bosh,cmd,f);
fclose(f);
chmod(bosh->tmpfscript,0700); /* we should probably set the umask first */
#ifdef LOG
bosh_log("%s: execv args:\n",logprefix);
bosh_log_stray(a);
bosh_log("%s execv()ing...:\n",logprefix);
#endif
execv(a->v[0],a->v);
#ifdef LOG
bosh_log("%s execv failed!\n",logprefix);
#endif
/* exec failed if we get here - drop through to cleanup. */
/* or not - this pipecleanup business needs sorting */
_exit(1);
}
/* fork() failed */
pipecleanup:
for(n=0;n<=2;n++) {
close(parent[n]);
close(child[n]);
}
return r;
}
int bosh_open(bosh_t *bosh) {
return bosh_popen(bosh,NULL,0);
}
int bosh_pipe(bosh_t *bosh, char *command) {
return bosh_popen(bosh,command,BOSH_POPEN_FILTER);
}
/* * * * * * * * * * * * * * * * * * * * * * *
*
* bosh_action
*
* prepare $BOSH and action destination stuff
* and then run action.
*
* UGLY: needs refactoring
*
* * * * * * * * * * * * * * * * * * * * * * */
int bosh_action(bosh_t *bosh, int a) {
char *action=NULL,*seperator=NULL,*p;
int dest,i,n;
if(a==BOSH_ACTION_SPECIAL_SHOWCONF) { /* shov conf (^v) */
dest = BOSH_ACTION_OUTPUT_ADVANCE;
if(confpath) {
action = malloc(strlen(confpath)+7);
if(action)
sprintf(action,"cat '%s'",confpath);
}
if(!action)
return -1;
} else { /* normal action */
if(bosh->cursorsize>1) { /* need to handle multiline $BOSH */
seperator = bosh->multilineseperator ? bosh->multilineseperator : "";
for(n=0,i=0;i<bosh->cursorsize;i++) {
p = list_get(bosh->buf,bosh->offset+bosh->cursor+i);
n += strlen(p) + strlen(seperator);
}
BOSH = malloc(n);
if(!BOSH)
return -1;
*BOSH = 0;
for(i=0;i<bosh->cursorsize;i++) {
p = list_get(bosh->buf,bosh->offset+bosh->cursor+i);
sprintf(BOSH+strlen(BOSH),"%s%s",p,seperator);
}
}
else /* single line $BOSH */
BOSH = strdup(list_get(bosh->buf,bosh->offset+bosh->cursor));
if(bosh->preaction)
action = strdup2(bosh->preaction,bosh->action[a].command);
else
action = strdup(bosh->action[a].command);
dest = bosh->action[a].dest;
setenv("BOSH",BOSH,1);
if(BOSHPARAM)
setenv("BOSHPARAM",BOSHPARAM,1);
}
/* take appropriate action depending on dest */
switch(dest) {
case BOSH_ACTION_OUTPUT_OVERWRITE:
#ifdef LOG
bosh_log("action: overwrite\n");
#endif
//list_reinit(bosh->buf);
bosh->line = bosh->offset = bosh->cursor = 0;
bosh_popen(bosh,action,BOSH_POPEN_ACTION);
break;
case BOSH_ACTION_OUTPUT_ADVANCE:
#ifdef LOG
bosh_log("action: advance\n");
#endif
p = NULL;
/* all very hacky: */
if(bosh->common)
p = strdup(bosh->common);
bosh = bosh_t_new();
bosh->command = strdup(action);
bosh->common = p;
bosh_popen(bosh,NULL,BOSH_POPEN_ACTION);
if(list_items(boshlist)>1) {
bosh_t_free(list_get(boshlist,1));
list_del(boshlist,1); /* this needs to be corrected if we ever get actions spawning bosh configurations */
}
list_add(boshlist,bosh);
boshlistpos = 1;
break;
case BOSH_ACTION_OUTPUT_ENDCURSES:
#ifdef LOG
bosh_log("action: end curses\n");
#endif
endwin();
bosh_popen(bosh,action,BOSH_POPEN_ACTION|BOSH_POPEN_NODUP|BOSH_POPEN_WAIT);
if(bosh->refresh)
bosh_open(bosh);
bosh_redraw();
break;
case BOSH_ACTION_OUTPUT_NONE:
#ifdef LOG
bosh_log("action: none\n");
#endif
bosh_popen(bosh,action,BOSH_POPEN_ACTION|BOSH_POPEN_WAIT);
if(bosh->refresh)
bosh_open(bosh);
break;
default:
break;
}
free(action);
free(BOSH);
free(BOSHPARAM);
BOSH = NULL;
BOSHPARAM = NULL;
return 0;
}
/* * * * * * * * * * * * * * * * *
*
* bosh_read
*
* read from child process if data available
*
* returns non-zero if a new line is read
*
* * * * * * * * * * * * * * * * */
#define FD_ISVALID(fd) (fd>=0)
#define FD_ISVALIDANDSET(fd,fdset) (FD_ISVALID(fd)?FD_ISSET(fd,fdset):0)
#define FD_SETIFVALID(fd,fdset,n) if(FD_ISVALID(fd)) { FD_SET(fd,fdset); n = hof(n,fd); }
int bosh_read(bosh_t *bosh) {
static unsigned int readbuflen = 1024;
static unsigned int readbufpos = 0;
static unsigned char *readbuf = NULL;
fd_set fdsetr;
int n,r,loop=1;
unsigned char c;
if(!readbuf)
readbuf = malloc(readbuflen);
while(loop) {
struct timeval tv = {0,0};
loop = 0;
FD_ZERO(&fdsetr);
FD_SETIFVALID(bosh->childout,&fdsetr,n);
FD_SETIFVALID(bosh->childerr,&fdsetr,n);
if(select(n+1,&fdsetr,0,0,&tv)>0) {
n = -1;
if(FD_ISVALIDANDSET(bosh->childout,&fdsetr))
n = bosh->childout;
else if(FD_ISVALIDANDSET(bosh->childerr,&fdsetr))
n = bosh->childerr;
if(FD_ISVALID(n)) {
/* a child fd is set for reading */
if(read(n,&c,1)>0) {
/* got data */
#ifdef LOG
if(log_read)
bosh_log("read: fd[%d] buf[%d/%d] got %03d %c\n",n,readbufpos,readbuflen,c,isprint(c)?c:' ');
#endif
if(c=='\n'||c=='\r') {
readbuf[readbufpos] = 0;
if(c=='\r')
if(bosh->buf->items)
list_del(bosh->buf,bosh->buf->items-1);
list_adddup(bosh->buf,readbuf,readbufpos+1);
if(bosh->width<readbufpos)
bosh->width = readbufpos;
#ifdef LOG
if(log_read) {
readbuf[readbufpos] = 0;
bosh_log("read: line: %s\n",readbuf);
}
#endif
readbufpos = 0;
r = 1;
} else {
readbuf[readbufpos] = c;
readbufpos++;
if(readbufpos>=readbuflen) {
readbuflen *= 2;
readbuf = realloc(readbuf,readbuflen);
}
}
loop = 1;
} else {
/* eof or error */
#ifdef LOG
bosh_log("read: fd[%d] eof/error\n",n);
#endif
if(n==bosh->childout) {
bosh->childout = -1;
close(n);
}
else if(n==bosh->childerr) {
bosh->childerr = -1;
close(n);
}
if(bosh->childout==-1 && bosh->childerr==-1)
bosh_close(bosh);
}
}
}
}
return r;
}
int bosh_write(bosh_t *bosh, char c) {
return write(bosh->childin,&c,1);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * *
*
* bosh_close
*
* cleans up child process and reads user vars
*
* * * * * * * * * * * * * * * * * * * * * * * * * */
void bosh_close(bosh_t *bosh) {
int i;
FILE *b;
close(bosh->childin);
bosh->childin = -1;
if(bosh->childpid) {
/* need to reap child */
#ifdef LOG
bosh_log("close: reaping child \"%d\"\n",bosh->childpid);
#endif
waitpid(bosh->childpid,&i,0);
if(WIFEXITED(i)) {
bosh->exit = WEXITSTATUS(i);
#ifdef LOG
bosh_log("close: child returned %d\n",bosh->exit);
#endif
} else if(WIFSIGNALED(i)) {
bosh->exit = WTERMSIG(i) * -1;
#ifdef LOG
bosh_log("close: child terminated by signal %d\n",bosh->exit*-1);
#endif
}
bosh->childpid = 0;
}
bosh_unlink(bosh->tmpfscript);
/* get VARs back from child */
b = fopen(BOSHVARFILE,"r");
if(b) {
#ifdef LOG
bosh_log("close: reading var file \"%s\"\n",BOSHVARFILE);
#endif
BOSHERR = malloc(256);
fgets(BOSHERR,256,b);
strswp(BOSHERR,'\n',0);
if(strlen(BOSHERR)) {
/* child set BOSHERR */
fclose(b);
bosh_finish(bosh->exit);
}
free(BOSHERR);
BOSHERR = NULL;
/* read title */
free(bosh->title);
bosh->title = malloc(256);
fgets(bosh->title,256,b);
strswp(bosh->title,'\n',0);
if(!strlen(bosh->title)) {
free(bosh->title);
bosh->title = NULL;
}
#ifdef LOG
else
bosh_log("close: varfile: bosh->title \"%s\"\n",bosh->title);
#endif
/* read user vars */
for(i=0;i<boshuservars;i++) {
free(boshuservar[i]);
boshuservar[i] = malloc(256);
if(!fgets(boshuservar[i],256,b)) {
free(boshuservar[i]);
boshuservar[i] = NULL;
break;
}
strswp(boshuservar[i],'\n',0);
#ifdef LOG
bosh_log("close: varfile: BOSHVAR%d/uservar[%d] \"%s\"\n",i+1,i,boshuservar[i]);
#endif
if(!strlen(boshuservar[i])) {
free(boshuservar[i]);
boshuservar[i] = NULL;
}
}
fclose(b);
bosh_unlink(BOSHVARFILE);
bosh_unlink(bosh->tmpfpipe);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * *
*
* bosh_unlink
*
* unlink wrapper
*
* * * * * * * * * * * * * * * * * * * * * * * * * */
int bosh_unlink(const char *path) {
#ifdef DEBUG
if(debug_leavetmp) {
#ifdef LOG
bosh_log("unlink: left %s\n",path);
#endif
return 0;
} else {
#ifdef LOG
bosh_log("unlink: %s\n",path);
#endif
return unlink(path);
}
#else
#ifdef LOG
bosh_log("unlink: %s\n",path);
#endif
return unlink(path);
#endif
}
|