コード例 #1
0
ファイル: multilog.c プロジェクト: schmurfy/daemontools
void startprocessor(struct cyclog *d)
{
  const char *args[4];
  int fd;

  sig_uncatch(sig_term);
  sig_uncatch(sig_alarm);
  sig_unblock(sig_term);
  sig_unblock(sig_alarm);

  fd = open_read("previous");
  if (fd == -1) return;
  if (fd_move(0,fd) == -1) return;
  fd = open_trunc("processed");
  if (fd == -1) return;
  if (fd_move(1,fd) == -1) return;
  fd = open_read("state");
  if (fd == -1) return;
  if (fd_move(4,fd) == -1) return;
  fd = open_trunc("newstate");
  if (fd == -1) return;
  if (fd_move(5,fd) == -1) return;

  args[0] = "sh";
  args[1] = "-c";
  args[2] = d->processor;
  args[3] = 0;
  execve("/bin/sh",args,environ);
}
コード例 #2
0
ファイル: supervise.c プロジェクト: ajtulloch/daemontools
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);
}
コード例 #3
0
ファイル: uint32_seed.c プロジェクト: rsenn/shish
/* feed data to the prng */
int uint32_seed(const void *p, unsigned long n)
{
  int fd = -1;
  
  if(n == 0)
  {
    int i;
    
    fd = open_read("/dev/urandom");
    i = read(fd, uint32_pool, sizeof(uint32_pool));
    
    if(i > 0)
      uint32_seeds += i;
    
    close(fd);
  } else {
    const char *b = (const char *)p;
    char *x = (char *)uint32_pool;
    
    while(n) {
      x[n % sizeof(uint32_pool)] ^= *b;
      n--;
      b++;
      uint32_seeds++;
    }
    
  }
  
  return fd;
}
コード例 #4
0
ファイル: tdlookup.c プロジェクト: siddhesh/djbdns
int respond(char *q,char qtype[2],char ip[4])
{
  int fd;
  int r;
  char key[6];

  tai_now(&now);
  fd = open_read("data.cdb");
  if (fd == -1) return 0;
  cdb_init(&c,fd);

  byte_zero(clientloc,2);
  key[0] = 0;
  key[1] = '%';
  byte_copy(key + 2,4,ip);
  r = cdb_find(&c,key,6);
  if (!r) r = cdb_find(&c,key,5);
  if (!r) r = cdb_find(&c,key,4);
  if (!r) r = cdb_find(&c,key,3);
  if (!r) r = cdb_find(&c,key,2);
  if (r == -1) return 0;
  if (r && (cdb_datalen(&c) == 2))
    if (cdb_read(&c,clientloc,2,cdb_datapos(&c)) == -1) return 0;

  r = doit(q,qtype);

  cdb_free(&c);
  close(fd);
  return r;
}
コード例 #5
0
ファイル: mmap_read.c プロジェクト: masroore/libowfat
extern const char* mmap_read(const char* filename,size_t * filesize) {
#ifdef __MINGW32__
  HANDLE fd,m;
  char* map;
  fd=CreateFile(filename,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  if (fd==INVALID_HANDLE_VALUE)
    return 0;
  m=CreateFileMapping(fd,0,PAGE_READONLY,0,0,NULL);
  map=0;
  if (m)
    if ((map=MapViewOfFile(m,FILE_MAP_READ,0,0,0)))
      *filesize=GetFileSize(fd,NULL);
  CloseHandle(m);
  CloseHandle(fd);
  return map;
#else
  int fd=open_read(filename);
  char *map;
  if (fd>=0) {
    register off_t o=lseek(fd,0,SEEK_END);
    if (sizeof(off_t)!=sizeof(size_t) && o > (off_t)(size_t)-1) { close(fd); return 0; }
    *filesize=(size_t)o;
    map=mmap(0,*filesize,PROT_READ,MAP_SHARED,fd,0);
    if (map==(char*)-1)
      map=0;
    close(fd);
    return map;
  }
  return 0;
#endif
}
コード例 #6
0
ファイル: sv.c プロジェクト: emuikernel/WNR2000v4
static int svstatus_get(void)
{
	if ((fd = open_write("supervise/ok")) == -1) {
		if (errno == ENODEV) {
			*acts == 'x' ? ok("runsv not running")
			             : failx("runsv not running");
			return 0;
		}
		warn_cannot("open supervise/ok");
		return -1;
	}
	close(fd);
	if ((fd = open_read("supervise/status")) == -1) {
		warn_cannot("open supervise/status");
		return -1;
	}
	r = read(fd, svstatus, 20);
	close(fd);
	switch (r) {
	case 20: break;
	case -1: warn_cannot("read supervise/status"); return -1;
	default: warnx_cannot("read supervise/status: bad format"); return -1;
	}
	return 1;
}
コード例 #7
0
ファイル: load_avg.c プロジェクト: mgumz/tmux-utils
int print_load_avg(int out_fd) {
    int fd = open_read("/proc/loadavg");
    if (fd == -1) {
        return 0;
    }
    return first_n_fields(out_fd, fd, ' ', 3);
}
コード例 #8
0
ファイル: sv_stat_load.c プロジェクト: dpejesh/libsv
int sv_stat_load(sv_stat_t *svst, char *path) {
  int r;
  struct stat st;
  char status[SUPERVISE_STATUS_SIZE];
  int status_fd;
  uint32_t tmp;
  
  /* stat() path and check that it's actually a file and the correct size. */
  r = stat(path, &st);
  if (r)
    return r;
  if (!S_ISREG(st.st_mode) || (st.st_size != SUPERVISE_STATUS_SIZE))
    return ESVINVALSTAT;
    
  /* Open path and load its contents into the status byte array. */
  status_fd = open_read(path);
  if (status_fd == -1)
    return ESVOPENSTAT;
  r = read(status_fd, status, SUPERVISE_STATUS_SIZE);
  close(status_fd);
  if (r == -1)
    return ESVREADSTAT;
    
  /* taia_unpack will read the pid as the attoseconds, so set it to zero after
   * reading it. */
  taia_unpack(&svst->timestamp, status);
  svst->timestamp.atto = 0;
  tmp = be32toh(((uint32_t *)status)[3]);
  svst->pid = tmp;
  svst->paused = status[16];
  svst->mode = status[17];
  
  return 0;
}
コード例 #9
0
ファイル: locallookup.c プロジェクト: ajtulloch/qmail
int
get_local_maildir(stralloc *home, stralloc *maildir)
{
	substdio	ss;
	char		buf[512];
	int		dirfd, fd, match, save;
	
	dirfd = open_read(".");
	if (dirfd == -1)
		return ERRNO;
	if (chdir(home->s) == -1)
		return ERRNO;

	if ((fd = open_read(".qmail")) == -1) {
		if (errno == error_noent) return 0;
		return ERRNO;
	}

	substdio_fdbuf(&ss, subread, fd, buf, sizeof(buf));
	while (1) {
		if (getln(&ss, maildir, &match, '\n') != 0) goto tryclose;
		if (!match && !maildir->len) {
			if (!stralloc_copyb(maildir, "", 1)) goto tryclose;
			break;
		}
		if ((maildir->s[0] == '.' || maildir->s[0] == '/') && 
			  maildir->s[maildir->len-2] == '/') {
			maildir->s[maildir->len-1] = '\0';
			break;
		}
	}
	if (fchdir(dirfd) == -1)
		return ERRNO;
	close(dirfd);
	close(fd);
	return 0;

tryclose:
	save = errno; /* preserve errno */
	if (fchdir(dirfd) == -1)
		return ERRNO;
	close(dirfd);
	close(fd);
	errno = save;
	return ERRNO;
}
コード例 #10
0
ファイル: open_read.c プロジェクト: dpejesh/libsv-common
int main() {
  int fd = open_read("read.txt");
  if (fd != -1)
    return 1;
    
  fd = open_trunc("read.txt");
  if (fd == -1)
    return 2;
    
  close(fd);
  
  fd = open_read("read.txt");
  if (fd == -1)
    return 3;
    
  return 0;
}
コード例 #11
0
ファイル: opentest.c プロジェクト: LlsDimple/tinyssh
/* test if close-on-exec works for open_read() */
static void test1(void) {

    int fd;

    close(0);
    fd = open_read("opentest.c");
    if (fd != 0) fail("unable to open opentest.c for reading");
    cat();
}
コード例 #12
0
ファイル: handle.cpp プロジェクト: bunsanorg/process
handle handle::open_null() {
#if defined(BOOST_POSIX_API)
  return open_read_write("/dev/null");
#elif defined(BOOST_WINDOWS_API)
  return open_read("NUL");
#else
#error Unknown platform
#endif
}
コード例 #13
0
static void do_get(const char *action)
{
  unsigned long u;
  struct stat st;
  char ch;
  int r;
  unsigned int pos;
  int fd;

  if (!flagget)
    strerr_die2x(100,FATAL,MSG(ERR_NOT_AVAILABLE));
  hdr_subject(MSG(SUB_GET_MSG));
  hdr_ctboundary();
  copy(&qq,"text/top",flagcd);

  pos = str_len(ACTION_GET);
  if (!case_starts(action,ACTION_GET))
    pos = str_len(ALT_GET);

  if (action[pos] == '.' || action [pos] == '_') pos++;
  scan_ulong(action + pos,&u);

  stralloc_copys(&line,"archive/");
  stralloc_catb(&line,strnum,fmt_ulong(strnum,u / 100));
  stralloc_cats(&line,"/");
  stralloc_catb(&line,strnum,fmt_uint0(strnum,(unsigned int) (u % 100),2));
  stralloc_0(&line);

  fd = open_read(line.s);
  if (fd == -1)
    if (errno != error_noent)
      strerr_die2sys(111,FATAL,MSG1(ERR_OPEN,line.s));
    else
      copy_act("text/get-bad");
  else {
    if (fstat(fd,&st) == -1)
      copy_act("text/get-bad");
    else if (!(st.st_mode & 0100))
      copy_act("text/get-bad");
    else {
      showsend("get");
      substdio_fdbuf(&sstext,read,fd,textbuf,sizeof(textbuf));
      qmail_puts(&qq,"> ");
      for (;;) {
	r = substdio_get(&sstext,&ch,1);
	if (r == -1) strerr_die2sys(111,FATAL,MSG1(ERR_READ,line.s));
	if (r == 0) break;
	qmail_put(&qq,&ch,1);
	if (ch == '\n') qmail_puts(&qq,"> ");
      }
      qmail_puts(&qq,"\n");
    }
    close(fd);
  }
  copybottom(0);
  qmail_to(&qq,target.s);
}
コード例 #14
0
ファイル: redirfd.c プロジェクト: bitgaming/execline
int main (int argc, char const *const *argv, char const *const *envp)
{
  int fd, fd2 ;
  unsigned int flags = 0 ;
  int what = -1 ;
  int changemode = 0 ;
  PROG = "redirfd" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "rwuacxnb", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'r' : what = O_RDONLY ; flags &= ~(O_APPEND|O_CREAT|O_TRUNC|O_EXCL) ; break ;
        case 'w' : what = O_WRONLY ; flags |= O_CREAT|O_TRUNC ; flags &= ~(O_APPEND|O_EXCL) ; break ;
        case 'u' : what = O_RDWR ; flags &= ~(O_APPEND|O_CREAT|O_TRUNC|O_EXCL) ; break ;
        case 'a' : what = O_WRONLY ; flags |= O_CREAT|O_APPEND ; flags &= ~(O_TRUNC|O_EXCL) ; break ;
        case 'c' : what = O_WRONLY ; flags |= O_APPEND ; flags &= ~(O_CREAT|O_TRUNC|O_EXCL) ; break ;
        case 'x' : what = O_WRONLY ; flags |= O_CREAT|O_EXCL ; flags &= ~(O_APPEND|O_TRUNC) ; break ;
        case 'n' : flags |= O_NONBLOCK ; break ;
        case 'b' : changemode = 1 ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if ((argc < 3) || (what == -1)) dieusage() ;
  if (!uint0_scan(argv[0], (unsigned int *)&fd)) dieusage() ;
  flags |= what ;
  fd2 = open3(argv[1], flags, 0666) ;
  if ((fd2 == -1) && (what == O_WRONLY) && (errno == ENXIO))
  {
    register int e ;
    int fdr = open_read(argv[1]) ;
    if (fdr == -1) strerr_diefu2sys(111, "open_read ", argv[1]) ;
    fd2 = open3(argv[1], flags, 0666) ;
    e = errno ;
    fd_close(fdr) ;
    errno = e ;
  }
  if (fd2 == -1) strerr_diefu2sys(111, "open ", argv[1]) ;
  if (fd_move(fd, fd2) == -1)
  {
    char fmt[UINT_FMT] ;
    fmt[uint_fmt(fmt, fd2)] = 0 ;
    strerr_diefu4sys(111, "move fd ", fmt, " to fd ", argv[0]) ;
  }
  if (changemode)
  {
    if (((flags & O_NONBLOCK) ? ndelay_off(fd) : ndelay_on(fd)) < 0)
      strerr_diefu1sys(111, "change blocking mode") ;
  }
  pathexec_run(argv[2], argv+2, envp) ;
  strerr_dieexec(111, argv[2]) ;
}
コード例 #15
0
ファイル: load.c プロジェクト: LlsDimple/tinyssh
int load(const char *fn,void *x,long long xlen)
{
  int fd;
  int r;
  fd = open_read(fn);
  if (fd == -1) return -1;
  r = readall(fd,x,xlen);
  close(fd);
  return r;
}
コード例 #16
0
ファイル: openreadclose.c プロジェクト: KodeKreatif/ucspi-tcp
int openreadclose(char *fn,stralloc *sa,unsigned int bufsize)
{
  int fd;
  fd = open_read(fn);
  if (fd == -1) {
    if (errno == error_noent) return 0;
    return -1;
  }
  if (readclose(fd,sa,bufsize) == -1) return -1;
  return 1;
}
コード例 #17
0
const char *get_from(const char *adr, /* target address */
		     const char *act) /* action */
/* If we captured a from line, it will be from the subscriber, except   */
/* when -S is used when it's usually from the subscriber, but of course */
/* could be from anyone. The matching to stored data is required only   */
/* to support moderated lists, and in cases where a new -sc is issued   */
/* because an old one was invalid. In this case, we read through the    */
/* from file trying to match up a timestamp with that starting in       */
/* *(act+3). If the time stamp matches, we compare the target address   */
/* itself. act + 3 must be a legal part of the string returns pointer to*/
/* fromline, NULL if not found. Since the execution time from when to   */
/* storage may differ, we can't assume that the timestamps are in order.*/
{
  int fd;
  const char *fl;
  unsigned int pos;
  unsigned long thistime;
  unsigned long linetime;

  if (!flagstorefrom) return 0;
  if (fromline.len) {	/* easy! We got it in this message */
    stralloc_0(&fromline);
    return fromline.s;
  }			/* need to recover it from DIR/from */
  fl = 0;
  (void) scan_ulong(act+3,&thistime);
  if ((fd = open_read("from")) == -1) {
    if (errno == error_noent)
      return 0;
    else
      strerr_die2sys(111,FATAL,MSG1(ERR_READ,"from"));
  }
  substdio_fdbuf(&sstext,read,fd,textbuf,(int) sizeof(textbuf));
  for (;;) {
    if (getln(&sstext,&fromline,&match,'\n') == -1)
      strerr_die2sys(111,FATAL,MSG1(ERR_READ,"from"));
    if (!match) break;
    fromline.s[fromline.len - 1] = (char) 0;
	/* now:time addr\0fromline\0 read all. They can be out of order! */
    pos = scan_ulong(fromline.s,&linetime);
    if (linetime != thistime) continue;
    if (!str_diff(fromline.s + pos + 1,adr)) {
      pos = str_len(fromline.s);
      if (pos < fromline.len) {
	fl = fromline.s + pos + 1;
	break;
      }
    }
  }
  close(fd);
  return fl;
}
コード例 #18
0
ファイル: trigger.c プロジェクト: 0xkag/qmail
void trigger_set()
{
 if (fd != -1)
   close(fd);
#ifdef HASNAMEDPIPEBUG1
 if (fdw != -1)
   close(fdw);
#endif
 fd = open_read("lock/trigger");
#ifdef HASNAMEDPIPEBUG1
 fdw = open_write("lock/trigger");
#endif
}
コード例 #19
0
extern char* mmap_private(const char* filename,unsigned long* filesize) {
  int fd=open_read(filename);
  char *map;
  if (fd>=0) {
    *filesize=lseek(fd,0,SEEK_END);
    map=mmap(0,*filesize,PROT_WRITE,MAP_PRIVATE,fd,0);
    if (map==(char*)-1)
      map=0;
    close(fd);
    return map;
  }
  return 0;
}
コード例 #20
0
ファイル: pickdns.c プロジェクト: kjseefried/didentd
int respond(char *q,char qtype[2],char ip[4])
{
    int fd;
    int result;

    fd = open_read("data.cdb");
    if (fd == -1) return 0;
    cdb_init(&c,fd);
    result = doit(q,qtype,ip);
    cdb_free(&c);
    close(fd);
    return result;
}
コード例 #21
0
ファイル: openreadclose.c プロジェクト: rsenn/dirlist
int
openreadclose(const char* fn, stralloc* sa, size_t bufsize) {
  int64 fd;
  fd = open_read(fn);
  if(fd == -1) {
    if(errno == ENOENT)
      return 0;
    return -1;
  }
  if(readclose(fd, sa, bufsize) == -1)
    return -1;
  return 1;
}
コード例 #22
0
ファイル: ezmlm-import.c プロジェクト: kunishi/qmail-hg
int main(int argc,char *argv[])
{
  const char* dir;
  int fd;
  int match;
  unsigned long msgsize = 0L;
  int opt;
  
  while ((opt = getopt(argc,argv,"vV")) != opteof) {
    switch(opt) {
    case 'v':
    case 'V':
      strerr_die2x(0, "ezmlm-import version: ",auto_version);
    default:
      die_usage();
    }
  }

  if (argc - optind != 2)
    die_usage();

  if ((fd = open_read(argv[optind+1])) == -1)
    strerr_die4sys(111,FATAL,ERR_OPEN,argv[optind+1],": ");
  substdio_fdbuf(&ssin,read,fd,inputbuf,sizeof inputbuf);

  startup(dir = argv[optind]);
  lockfile("lock");

  getconf_ulong2(&msgnum,&cumsize,"num",0,dir);
  
  fd = 0;
  while (getln(&ssin,&line,&match,'\n') == 0 && match) {
    if (line.len > 5
	&& byte_diff(line.s,5,"From ") == 0) {
      if (fd > 0) {
	if (substdio_flush(&ssarchive) == -1
	    || fchmod(fd,MODE_ARCHIVE|0700) == -1
	    || close(fd) == -1)
	  strerr_die4sys(111,FATAL,ERR_WRITE,fnaf.s,": ");
	fd = 0;
      }
      ++msgnum;
      cumsize += (msgsize + 128L) >> 8;
      msgsize = 0L;
      fd = openone(msgnum);
    }
    else if (fd > 0) {
      substdio_put(&ssarchive,line.s,line.len);
      msgsize += line.len;
    }
  }
コード例 #23
0
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 ;
}
コード例 #24
0
ファイル: io.c プロジェクト: alecrosic/hurd
/* Read data from an IO object.  If offset if -1, read from the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount desired to be read is in AMOUNT.  */
error_t
trivfs_S_io_read (struct trivfs_protid *cred,
                  mach_port_t reply, mach_msg_type_name_t reply_type,
                  char **data, mach_msg_type_number_t *data_len,
                  loff_t offs, mach_msg_type_number_t amount)
{
    if (! cred)
        return EOPNOTSUPP;
    else if (! (cred->po->openmodes & O_READ))
        return EBADF;
    else
        return open_read ((struct open *)cred->po->hook,
                          offs, amount, (void **)data, data_len);
}
コード例 #25
0
ファイル: dnscache.c プロジェクト: rahulsundaram/ndjbdns
static int
dbl_init (void)
{
    extern struct cdb bl;    /* dns block list */

    int fd = open_read ("dnsbl.cdb");
    if (fd == -1)
        return 0;

    cdb_init (&bl, fd);
    close (fd);

    return 1;
}
コード例 #26
0
ファイル: roots.c プロジェクト: GunioRobot/djbdns
int roots_init(void)
{
  int fddir;
  int r;

  if (!stralloc_copys(&data,"")) return 0;

  fddir = open_read(".");
  if (fddir == -1) return 0;
  r = init1();
  if (fchdir(fddir) == -1) r = 0;
  close(fddir);
  return r;
}
コード例 #27
0
void store_from(stralloc *frl,	/* from line */
		const char *adr)
/* rewrites the from file removing all that is older than 1000000 secs  */
/* and add the curent from line (frl). Forget it if there is none there.*/
/* NOTE: This is used only for subscribes to moderated lists!           */
{
  int fdin;
  int fdout;
  unsigned long linetime;

  if (!flagstorefrom || !frl->len) return;	/* nothing to store */
  lock();
  if ((fdout = open_trunc("fromn")) == -1)
    strerr_die2sys(111,FATAL,MSG1(ERR_OPEN,"fromn"));
  substdio_fdbuf(&ssfrom,write,fdout,frombuf,(int) sizeof(frombuf));
  if ((fdin = open_read("from")) == -1) {
    if (errno != error_noent)
      strerr_die2sys(111,FATAL,MSG1(ERR_OPEN,"from"));
  } else {
      substdio_fdbuf(&sstext,read,fdin,textbuf,(int) sizeof(textbuf));
      for (;;) {
	if (getln(&sstext,&line,&match,'\n') == -1)
	strerr_die2sys(111,FATAL,MSG1(ERR_READ,"from"));
	if (!match) break;
	(void) scan_ulong(line.s,&linetime);
	if (linetime + 1000000 > (unsigned long) when
	    && linetime <= (unsigned long) when)
	  if (substdio_bput(&ssfrom,line.s,line.len))
	    strerr_die2sys(111,FATAL,MSG1(ERR_WRITE,"fromn"));
      }
      close(fdin);
  }					/* build new entry */
  stralloc_copyb(&line,strnum,fmt_ulong(strnum,when));
  stralloc_append(&line,' ');
  stralloc_cats(&line,adr);
  stralloc_0(&line);
  stralloc_catb(&line,frl->s,frl->len);
  stralloc_append(&line,'\n');
  if (substdio_bput(&ssfrom,line.s,line.len) == -1)
    strerr_die2sys(111,FATAL,MSG1(ERR_WRITE,"fromn"));
  if (substdio_flush(&ssfrom) == -1)
    strerr_die2sys(111,FATAL,MSG1(ERR_WRITE,"fromn"));
  if (fsync(fdout) == -1)
    strerr_die2sys(111,FATAL,MSG1(ERR_SYNC,"fromn"));
  if (close(fdout) == -1)
    strerr_die2sys(111,FATAL,MSG1(ERR_CLOSE,"fromn"));
  wrap_rename("fromn","from");
  unlock();
}
コード例 #28
0
int main(int argc,const char *const *argv)
{
  int opt;
  int fd;
  const char *dir;

  sig_ignore(sig_pipe);

  while ((opt = getopt(argc,argv,"?vudopchaitkx")) != opteof)
    if (opt == 'v'){
      puts("version: 0.76d");
      exit(0);
    }
    else if (opt == '?')
      strerr_die1x(100,"svc options: u up, d down, o once, x exit, p pause, c continue, h hup, a alarm, i interrupt, t term, k kill");
    else
      if (datalen < sizeof data)
        if (byte_chr(data,datalen,opt) == datalen)
          data[datalen++] = opt;
  argv += optind;

  fdorigdir = open_read(".");
  if (fdorigdir == -1)
    strerr_die2sys(111,FATAL,"unable to open current directory: ");

  while (dir = *argv++) {
    if (chdir(dir) == -1)
      strerr_warn4(WARNING,"unable to chdir to ",dir,": ",&strerr_sys);
    else {
      fd = open_write("supervise/control");
      if (fd == -1)
        if (errno == error_nodevice)
          strerr_warn4(WARNING,"unable to control ",dir,": supervise not running",0);
        else
          strerr_warn4(WARNING,"unable to control ",dir,": ",&strerr_sys);
      else {
        ndelay_off(fd);
        buffer_init(&b,buffer_unixwrite,fd,bspace,sizeof bspace);
        if (buffer_putflush(&b,data,datalen) == -1)
          strerr_warn4(WARNING,"error writing commands to ",dir,": ",&strerr_sys);
        close(fd);
      }
    }
    if (fchdir(fdorigdir) == -1)
      strerr_die2sys(111,FATAL,"unable to set directory: ");
  }

  _exit(0);
}
コード例 #29
0
ファイル: hfile.cpp プロジェクト: 3ddy/Jedi-Academy
////////////////////////////////////////////////////////////////////////////////////
// Searches for the first block with the matching data size, and reads it in. 
////////////////////////////////////////////////////////////////////////////////////
bool		hfile::load(void* data, int datasize)
{
	// Go Ahead And Open The File For Reading
	//----------------------------------------
	bool auto_opened = false;
	if (!is_open())
	{
		if (!open_read())
		{
			return false;
		}
		auto_opened = true;
	}

	// Make Sure That The File Is Readable
	//-------------------------------------
	SOpenFile&	sfile = Pool()[mHandle];
	if (!sfile.mForRead)
	{
		assert("HFILE: Unable to load from a file that is opened for save"==0);
		if (auto_opened)
		{
			close();
		}
		return false;
	}


	// Now Read It
	//-------------
	if (!HFILEread(sfile.mHandle, data, datasize))
	{
		assert("HFILE: Unable To Read Object"==0);
		if (auto_opened)
		{
			close();
		}
		return false;
	}

	// Success!
	//----------
	if (auto_opened)
	{
		close();
	}
	return true;
}
コード例 #30
0
ファイル: data.cpp プロジェクト: mokayy/KDDCup2011
void read_validate()
{
	int uid, n, iid, stmp, day, hr, min, sec;
	double  score;
	ssize_t didx = 0;
	unsigned int uidx=0, ridx=0, iidx=0;
	ssize_t size;
	char *data = (char *)open_read("validationIdx1.txt", &size);
	int sum = 0;
	while(1) {
		if ( parseInt(data, &didx, &uid, size) ) {
			break;
		}
		uidx = umap[uid];
		parseInt(data, &didx, &n, size);
		//users[uidx].nu = n;
		for(int i = 0; i < n; i++) {
			parseInt(data, &didx, &iid, size);
			parseInt(data, &didx, &stmp, size);
			parseInt(data, &didx, &day, size);
			parseInt(data, &didx, &hr, size);
			parseInt(data, &didx, &min, size);
			parseInt(data, &didx, &sec, size);
			score =  (double)stmp / SCORENORM;
			day -= 2672;
			if( imap.count(iid) == 0) {
				iidx = imaplen;
				imaplen ++;
				imap[iid] = iidx;

				items[iidx].id = iid;
				items[iidx].count = 0;
				items[iidx].artistid = -1;
				items[iidx].albumid = -1;
			} else {
				iidx = imap[iid];
				assert(items[iidx].id == iid);
			}
			validations[ridx].item = iidx;
			validations[ridx].rating = score;
			validations[ridx].day = day;
			ridx++;
		}
	}
	munmap(data, size);
	nValidations = ridx;
	printf("nValidations = %d\n", nValidations);
}