Example #1
0
void
cerebrod_daemon_init(void)
{
  /* Based on code in Unix network programming by R. Stevens */
  pid_t pid;
  int i;
 
  pid = Fork();
  if (pid != 0)			/* Terminate Parent */
    exit(0);
 
  setsid();
 
  Signal(SIGHUP, SIG_IGN);
   
  pid = Fork();
  if (pid != 0)			/* Terminate 1st Child */
    exit(0);

  Chdir("/");
 
  Umask(0);
   
  /* Don't use Close() wrapper, we don't want to exit on error */
  for (i = 0; i < 64; i++)
    close(i);			
}
Example #2
0
/* applies to fd all options belonging to phase */
int applyopts_named(const char *filename, struct opt *opts, unsigned int phase) {
   struct opt *opt;

   if (!opts)  return 0;

   opt = opts; while (opt->desc != ODESC_END) {
      if (opt->desc == ODESC_DONE ||
	  opt->desc->phase != phase && phase != PH_ALL ||
	  !(opt->desc->group & GROUP_NAMED)) {
	 ++opt; continue; }
      switch (opt->desc->optcode) {
      case OPT_GROUP_EARLY:
      case OPT_GROUP:
	 if (Chown(filename, -1, opt->value.u_gidt) < 0) {
	    Error3("chown(\"%s\", -1, "F_gid"): %s", filename,
		   opt->value.u_gidt, strerror(errno));
	 }
	 break;
      case OPT_USER_EARLY:
      case OPT_USER:
	 if (Chown(filename, opt->value.u_uidt, -1) < 0) {
	    Error3("chown(\"%s\", "F_uid", -1): %s", filename,
		   opt->value.u_uidt, strerror(errno));
	 }
	 break;
      case OPT_PERM_EARLY:
      case OPT_PERM:
	 if (Chmod(filename, opt->value.u_modet) < 0) {
	    Error3("chmod(\"%s\", "F_mode"): %s",
		   filename, opt->value.u_modet, strerror(errno));
	 }
	 break;
      case OPT_UNLINK_EARLY:
      case OPT_UNLINK:
      case OPT_UNLINK_LATE:
	 if (Unlink(filename) < 0) {
	    if (errno == ENOENT) {
	       Warn2("unlink(\"%s\"): %s", filename, strerror(errno));
	    } else {
	       Error2("unlink(\"%s\"): %s", filename, strerror(errno));
	    }
	 }
	 break;
      case OPT_UMASK:
	 if (Umask(opt->value.u_modet) < 0) {
	    /* linux docu says it always succeeds, but who believes it? */
	    Error2("umask("F_mode"): %s", opt->value.u_modet, strerror(errno));
	 }
	 break;
      default: Error1("applyopts_named(): option \"%s\" not implemented",
		      opt->desc->defname);
	 break;
      }
      opt->desc = ODESC_DONE;
      ++opt;
   }
   return 0;
}