Exemple #1
0
void f_init(char **script)
{
  int i;
  int fd;

  for (i = 0;script[i];++i)
    ;

  f = (int *) alloc(i * sizeof(*f));
  if (!f) strerr_die2x(111,FATAL,"out of memory");

  for (i = 0;script[i];++i) {
    fd = -1;
    if (script[i][0] == '=') {
      if (fchdir(fdstartdir) == -1)
        strerr_die2sys(111,FATAL,"unable to switch to starting directory: ");
      fd = open_append(script[i] + 1);
      if (fd == -1)
        strerr_die4sys(111,FATAL,"unable to create ",script[i] + 1,": ");
      close(fd);
      fd = open_write(script[i] + 1);
      if (fd == -1)
        strerr_die4sys(111,FATAL,"unable to write ",script[i] + 1,": ");
      coe(fd);
    }
    f[i] = fd;
  }
}
void write_header_SD(void)
{
	ioport_set_pin_level(LED_SD_USE, IOPORT_PIN_LEVEL_LOW); //accendo led esterno
	if(open_append(&log_file_object, (char const *)file_name))
		terminate("Error opening logfile!");
	//scrivo intestazione file di log a seconda del modello scelto
	switch(model)
	{	
		case log:
			f_printf(&log_file_object, "Model:%s Interval:%ds  Vout:%dmV\r\n", modelStrings[model], interval, log_vout);
			break;
		case bat:
			f_printf(&log_file_object, "Model:%s Interval:%ds Vmax:%dmV Vexp:%dmV Qexp:%dmAh Vnom:%dmV Qnom:%dmAh Qtot:%dmAh Rint:%dmOhm Isam:%dmAh\r\n", 
					modelStrings[model], interval, bat_vmax, bat_vexp, bat_qexp, bat_vnom, bat_qnom, bat_qtot, bat_rint, bat_isam);
			sprintf(str, "Calculated values:E0:%lf K:%lf A:%lf B:%lf\r\n", bat_param[0], bat_param[1], bat_param[2], bat_param[3]);
			f_printf(&log_file_object, "%s", str);
			break;
	/*	case phv:
			//impl
			break;
		case tec:
			//impl
			break;
	*/
	}
	f_printf(&log_file_object, "Data Ora Tensione(mV) Corrente(uA)\r\n");
	ioport_set_pin_level(LED_SD_USE, IOPORT_PIN_LEVEL_HIGH); //spengo led esterno
}
Exemple #3
0
int wtmp_logout(const char *line) {
  int fd;
  int len;
  struct stat st;
  uw_tmp ut;

  if ((fd = open_append(UW_TMP_WFILE)) == -1)
    strerr_die4sys(111, FATAL, "unable to open ", UW_TMP_WFILE, ": ");
  if (lock_ex(fd) == -1)
    strerr_die4sys(111, FATAL, "unable to lock ", UW_TMP_WFILE, ": ");

  if (fstat(fd, &st) == -1) {
    close(fd);
    return(-1);
  }
  memset(&ut, 0, sizeof(uw_tmp));
  if ((len =str_len(line)) > sizeof ut.ut_line) len =sizeof ut.ut_line -2;
  byte_copy(ut.ut_line, len, line);
  if (time(&ut.ut_time) == -1) {
    close(fd);
    return(-1);
  }
#ifdef DEAD_PROCESS
  ut.ut_type =DEAD_PROCESS;
#endif
  if (write(fd, &ut, sizeof(uw_tmp)) != sizeof(uw_tmp)) {
    ftruncate(fd, st.st_size);
    close(fd);
    return(-1);
  }
  close(fd);
  return(1);
}
Exemple #4
0
void logaddr(const char *subdir,const char *event,
	     const char *addr,const char *comment)
{
  char ch;
  int fd;

  stralloc_copyb(&line,num,fmt_ulong(num,(unsigned long) now()));
  stralloc_cats(&line," ");
  stralloc_cats(&line,event);
  stralloc_cats(&line," ");
  while ((ch = *addr++) != 0) {
    if ((ch < 33) || (ch > 126)) ch = '?';
    stralloc_append(&line,ch);
  }
  if (comment && *comment) {
    stralloc_cats(&line," ");
    while ((ch = *comment++) != 0) {
      if (ch == '\t')
        ch = ' ';
      else 
        if ((ch < 32) || (ch > 126)) ch = '?';
      stralloc_append(&line,ch);
    }
  }
  stralloc_cats(&line,"\n");

  makepath(&fn,subdir,"/Log",0);
  fd = open_append(fn.s);
  if (fd == -1) return;
  substdio_fdbuf(&ss,write,fd,NULL,0);
  substdio_putflush(&ss,line.s,line.len);
  close(fd);
  return;
}
Exemple #5
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);
}
Exemple #6
0
void slock(const char *f, unsigned int d, unsigned int x) {
  int fd;

  if ((fd =open_append(f)) == -1) slock_die("unable to open lock", f, x);
  if (d) {
    if (lock_ex(fd) == -1) slock_die("unable to lock", f, x);
    return;
  }
  if (lock_exnb(fd) == -1) slock_die("unable to lock", f, x);
}
Exemple #7
0
int lockfile(const char *path)
{
  int fd;
  fd = open_append(path);
  if (fd == -1)
    strerr_die4sys(111,FATAL,ERR_OPEN,path,": ");
  if (lock_ex(fd) == -1)
    strerr_die4sys(111,FATAL,ERR_OBTAIN,path,": ");
  return fd;
}
void write_record_syslog(char *string)
{
	
	if(open_append(&syslog_file_object, (char const *)SYSLOG_FILE))//apro il file
		terminate("Errore di apertura syslog file !\r\n");	
	//scrivo su file syslog
	f_printf(&syslog_file_object, "%02d/%02d/%04d %02d:%02d:%02d %s\r\n", day, month, year, hour, minute, second, string);
	//scrivo su seriale
	printf("%02d/%02d/%04d %02d:%02d:%02d %s\r\n", day, month, year, hour, minute, second, string);
	f_close(&syslog_file_object); //chiudo file
}
Exemple #9
0
int main() {
  int fd;
  
  fd = open_append("test.txt");
  if (fd == -1)
    return 1;
    
  if (flock_exnb(fd) == -1)
    return 2;
    
  return 0;
}
Exemple #10
0
static void write_log(int fd, int level, const char *info0, const char *info1, const char *info2)
{
	struct stat st;
	int fdx, ret;
	char tmp[2048];
	char timestr[128];
	char loglevel[3][16] = { "NOTICE", "WARNING", "FATAL" };
	if (fd == fdlog)
	{
		if ((ret = stat(status_files[5], &st)) < 0 || st.st_size > 1800000000)
		{
			close(fdlog);
			if (ret == 0)
				unlink(status_files[5]);
			fdlog = open_append(status_files[5]);
			coe(fdlog);
		}
		fdx = fdlog;
	}
	else if (fd == fdlogwf)
	{
		if ((ret = stat(status_files[6], &st)) < 0 || st.st_size > 1800000000)
		{
			close(fdlogwf);
			if (ret == 0)
				unlink(status_files[6]);
			fdlogwf = open_append(status_files[6]);
			coe(fdlogwf);
		}
		fdx = fdlogwf;
	}
	else
		return;
	bzero(timestr, 128);
	gettimenow(timestr, 128);
	snprintf(tmp, 2048, "[%s] %s %s%s%s", level >= 0 && level < 3 ? loglevel[level] : "", timestr, info0, info1, info2);
	write(fdx, tmp, strlen(tmp));
}
Exemple #11
0
char File_Write_Append(char* filename, int length, char *Input_Array){ 
	  
	  printf("\n opening file (%s) for %d \n", filename, length);
	  
    errCode = open_append(&file, filename);
    Fat_FS_Error(errCode,"f_open");
    
    errCode = f_write(&file, Input_Array, length , &bw);
    Fat_FS_Error(errCode,"f_write");

    
    f_close(&file); 
    Fat_FS_Error(errCode,"f_close"); 
}
void open_log_file(void)
{
	//nome del file di log
	sprintf(file_name, "%s_%04d-%02d-%02d_%02d-%02d-%02d.log", modelStrings[model], year, month, day, hour, minute, second);
	//apro file log
	if (open_append(&log_file_object, (char const *)file_name) != FR_OK)
	{
		sprintf(str,"Errore creazione file %s! [ERROR]", file_name);
		terminate(str);
	}
	else
	{
		sprintf(str,"Creato file %s [OK]", file_name);
		write_record_syslog(str);
	}
}
Exemple #13
0
int main (void)
{
    FRESULT fr;
    FIL fil;


    f_mount(0, &Fatfs);

    /* Open or create a log file and ready to append */
    fr = open_append(&fil, "logfile.txt");
    if (fr != FR_OK) return 1;

    /* Append a line */
    f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min);

    /* Close the file */
    f_close(&fil);

    return 0;
}
Exemple #14
0
int main(int argc,char * const *argv) {
  int r;
  int fd;
  const char *file;

  file = *++argv;
  if (!file) usage();

  fd = open_append(file);
  if (fd == -1)
    strerr_die4sys(111,FATAL,"cannot open ",file,": ");
  if (ndelay_on(fd) == -1)
    strerr_die4sys(111,FATAL,"cannot control ",file,": ");
  sig_ignore(sig_pipe);
  r = write(fd,"",1);
  if (r != -1) _exit(0);
  if (errno == error_again) _exit(0);
  if (errno == error_pipe) _exit(0);
  strerr_die4sys(111,FATAL,"cannot write ",file,": ");
}
Exemple #15
0
static void show_config()
{
	int i, len;
	struct stat st;
	char buf[4096];
	if (stat(status_files[5], &st) < 0)
	{
		close(fdlog);
		fdlog = open_append(status_files[5]);
		coe(fdlog);
	}
	bzero(buf, 4096);
	snprintf(buf, 4096, "alarm_interval: %u\nmax_tries: %d\nmax_tries_if_coredumped: %d\nalarm_mail : %s\ngsm_list_len: %d\n",
			alarm_interval, max_tries, max_tries_if_coredumped, alarm_mail, gsm_list_len);
	for (i = 0; i < gsm_list_len; i++)
	{
		len = strlen(buf);
		snprintf(buf + len, 4096 - len, "alarm_gsm[%d]: %s\n", i, alarm_gsm[i]);
	}
	write(fdlog, buf, strlen(buf));
}
Exemple #16
0
int main()
{
  buffer bin;
  buffer bout;
  struct prioq_elt pe;
  int fdoldmbox;
  int fdnewmbox;
  int fd;
  int match;
  int fdlock;

  umask(077);

  mbox = env_get("MAIL");
  if (!mbox) strerr_die2x(111,FATL,"MAIL not set");
  mboxtmp = env_get("MAILTMP");
  if (!mboxtmp) strerr_die2x(111,FATL,"MAILTMP not set");

  if (maildir_chdir() == -1)
    strerr_die1(111,FATAL,&maildir_chdir_err);
  maildir_clean(&filenames);
  if (maildir_scan(&pq,&filenames,1,1) == -1)
    strerr_die1(111,FATAL,&maildir_scan_err);

  if (!prioq_min(&pq,&pe)) _exit(0); /* nothing new */

  fdlock = open_append(mbox);
  if (fdlock == -1)
    strerr_die4sys(111,FATL,"unable to lock ",mbox,": ");
  if (lock_ex(fdlock) == -1)
    strerr_die4sys(111,FATL,"unable to lock ",mbox,": ");

  fdoldmbox = open_read(mbox);
  if (fdoldmbox == -1)
    strerr_die4sys(111,FATL,"unable to read ",mbox,": ");

  fdnewmbox = open_trunc(mboxtmp);
  if (fdnewmbox == -1)
    strerr_die4sys(111,FATL,"unable to create ",mboxtmp,": ");

  buffer_init(&bin,read,fdoldmbox,inbuf,sizeof(inbuf));
  buffer_init(&bout,write,fdnewmbox,outbuf,sizeof(outbuf));

  switch(buffer_copy(&bout,&bin))
  {
    case -2: strerr_die4sys(111,FATL,"unable to read ",mbox,": ");
    case -3: strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  }

  while (prioq_min(&pq,&pe))
  {
    prioq_delmin(&pq);
    if (!prioq_insert(&pq2,&pe)) die_nomem();

    fd = open_read(filenames.s + pe.id);
    if (fd == -1)
      strerr_die4sys(111,FATL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");
    buffer_init(&bin,read,fd,inbuf,sizeof(inbuf));

    if (getln(&bin,&line,&match,'\n') != 0)
      strerr_die4sys(111,FATL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");

    if (!stralloc_copys(&ufline,"From XXX ")) die_nomem();
    if (match)
      if (stralloc_starts(&line,"Return-Path: <"))
      {
        if (line.s[14] == '>')
    {
          if (!stralloc_copys(&ufline,"From MAILER-DAEMON ")) die_nomem();
    }
        else
        {
      int i;
         if (!stralloc_ready(&ufline,line.len)) die_nomem();
         if (!stralloc_copys(&ufline,"From ")) die_nomem();
      for (i = 14;i < line.len - 2;++i)
        if ((line.s[i] == ' ') || (line.s[i] == '\t'))
          ufline.s[ufline.len++] = '-';
        else {
          ufline.s[ufline.len++] = line.s[i];
          if (!stralloc_cats(&ufline," ")) die_nomem();
          }
        }
      }
    if (!stralloc_cats(&ufline,myctime(pe.dt))) die_nomem();
    if (buffer_put(&bout,ufline.s,ufline.len) == -1)
      strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");

    while (match && line.len)
    {
      if (gfrom(line.s,line.len))
        if (buffer_puts(&bout,">") == -1)
          strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
      if (buffer_put(&bout,line.s,line.len) == -1)
        strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
      if (!match)
      {
        if (buffer_puts(&bout,"\n") == -1)
          strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
        break;
      }
      if (getln(&bin,&line,&match,'\n') != 0)
        strerr_die4sys(111,FATL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");
    }
    if (buffer_puts(&bout,"\n"))
      strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");

   close(fd);
  }

  if (buffer_flush(&bout) == -1)
    strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  if (fsync(fdnewmbox) == -1)
    strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  if (close(fdnewmbox) == -1) /* NFS dorks */
    strerr_die4sys(111,FATL,"unable to write to ",mboxtmp,": ");
  if (rename(mboxtmp,mbox) == -1)
    strerr_die6(111,FATL,"unable to move ",mboxtmp," to ",mbox,": ",&strerr_sys);

  while (prioq_min(&pq2,&pe))
  {
    prioq_delmin(&pq2);
    if (unlink(filenames.s + pe.id) == -1)
      strerr_warn4(WARNING,"$MAILDIR/",filenames.s + pe.id," will be delivered twice; unable to unlink: ",&strerr_sys);
  }

  return(0);
}
Exemple #17
0
void restart(struct cyclog *d)
{
  struct stat st;
  int fd;
  int flagprocessed;

  if (fchdir(fdstartdir) == -1)
    strerr_die2sys(111,FATAL,"unable to switch to starting directory: ");

  mkdir(d->dir,0700);
  d->fddir = open_read(d->dir);
  if ((d->fddir == -1) || (fchdir(d->fddir) == -1))
    strerr_die4sys(111,FATAL,"unable to open directory ",d->dir,": ");
  coe(d->fddir);

  d->fdlock = open_append("lock");
  if ((d->fdlock == -1) || (lock_exnb(d->fdlock) == -1))
    strerr_die4sys(111,FATAL,"unable to lock directory ",d->dir,": ");
  coe(d->fdlock);

  if (stat("current",&st) == -1) {
    if (errno != error_noent)
      strerr_die4sys(111,FATAL,"unable to stat ",d->dir,"/current: ");
  }
  else
    if (st.st_mode & 0100) {
      fd = open_append("current");
      if (fd == -1)
        strerr_die4sys(111,FATAL,"unable to append to ",d->dir,"/current: ");
      if (fchmod(fd,0644) == -1)
        strerr_die4sys(111,FATAL,"unable to set mode of ",d->dir,"/current: ");
      coe(fd);
      d->fdcurrent = fd;
      d->bytes = st.st_size;
      return;
    }

  unlink("state");
  unlink("newstate");

  flagprocessed = 0;
  if (stat("processed",&st) == -1) {
    if (errno != error_noent)
      strerr_die4sys(111,FATAL,"unable to stat ",d->dir,"/processed: ");
  }
  else if (st.st_mode & 0100)
    flagprocessed = 1;

  if (flagprocessed) {
    unlink("previous");
    finish(d,"processed","s");
  }
  else {
    unlink("processed");
    finish(d,"previous","u");
  }

  finish(d,"current","u");

  fd = open_trunc("state");
  if (fd == -1)
    strerr_die4sys(111,FATAL,"unable to write to ",d->dir,"/state: ");
  close(fd);
  fd = open_append("current");
  if (fd == -1)
    strerr_die4sys(111,FATAL,"unable to write to ",d->dir,"/current: ");
  if (fchmod(fd,0644) == -1)
    strerr_die4sys(111,FATAL,"unable to set mode of ",d->dir,"/current: ");
  coe(fd);
  d->fdcurrent = fd;
  d->bytes = 0;
}
Exemple #18
0
void fullcurrent(struct cyclog *d)
{
  int fd;
  int pid;
  int wstat;

  while (fchdir(d->fddir) == -1)
    pause3("unable to switch to ",d->dir,", pausing: ");

  while (fsync(d->fdcurrent) == -1)
    pause3("unable to write ",d->dir,"/current to disk, pausing: ");
  close(d->fdcurrent);

  while (rename("current","previous") == -1)
    pause3("unable to rename current to previous in directory ",d->dir,", pausing: ");
  while ((d->fdcurrent = open_append("current")) == -1)
    pause3("unable to create ",d->dir,"/current, pausing: ");
  coe(d->fdcurrent);
  d->bytes = 0;
  while (fchmod(d->fdcurrent,0644) == -1)
    pause3("unable to set mode of ",d->dir,"/current, pausing: ");

  while (chmod("previous",0744) == -1)
    pause3("unable to set mode of ",d->dir,"/previous, pausing: ");

  if (!d->processor)
    finish(d,"previous","s");
  else {
    for (;;) {
      while ((pid = fork()) == -1)
        pause3("unable to fork for processor in ",d->dir,", pausing: ");
      if (!pid) {
        startprocessor(d);
        strerr_die4sys(111,FATAL,"unable to run ",d->processor,": ");
      }
      if (wait_pid(&wstat,pid) == -1)
        pause3("wait failed for processor in ",d->dir,", pausing: ");
      else if (wait_crashed(wstat))
        pause3("processor crashed in ",d->dir,", pausing: ");
      else if (!wait_exitcode(wstat))
        break;
      strerr_warn4(WARNING,"processor failed in ",d->dir,", pausing",0);
      deepsleep(5);
    }

    while ((fd = open_append("processed")) == -1)
      pause3("unable to create ",d->dir,"/processed, pausing: ");
    while (fsync(fd) == -1)
      pause3("unable to write ",d->dir,"/processed to disk, pausing: ");
    while (fchmod(fd,0744) == -1)
      pause3("unable to set mode of ",d->dir,"/processed, pausing: ");
    close(fd);

    while ((fd = open_append("newstate")) == -1)
      pause3("unable to create ",d->dir,"/newstate, pausing: ");
    while (fsync(fd) == -1)
      pause3("unable to write ",d->dir,"/newstate to disk, pausing: ");
    close(fd);

    while (unlink("previous") == -1)
      pause3("unable to remove ",d->dir,"/previous, pausing: ");
    while (rename("newstate","state") == -1)
      pause3("unable to rename newstate to state in directory ",d->dir,", pausing: ");
    finish(d,"processed","s");
  }
}
Exemple #19
0
void CreateFileAppendModeAccordingly(char *dirPath, int min) {
  int mMin;
  mMin = min / 3;
  
  char filePath[31];
  strcpy(filePath, dirPath);
  printf("\r\nopen_append(&fsrc, filePath) : ");
  
  switch(mMin) {
  case 0:
    strcat(filePath, "/EQLOG1.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG1.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG1.txt already exist");
    }
    break;
  case 1:
    strcat(filePath, "/EQLOG2.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG2.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG2.txt already exist");
    }
    break;
  case 2:
    strcat(filePath, "/EQLOG3.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG3.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG3.txt already exist");
    }
    break;
  case 3:
    strcat(filePath, "/EQLOG4.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG4.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG4.txt already exist");
    }
    break;
  case 4:
    strcat(filePath, "/EQLOG5.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG5.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG5.txt already exist");
    }
    break;
  case 5:
    strcat(filePath, "/EQLOG6.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG6.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG6.txt already exist");
    }
    break;
  case 6:
    strcat(filePath, "/EQLOG7.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG7.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG7.txt already exist");
    }
    break;
  case 7:
    strcat(filePath, "/EQLOG8.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG8.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG8.txt already exist");
    }
    break;
  case 8:
    strcat(filePath, "/EQLOG9.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG9.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG9.txt already exist");
    }
    break;
  case 9:
    strcat(filePath, "/EQLOG10.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG10.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG10.txt already exist");
    }
    break;
  case 10:
    strcat(filePath, "/EQLOG11.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG11.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG11.txt already exist");
    }
    break;
  case 11:
    strcat(filePath, "/EQLOG12.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG12.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG12.txt already exist");
    }
    break;
  case 12:
    strcat(filePath, "/EQLOG13.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG13.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG13.txt already exist");
    }
    break;
  case 13:
    strcat(filePath, "/EQLOG14.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG14.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG14.txt already exist");
    }
    break;
  case 14:
    strcat(filePath, "/EQLOG15.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG15.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG15.txt already exist");
    }
    break;
  case 15:
    strcat(filePath, "/EQLOG16.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG16.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG16.txt already exist");
    }
    break;
  case 16:
    strcat(filePath, "/EQLOG17.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG17.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG17.txt already exist");
    }
    break;
  case 17:
    strcat(filePath, "/EQLOG18.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG18.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG18.txt already exist");
    }
    break;
  case 18:
    strcat(filePath, "/EQLOG19.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG19.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG19.txt already exist");
    }
    break;
  case 19:
    strcat(filePath, "/EQLOG20.TXT");
    // Create log file on the drive 0
    res = open_append(&fsrc, filePath);
    FPrintFatResult(res);
    if(res == FR_OK) {
      // Write buffer to file
      //res = f_write(&fsrc, HEADER, sizeof(HEADER), &br);
      
      printf("\r\nEQLOG20.txt successfully created");
      
      // Close file
      //f_close(&fsrc);
      
    } else if ( res == FR_EXIST ) {
      printf("EQLOG20.txt already exist");
    }
    break;
  }
}
Exemple #20
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);
}
Exemple #21
0
int main (int argc, char* argv[])
{
  char ip[4];
  stralloc partial = {0};
  stralloc out = {0};
  char ip_fmt[IP4_FMT];
  char line[BUF_LEN];
  int r, i, j;
  int inbuflen = 0;
  int flag0 = 1;
  buffer sslist;
  int fdlist = 0;
  char outlist[BUF_LEN];
  unsigned long skip = 0;
  int opt;

  while ((opt = getopt (argc, argv, "s:")) != opteof)
    switch (opt) {
      case 's': scan_ulong (optarg, &skip); break;
      default:  usage ();
    }

  argc -= optind;
  argv += optind;

  if (!argc) usage ();

  fdlist = open_append(argv[0]);
  if (fdlist == -1) 
    strerr_die4sys (111,FATAL,"unable to write ",argv[0],": ");
  buffer_init (&sslist,write,fdlist,outlist,sizeof(outlist));

  if (!stralloc_copys (&partial, "")) nomem ();

  while (flag0 || inbuflen || partial.len) {
    if (flag0)
      if (inbuflen < sizeof line) {
        r = read (0, line+inbuflen, sizeof line-inbuflen);

        if (r <= 0)
          flag0 = 0;
        else
          inbuflen += r;
      }

    while (flag0) {
      i = byte_chr (line, inbuflen, '\n');
      if (inbuflen && (i == inbuflen)) {
        if (!stralloc_catb (&partial, line, inbuflen)) nomem ();
        inbuflen = 0;
        continue;
      }

      if ((i < inbuflen) || (!flag0 && partial.len)) {
        if (i < inbuflen) ++i;
        if (!stralloc_catb (&partial, line, i)) nomem ();

        inbuflen -= i;
        for (j = 0; j < inbuflen; ++j) line[j] = line[j + i];

        /* end of header */
        if (partial.len == 1) {
          inbuflen = partial.len = flag0 = 0;
          break;
        }

        if (partial.len && flag0) {
          if (str_start (partial.s, "Received: from ")) {
            for (j = str_rchr (partial.s, '(')+1; flag0 && j; j--) {
              i = ip4_scan (partial.s+j, ip);

	      if (skip && i) {
                skip--;
                break;
	      }

              if (i) {
                /* write the IP to the output file */
                stralloc_copyb (&out,ip_fmt, ip4_fmt (ip_fmt, ip));
                buffer_put (&sslist, out.s, out.len);
                buffer_puts (&sslist, "\n");
                flag0 = 0;
                inbuflen = 0;
              }
            }
          }
        }

        partial.len = 0;
        continue;
      }

      break;
    }
  }

  /* flush and close output file */
  buffer_flush (&sslist);
  fsync(fdlist);
  close(fdlist);

  _exit (EXIT_OK);
}
int main(int argc,char * const *argv) {
  const char *hostname;
  int opt;
  struct servent *se;
  char *x;
  unsigned long u;
  int s;
  int t;

  io_opt = ssl_io_opt_default;
  io_opt.timeout = 3600;

  while ((opt = getopt(argc,argv,"46dDvqQhHrR1UXx:t:T:u:g:l:b:B:c:Z:pPoO3IiEeSsaAw:nNyYuUjJ")) != opteof)
    switch(opt) {
      case 'b': scan_ulong(optarg,&backlog); break;
      case 'c': scan_ulong(optarg,&limit); break;
      case 'X': flagallownorules = 1; break;
      case 'x': fnrules = optarg; break;
      case 'B': banner = optarg; break;
      case 'd': flagdelay = 1; break;
      case 'D': flagdelay = 0; break;
      case 'v': verbosity = 2; break;
      case 'q': verbosity = 0; break;
      case 'Q': verbosity = 1; break;
      case 'P': flagparanoid = 0; break;
      case 'p': flagparanoid = 1; break;
      case 'O': flagkillopts = 1; break;
      case 'o': flagkillopts = 0; break;
      case 'H': flagremotehost = 0; break;
      case 'h': flagremotehost = 1; break;
      case 'R': flagremoteinfo = 0; break;
      case 'r': flagremoteinfo = 1; break;
      case 't': scan_ulong(optarg,&timeout); break;
      case 'T': scan_ulong(optarg,&ssltimeout); break;
      case 'w': scan_uint(optarg,&io_opt.timeout); break;
      case 'U': x = env_get("UID"); if (x) scan_ulong(x,&uid);
		x = env_get("GID"); if (x) scan_ulong(x,&gid); break;
      case 'u': scan_ulong(optarg,&uid); break;
      case 'g': scan_ulong(optarg,&gid); break;
      case 'Z': netif=socket_getifidx(optarg); break;
      case '1': flag1 = 1; break;
      case '4': noipv6 = 1; break;
      case '6': forcev6 = 1; break;
      case 'l': localhost = optarg; break;
      case '3': flag3 = 1; break;
      case 'I': flagclientcert = 0; break;
      case 'i': flagclientcert = 1; break;
      case 'S': flagsslenv = 0; break;
      case 's': flagsslenv = 1; break;
      case 'E': flagtcpenv = 0; break;
      case 'e': flagtcpenv = 1; break;
      case 'n': case 'y': flagsslwait = 1; break;
      case 'N': case 'Y': flagsslwait = 0; break;
      case 'j': io_opt.just_shutdown = 1; break;
      case 'J': io_opt.just_shutdown = 0; break;
      default: usage();
    }
  argc -= optind;
  argv += optind;

  if (!verbosity)
    buffer_2->fd = -1;
 
  hostname = *argv++;
  if (!hostname) usage();
  if (str_equal(hostname,"")) hostname = "0";

  x = *argv++;
  if (!x) usage();
  prog = argv;
  if (!*argv) usage();
  if (!x[scan_ulong(x,&u)])
    localport = u;
  else {
    se = getservbyname(x,"tcp");
    if (!se)
      strerr_die3x(111,FATAL,"unable to figure out port number for ",x);
    uint16_unpack_big((char*)&se->s_port,&localport);
  }

  if (x = env_get("VERIFYDEPTH")) {
    scan_ulong(x,&u);
    verifydepth = u;
  }

  if (x = env_get("CAFILE")) cafile = x;
  if (cafile && str_equal(cafile,"")) cafile = 0;

  if (x = env_get("CCAFILE")) ccafile = x;
  if (ccafile && str_equal(ccafile,"")) ccafile = 0;
  if (!flagclientcert) ccafile = 0;

  if (x = env_get("CADIR")) cadir = x;
  if (cadir && str_equal(cadir,"")) cadir= 0;

  if (x = env_get("CERTFILE")) certfile = x;
  if (certfile && str_equal(certfile,"")) certfile = 0;

  if (x = env_get("KEYFILE")) keyfile = x;
  if (keyfile && str_equal(keyfile,"")) keyfile = 0;

  if (x = env_get("DHFILE")) dhfile = x;
  if (dhfile && str_equal(dhfile,"")) dhfile = 0;

  if (x = env_get("CIPHERS")) ciphers = x;
  if (ciphers && str_equal(ciphers,"")) ciphers = 0;

  sig_block(sig_child);
  sig_catch(sig_child,sigchld);
  sig_catch(sig_term,sigterm);
  sig_ignore(sig_pipe);
 
  if (str_equal(hostname,"0")) {
    byte_zero(localip,sizeof localip);
  } else {
    if (!stralloc_copys(&tmp,hostname))
      strerr_die2x(111,FATAL,"out of memory");
    if (dns_ip6_qualify(&addresses,&fqdn,&tmp) == -1)
      strerr_die4sys(111,FATAL,"temporarily unable to figure out IP address for ",hostname,": ");
    if (addresses.len < 16)
      strerr_die3x(111,FATAL,"no IP address for ",hostname);
    byte_copy(localip,16,addresses.s);
    if (ip6_isv4mapped(localip))
      noipv6=1;
  }

  s = socket_tcp6();
  if (s == -1)
    strerr_die2sys(111,FATAL,"unable to create socket: ");

  if (socket_bind6_reuse(s,localip,localport,netif) == -1)
    strerr_die2sys(111,FATAL,"unable to bind: ");

  if (socket_local6(s,localip,&localport,&netif) == -1)
    strerr_die2sys(111,FATAL,"unable to get local address: ");
  if (socket_listen(s,backlog) == -1)
    strerr_die2sys(111,FATAL,"unable to listen: ");
  ndelay_off(s);

  localportstr[fmt_ulong(localportstr,localport)] = 0;
  if (flag1) {
    buffer_init(&b,buffer_unixwrite,1,bspace,sizeof bspace);
    buffer_puts(&b,localportstr);
    buffer_puts(&b,"\n");
    buffer_flush(&b);
  }
 
  if (flag3) read_passwd();

  ctx = ssl_server();
  ssl_errstr();
  if (!ctx) strerr_die2x(111,FATAL,"unable to create SSL context");

  switch (ssl_certkey(ctx,certfile,keyfile,passwd_cb)) {
    case -1: strerr_die2x(111,FATAL,"unable to load certificate");
    case -2: strerr_die2x(111,FATAL,"unable to load key");
    case -3: strerr_die2x(111,FATAL,"key does not match certificate");
    default: break;
  }

  if (!ssl_ca(ctx,cafile,cadir,verifydepth))
    strerr_die2x(111,FATAL,"unable to load CA list");

  if (!ssl_cca(ctx,ccafile))
    strerr_die2x(111,FATAL,"unable to load client CA list");

  if (!ssl_params(ctx,dhfile,rsalen))
    strerr_die2x(111,FATAL,"unable to set cipher parameters");

  if (!ssl_ciphers(ctx,ciphers))
    strerr_die2x(111,FATAL,"unable to set cipher list");

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    strnum2[fmt_ulong(strnum2,rsalen)] = 0;
    strerr_warn4("sslserver: cafile ",strnum," ",cafile,0);
    strerr_warn4("sslserver: ccafile ",strnum," ",ccafile,0);
    strerr_warn4("sslserver: cadir ",strnum," ",cadir,0);
    strerr_warn4("sslserver: cert ",strnum," ",certfile,0);
    strerr_warn4("sslserver: key ",strnum," ",keyfile,0);
    strerr_warn6("sslserver: param ",strnum," ",dhfile," ",strnum2,0);
  }

  close(0); open_read("/dev/null");
  close(1); open_append("/dev/null");

  printstatus();
 
  for (;;) {
    while (numchildren >= limit) sig_pause();

    sig_unblock(sig_child);
    t = socket_accept6(s,remoteip,&remoteport,&netif);
    sig_block(sig_child);

    if (t == -1) continue;
    ++numchildren; printstatus();
 
    switch(fork()) {
      case 0:
        close(s);
        doit(t);
	strerr_die4sys(111,DROP,"unable to run ",*argv,": ");
      case -1:
        strerr_warn2(DROP,"unable to fork: ",&strerr_sys);
        --numchildren; printstatus();
    }
    close(t);
  }
}
int main(int argc,char * const *argv) {
  const char *hostname;
  int opt;
  char *x;
  unsigned long u;
  int s;
  int t;
  int flagv4 = 1, flagv6 = 1, rc;
  struct addrinfo *localai = NULL, hints = {0}, *ai;
 
  while ((opt = getopt(argc,argv,"dDvqQhHrR1UXx:t:T:u:g:l:b:B:c:pPoO3IiEeSsw:nN46")) != opteof)
    switch(opt) {
      case 'b': scan_ulong(optarg,&backlog); break;
      case 'c': scan_ulong(optarg,&limit); break;
      case 'X': flagallownorules = 1; break;
      case 'x': fnrules = optarg; break;
      case 'B': banner = optarg; break;
      case 'd': flagdelay = 1; break;
      case 'D': flagdelay = 0; break;
      case 'v': verbosity = 2; break;
      case 'q': verbosity = 0; break;
      case 'Q': verbosity = 1; break;
      case 'P': flagparanoid = 0; break;
      case 'p': flagparanoid = 1; break;
      case 'O': flagkillopts = 1; break;
      case 'o': flagkillopts = 0; break;
      case 'H': flagremotehost = 0; break;
      case 'h': flagremotehost = 1; break;
      case 'R': flagremoteinfo = 0; break;
      case 'r': flagremoteinfo = 1; break;
      case 't': scan_ulong(optarg,&timeout); break;
      case 'T': scan_ulong(optarg,&ssltimeout); break;
      case 'w': scan_uint(optarg,&progtimeout); break;
      case 'U': x = env_get("UID"); if (x) scan_ulong(x,&uid);
		x = env_get("GID"); if (x) scan_ulong(x,&gid); break;
      case 'u': scan_ulong(optarg,&uid); break;
      case 'g': scan_ulong(optarg,&gid); break;
      case '1': flag1 = 1; break;
      case 'l': localhost = optarg; break;
      case '3': flag3 = 1; break;
      case 'I': flagclientcert = 0; break;
      case 'i': flagclientcert = 1; break;
      case 'S': flagsslenv = 0; break;
      case 's': flagsslenv = 1; break;
      case 'E': flagtcpenv = 0; break;
      case 'e': flagtcpenv = 1; break;
      case 'n': flagsslwait = 1; break;
      case 'N': flagsslwait = 0; break;
      case '4': flagv6 = 0; break;
      case '6': flagv4 = 0; break;
      default: usage();
    }
  if (flagv4 == flagv6) { flagv4 = flagv6 = 1; }
  argc -= optind;
  argv += optind;

  if (!verbosity)
    buffer_2->fd = -1;
 
  hostname = *argv++;
  if (!hostname) usage();
  if (str_equal(hostname,"")) hostname = NULL;
  if (str_equal(hostname,"0")) hostname = NULL;

  x = *argv++;
  if (!x) usage();
  prog = argv;
  if (!*argv) usage();

  hints.ai_family = flagv4 == flagv6 ? AF_UNSPEC : flagv4 ? AF_INET : AF_INET6;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if ((rc = getaddrinfo(hostname, x, &hints, &localai))) {
    strerr_die(111,FATAL "unable to figure out address for ", hostname ? hostname : "0",
		" ",x,": ",gai_strerror(rc),0);
  }
  if (!localai) {
    strerr_die2x(111,FATAL,"address not found");
  }

  if (x = env_get("VERIFYDEPTH")) {
    scan_ulong(x,&u);
    verifydepth = u;
  }

  if (x = env_get("CAFILE")) cafile = x;
  if (cafile && str_equal(cafile,"")) cafile = 0;

  if (x = env_get("CCAFILE")) ccafile = x;
  if (ccafile && str_equal(ccafile,"")) ccafile = 0;
  if (!flagclientcert) ccafile = 0;

  if (x = env_get("CADIR")) cadir = x;
  if (cadir && str_equal(cadir,"")) cadir= 0;

  if (x = env_get("CERTCHAINFILE")) certchainfile = x;
  if (certchainfile && str_equal(certchainfile,"")) certchainfile = 0;

  if (x = env_get("CERTFILE")) certfile = x;
  if (certfile && str_equal(certfile,"")) certfile = 0;

  if (x = env_get("KEYFILE")) keyfile = x;
  if (keyfile && str_equal(keyfile,"")) keyfile = 0;

  if (x = env_get("DHFILE")) dhfile = x;
  if (dhfile && str_equal(dhfile,"")) dhfile = 0;

  if (x = env_get("CIPHERS")) ciphers = x;
  if (ciphers && str_equal(ciphers,"")) ciphers = 0;

  sig_block(sig_child);
  sig_catch(sig_child,sigchld);
  sig_catch(sig_term,sigterm);
  sig_ignore(sig_pipe);

  for (ai = localai; ai; ai = ai->ai_next) { 
    s = socket_tcp(ai->ai_family, ai->ai_protocol);
    if (s == -1)
      strerr_die2sys(111,FATAL,"unable to create socket: ");

    if (socket_bind_reuse(s,ai) == -1)
      strerr_die2sys(111,FATAL,"unable to bind: ");

    if (socket_local(s,&localaddr,&localport) == -1)
      strerr_die2sys(111,FATAL,"unable to get local address: ");
    if (socket_listen(s,backlog) == -1)
      strerr_die2sys(111,FATAL,"unable to listen: ");
    break;
  }
  freeaddrinfo(localai); localai = NULL;
  ndelay_off(s);

  localportstr[fmt_ulong(localportstr,localport)] = 0;
  if (flag1) {
    buffer_init(&b,buffer_unixwrite,1,bspace,sizeof bspace);
    buffer_puts(&b,localportstr);
    buffer_puts(&b,"\n");
    buffer_flush(&b);
  }
 
  if (flag3) read_passwd();

  ctx = ssl_server();
  ssl_errstr();
  if (!ctx) strerr_die2x(111,FATAL,"unable to create SSL context");

  if (certchainfile) {
    switch (ssl_chainfile(ctx,certchainfile,keyfile,passwd_cb)) {
      case -1: strerr_die2x(111,FATAL,"unable to load certificate chain file");
      case -2: strerr_die2x(111,FATAL,"unable to load key");
      case -3: strerr_die2x(111,FATAL,"key does not match certificate");
      default: break;
    }
  } 
  else {  
    switch (ssl_certkey(ctx,certfile,keyfile,passwd_cb)) {
      case -1: strerr_die2x(111,FATAL,"unable to load certificate");
      case -2: strerr_die2x(111,FATAL,"unable to load key");
      case -3: strerr_die2x(111,FATAL,"key does not match certificate");
      default: break;
    }
  }

  if (!ssl_ca(ctx,cafile,cadir,verifydepth))
    strerr_die2x(111,FATAL,"unable to load CA list");

  if (!ssl_cca(ctx,ccafile))
    strerr_die2x(111,FATAL,"unable to load client CA list");

  if (!ssl_params(ctx,dhfile,rsalen))
    strerr_die2x(111,FATAL,"unable to set DH/RSA parameters");

  if (!ssl_ciphers(ctx,ciphers))
    strerr_die2x(111,FATAL,"unable to set cipher list");

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    strnum2[fmt_ulong(strnum2,rsalen)] = 0;
    strerr_warn4("sslserver: cafile ",strnum," ",cafile,0);
    strerr_warn4("sslserver: ccafile ",strnum," ",ccafile,0);
    strerr_warn4("sslserver: cadir ",strnum," ",cadir,0);
    strerr_warn4("sslserver: chainfile ",strnum," ",certchainfile,0);
    strerr_warn4("sslserver: cert ",strnum," ",certfile,0);
    strerr_warn4("sslserver: key ",strnum," ",keyfile,0);
    strerr_warn6("sslserver: param ",strnum," ",dhfile," ",strnum2,0);
  }

  close(0); open_read("/dev/null");
  close(1); open_append("/dev/null");

  printstatus();
 
  for (;;) {
    while (numchildren >= limit) sig_pause();

    sig_unblock(sig_child);
    t = socket_accept(s,&remoteaddr,&remoteport);
    sig_block(sig_child);

    if (t == -1) continue;
    ++numchildren; printstatus();
 
    switch(fork()) {
      case 0:
        close(s);
        doit(t);
	strerr_die4sys(111,DROP,"unable to run ",*argv,": ");
      case -1:
        strerr_warn2(DROP,"unable to fork: ",&strerr_sys);
        --numchildren; printstatus();
    }
    close(t);
  }
}