Example #1
0
int main(int argc,char **argv)
{
  struct stat st;

  dir = argv[1];
  if (!dir || argv[2])
    strerr_die1x(100,"supervise: usage: supervise dir");

  if (pipe(selfpipe) == -1)
    strerr_die4sys(111,FATAL,"unable to create pipe for ",dir,": ");
  coe(selfpipe[0]);
  coe(selfpipe[1]);
  ndelay_on(selfpipe[0]);
  ndelay_on(selfpipe[1]);

  sig_block(sig_child);
  sig_catch(sig_child,trigger);

  if (chdir(dir) == -1)
    strerr_die4sys(111,FATAL,"unable to chdir to ",dir,": ");

  if (stat("down",&st) != -1)
    flagwantup = 0;
  else
    if (errno != error_noent)
      strerr_die4sys(111,FATAL,"unable to stat ",dir,"/down: ");

  mkdir("supervise",0700);
  fdlock = open_append("supervise/lock");
  if ((fdlock == -1) || (lock_exnb(fdlock) == -1))
    strerr_die4sys(111,FATAL,"unable to acquire ",dir,"/supervise/lock: ");
  coe(fdlock);

  fifo_make("supervise/control",0600);
  fdcontrol = open_read("supervise/control");
  if (fdcontrol == -1)
    strerr_die4sys(111,FATAL,"unable to read ",dir,"/supervise/control: ");
  coe(fdcontrol);
  ndelay_on(fdcontrol); /* shouldn't be necessary */
  fdcontrolwrite = open_write("supervise/control");
  if (fdcontrolwrite == -1)
    strerr_die4sys(111,FATAL,"unable to write ",dir,"/supervise/control: ");
  coe(fdcontrolwrite);

  pidchange();
  announce();

  fifo_make("supervise/ok",0600);
  fdok = open_read("supervise/ok");
  if (fdok == -1)
    strerr_die4sys(111,FATAL,"unable to read ",dir,"/supervise/ok: ");
  coe(fdok);

  if (!flagwant || flagwantup) trystart();

  doit();
  announce();
  _exit(0);
}
static bool
fifo_check(struct fifo_data *fd, GError **error)
{
	struct stat st;

	if (stat(fd->path, &st) < 0) {
		if (errno == ENOENT) {
			/* Path doesn't exist */
			return fifo_make(fd, error);
		}

		g_set_error(error, fifo_output_quark(), errno,
			    "Failed to stat FIFO \"%s\": %s",
			    fd->path, strerror(errno));
		return false;
	}

	if (!S_ISFIFO(st.st_mode)) {
		g_set_error(error, fifo_output_quark(), 0,
			    "\"%s\" already exists, but is not a FIFO",
			    fd->path);
		return false;
	}

	return true;
}
Example #3
0
/* Opens a file descriptor and sets it's type, data and permissions*/
int fd_open_with_index (int fd, int type, void * data, int perms) {
	if(files_allocd == 0) {
		files_alloc();
	}
	if(!files[fd].used)		{
		files[fd].type = type;
		files[fd].data = data;
		files[fd].perms = perms;
		switch(files[fd].type) {
			case _FD_TTY:
			//
			break;
			case _FD_FIFO:
				files[fd].fifo_inode = (int) fs_open_fifo(data, current_ttyc()->pwd, perms);                    // Fifos are stored with their own id...
				if(files[fd].fifo_inode > 0)	{
					files[fd].data = (void *) fifo_make(files[fd].fifo_inode);
				}
			break;
			case _FD_FILE:
				files[fd].data = (void *)fs_open_reg_file(data, current_ttyc()->pwd, perms);             // Inodes are stoded with their inode number.

				if(files[fd].data > 0 && fs_is_fifo((int)files[fd].data))
				{
					files[fd].fifo_inode = (int) files[fd].data;
					files[fd].data = (void *) fifo_make(files[fd].fifo_inode);
					files[fd].type = _FD_FIFO;
				}
			break;
			default:
			return -1;
		}
	}
	
	// Errors...
	if((int)files[fd].fifo_inode < 0 && type == _FD_FIFO)	{
		return (int)files[fd].fifo_inode;
	}
	if((int)files[fd].data < 0 && type == _FD_FILE)	{
		return (int)files[fd].data;
	}
	files[fd].used++;
	return fd;
}
int ftrig1_make (ftrig1 *f, char const *path)
{
  ftrig1 ff = FTRIG1_ZERO ;
  unsigned int pathlen = str_len(path) ;
  int e = 0 ;
  char tmp[pathlen + 46 + FTRIG1_PREFIXLEN] ;
  
  byte_copy(tmp, pathlen, path) ;
  tmp[pathlen] = '/' ; tmp[pathlen+1] = '.' ;
  byte_copy(tmp + pathlen + 2, FTRIG1_PREFIXLEN, FTRIG1_PREFIX) ;
  tmp[pathlen + 2 + FTRIG1_PREFIXLEN] = ':' ;
  if (!timestamp(tmp + pathlen + 3 + FTRIG1_PREFIXLEN)) return 0 ;
  tmp[pathlen + 28 + FTRIG1_PREFIXLEN] = ':' ;
  if (random_name(tmp + pathlen + 29 + FTRIG1_PREFIXLEN, 16) < 16) return 0 ;
  tmp[pathlen + 45 + FTRIG1_PREFIXLEN] = 0 ;
  
  {
    mode_t m = umask(0) ;
    if (fifo_make(tmp, S_IRUSR|S_IWUSR|S_IWGRP|S_IWOTH) == -1)
    {
      umask(m) ;
      return 0 ;
    }
    umask(m) ;
  }

  if (!stralloc_catb(&ff.name, tmp, pathlen+1)) { e = errno ; goto err0 ; }
  if (!stralloc_catb(&ff.name, tmp + pathlen + 2, FTRIG1_PREFIXLEN + 44))
  {
    e = errno ; goto err1 ;
  }
  ff.fd = open_read(tmp) ;
  if (ff.fd == -1) { e = errno ; goto err1 ; }
  ff.fdw = open_write(tmp) ;
  if (ff.fdw == -1) { e = errno ; goto err2 ; }
  if (rename(tmp, ff.name.s) == -1) goto err3 ;
  *f = ff ;
  return 1 ;

 err3:
  e = errno ;
  fd_close(ff.fdw) ;
 err2:
  fd_close(ff.fd) ;
 err1:
  stralloc_free(&ff.name) ;
 err0:
  unlink(tmp) ;
  errno = e ;
  return 0 ;
}
Example #5
0
void main()
{
    int flagbug;
    struct timeval instant;
    fd_set rfds;

    flagbug = 0;
    if (fifo_make(FN,0600) != -1) {
        close(0);
        if (open_read(FN) == 0) {
            FD_ZERO(&rfds);
            FD_SET(0,&rfds);
            instant.tv_sec = instant.tv_usec = 0;
            if (select(1,&rfds,(fd_set *) 0,(fd_set *) 0,&instant) > 0)
                flagbug = 1;
        }
        unlink(FN);
    }
    _exit(!flagbug);
}
Example #6
0
int main(int argc, char **argv)
{
  int fdfifo, fdfifowrite;
  char *x;
  unsigned long id;

  VERSIONINFO;

  x = env_get("WORKDIR");
  if (!x)
    strerr_die2x(111, FATAL, "$WORKDIR not set");
  if (chdir(x) == -1)
    strerr_die4sys(111, FATAL, "unable to chdir to ", x, ": ");

  x = env_get("GID");
  if (!x)
    strerr_die2x(111, FATAL, "$GID not set");
  scan_ulong(x,&id);
  if (prot_gid((int) id) == -1)
    strerr_die2sys(111, FATAL, "unable to setgid: ");

  x = env_get("UID");
  if (!x)
    strerr_die2x(111, FATAL, "$UID not set");
  scan_ulong(x,&id);

  /* undocumented feature */
  if(id == 0)
    if(!env_get("IWANTTORUNASROOTANDKNOWWHATIDO"))
      strerr_die2x(111, FATAL, "unable to run under uid 0: please change $UID");

  if (prot_uid((int) id) == -1)
    strerr_die2sys(111, FATAL, "unable to setuid: ");

  buffer_putsflush(buffer_2, ARGV0 "starting\n");

  if(fifo_make(FIFONAME, 0620) == -1)
    strerr_warn4(ARGV0, "unable to create fifo ", FIFONAME, " ", &strerr_sys);

  fdfifo = open_read(FIFONAME);
  if(fdfifo == -1)
    strerr_die4sys(111, FATAL, "unable to open for read ", FIFONAME, " ");
  coe(fdfifo);
  ndelay_on(fdfifo); /* DJB says: shouldn't be necessary */

  /* we need this to keep the fifo from beeing closed */
  fdfifowrite = open_write(FIFONAME);
  if (fdfifowrite == -1)
    strerr_die4sys(111, FATAL, "unable to open for write ", FIFONAME, " ");
  coe(fdfifowrite);

  /* init a buffer for nonblocking reading */
  buffer_init(&wr, waitread, fdfifo, waitreadspace, sizeof waitreadspace);

  t = dAVLAllocTree();

  /* read snapshot of dnsdatatree */ 
  fill_db();

  /* SIGALRM can be used to check if dumping the database is needed */
  sig_catch(sig_alarm, sigalrm);

  /* SIGHUP can be used to force dumping the database */
  sig_catch(sig_hangup, sighup);  

  /* check if out child is done */
  sig_catch(sig_child, sigchld);  

  // XXX SIGINT, SIGTERM,

  /* do our normal workloop */
  doit();

  /* we shouldn't get here */
  return 1;
}
Example #7
0
int main(int argc, char **argv)
{
	struct sigaction sa;

	if (parse_argv(argc, argv) < 0)
		_exit(100);

	snprintf(status_files[0], 1024, "%s/lock", status_dir);
	snprintf(status_files[1], 1024, "%s/control", status_dir);
	snprintf(status_files[2], 1024, "%s/ok", status_dir);
	snprintf(status_files[3], 1024, "%s/status", status_dir);
	snprintf(status_files[4], 1024, "%s/status.new", status_dir);
	snprintf(status_files[5], 1024, "%s/supervise.log", status_dir);
	snprintf(status_files[6], 1024, "%s/supervise.log.wf", status_dir);

	sa.sa_handler = SIG_IGN;
	sigemptyset(&sa.sa_mask);
	if (sigaction(SIGHUP, &sa, NULL) < 0)
	{
		printf("unable to ignore SIGHUP for %s\n", service);  
		_exit(110);
	}

	if (mkdir(status_dir, 0700) < 0 && errno !=  EEXIST)
	{
		printf("unable to create dir: %s\n", status_dir);  
		_exit(110);
	}

	fdlog = open_append(status_files[5]);
	if (fdlog == -1)
	{
		printf("unable to open %s%s", status_dir, "/supervise.log");
		_exit(111);
	}
	coe(fdlog);

	fdlogwf = open_append(status_files[6]);
	if (fdlogwf == -1)
	{
		printf("unable to open %s%s", status_dir, "/supervise.log.wf");
		_exit(1);
	}
	coe(fdlogwf);

	if (daemon(1, 0) < 0)
	{
		printf("failed to daemonize supervise!\n");
		_exit(111);
	}

	if (pipe(selfpipe) == -1)
	{
		write_log(fdlogwf, FATAL, "unable to create pipe for ", service, "\n");
		_exit(111);
	}
	coe(selfpipe[0]);
	coe(selfpipe[1]);
	ndelay_on(selfpipe[0]);
	ndelay_on(selfpipe[1]);

	sig_block(sig_child);
	sig_catch(sig_child, trigger);

	sig_block(sig_alarm);
	sig_catch(sig_alarm, timer_handler);
	sig_unblock(sig_alarm);

	fdlock = open_append(status_files[0]);
	if ((fdlock == -1) || (lock_exnb(fdlock) == -1))
	{
		write_log(fdlogwf, FATAL, "Unable to acquier ", status_dir, "/lock\n");
		_exit(111);
	}
	coe(fdlock);

	fifo_make(status_files[1], 0600);
	fdcontrol = open_read(status_files[1]);
	if (fdcontrol == -1)
	{
		write_log(fdlogwf, FATAL, "unable to read ", status_dir, "/control\n");
		_exit(1);
	}
	coe(fdcontrol);
	ndelay_on(fdcontrol);

	fdcontrolwrite = open_write(status_files[1]);
	if (fdcontrolwrite == -1)
	{
		write_log(fdlogwf, FATAL, "unable to write ", status_dir, "/control\n");
		_exit(1);
	}
	coe(fdcontrolwrite);

	fifo_make(status_files[2], 0600);
	fdok = open_read(status_files[2]);
	if (fdok == -1)
	{
		write_log(fdlogwf, FATAL, "unable to read ", status_dir, "/ok\n");
		_exit(1);
	}
	coe(fdok);
	
	if (!restart_sh[0])
	{
		parse_conf(); 
	}
	pidchange();
	announce();

	if (!flagwant || flagwantup)
		trystart();
	doit();
	announce();

	_exit(0);
}
Example #8
0
int main(int argc,char * const *argv) {
  int opt;
  const char *path;
  mode_t m;
  iopause_fd x;
  struct taia deadline;
  struct taia stamp;
  int pid = 0;
  char ch;
  int wstat;

  while ((opt = getopt(argc,argv,"wWdDt:")) != opteof)
    switch(opt) {
      case 't': scan_uint(optarg,&timeout); break;
      case 'w': flagwait = 1; break;
      case 'W': flagwait = 0; break;
      case 'd': flagdelete = 1; break;
      case 'D': flagdelete = 0; break;
      default: usage();
    }

  argv += optind;
  path = *argv++;
  if (!path) usage();

  if (flagdelete) {
    m = umask(0);
    unlink(path);
    fifo_make(path,0666);
    umask(m);
  }

  fdr = open_read(path);
  if (fdr == -1)
    strerr_die4sys(111,FATAL,"cannot read ",path,": ");
  ndelay_on(fdr);

  fdw = open_write(path);
  if (fdw == -1)
    strerr_die4sys(111,FATAL,"cannot write ",path,": ");

  if (*argv) {
    pid = fork();
    switch (pid) {
      case 0:
	close(fdr); close(fdw);
	pathexec(argv);
	strerr_die4sys(111,FATAL,"cannot run ",*argv,": ");
      case -1:
	strerr_die4sys(111,FATAL,"cannot fork ",*argv,": ");
	break;
      default:
	break;
    }
  }
  else
    flagwait = 0;

  x.fd = fdr;
  x.events = IOPAUSE_READ;
  taia_now(&stamp);
  taia_uint(&deadline,timeout);
  taia_add(&deadline,&stamp,&deadline);

  for (;;) {
    taia_now(&stamp);
    iopause(&x,1,&deadline,&stamp);
    if (x.revents) {
      waiting = 0;
      break;
    }
    if (taia_less(&deadline,&stamp)) {
      waiting = 99;
      break;
    }
  }

  if (!waiting)
    if (read(fdr,&ch,1) != 1)
      waiting = 0;

  if (flagwait)
    if (wait_pid(&wstat,pid) != pid)
      strerr_die2x(111,FATAL,"cannot reap child process");

  _exit(waiting);
}