示例#1
0
// Check if there is a lockfile and a corresponding process
void App::GetLock() {
    std::ifstream lockfile(cfg.getOption("lockfile").c_str());
    if (!lockfile) {
        // no lockfile present, create one
        std::ofstream lockfile(cfg.getOption("lockfile").c_str(), ios_base::out);
        if (!lockfile) {
            cerr << APPNAME << ": Could not create lock file: " << cfg.getOption("lockfile").c_str() << std::endl;
            exit(ERR_EXIT);
        }
        lockfile << getpid() << std::endl;
        lockfile.close();
    } else {
        // lockfile present, read pid from it
        int pid = 0;
        lockfile >> pid;
        lockfile.close();
        if (pid > 0) {
            // see if process with this pid exists
            int ret = kill(pid, 0);
            if (ret == 0 || (ret == -1 && errno == EPERM) ) {
                cerr << APPNAME << ": Another instance of the program is already running with PID " << pid << std::endl;
                exit(0);
            } else {
                cerr << APPNAME << ": Stale lockfile found, removing it" << std::endl;
                std::ofstream lockfile(cfg.getOption("lockfile").c_str(), ios_base::out);
                if (!lockfile) {
                    cerr << APPNAME << ": Could not create new lock file: " << cfg.getOption("lockfile") << std::endl;
                    exit(ERR_EXIT);
                }
                lockfile << getpid() << std::endl;
                lockfile.close();
            }
        }
    }
}
示例#2
0
文件: relock.c 项目: clamxyz/apue
/*
 * 同一个进程可以对同一个文件可以一直加锁
 */
int main()
{
	int fd;

	if ( (fd = open("temp.foo", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG)) < 0)
	{
		printf("open failed.[%d][%s]\n", errno, strerror(errno));
		return -1;
	}

	if (lockfile(fd) < 0)
	{
		printf("lockfile failed[%d][%s]\n", errno, strerror(errno));
		close(fd);
		return -1;
	}
	write(fd, "ww", 3);
	sleep(100);
	if (lockfile(fd) < 0)
	{
		printf("lockfile failed[%d][%s]\n", errno, strerror(errno));
		close(fd);
		return -1;
	}

	close(fd);
	return -1;
}
示例#3
0
/*
return
    0:  还没运行
    <0: 出错
    >0: 已运行
*/
int
already_running(void)
{
    int                 fd;
    char                buf[16];

    fd = fopen(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
    if(fd < 0){
        syslog(LOG_ERR, "can't open %s: %s", LOCKFILE, strerror(errno));
        return -1;
    }

    if(lockfile(fd) < 0){
        if(errno == EACCES || errno == EAGAIN){/*已运行了*/
            close(fd);
            return 1;
        }
        syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno));
        return -2;
    }

    ftruncate(fd, 0);/*将文件长度截短为0*/
    sprintf(buf, "%ld", (long)getpid());
    write(fd, buf, strlen(buf)+1);  /*将运行的进程号写到文件中*/
    return 0;   /**/
}
示例#4
0
int already_running(void)
{
	int fd;
	char buf[16];

	fd = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);
	if (fd < 0)
	{
		syslog(LOG_ERR, "can't open %s: %s", 
					LOCKFILE, strerror(errno));
		exit(1);
	}
	if (lockfile(fd) < 0)
	{
		if (errno == EACCES || errno == EAGAIN)
		{	
			close(fd);
			return (1);
		}
		syslog(LOG_ERR, "can't lock %s: %s",
			LOCKFILE, strerror(errno));
		exit(1);
	}
	ftruncate(fd, 0);
	sprintf(buf, "%ld", (long)getpid());
	write(fd, buf, strlen(buf)+1);
	return 0;
}
示例#5
0
int already_runing(void)
{
	int fd;
	char buf[16];

	fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
	if(fd<0)
	{
		printf("can't open %s, %s\n", LOCKFILE, strerror(errno));
		exit(1);
	}
	
	if(lockfile(fd)<0)
	{
		if(errno==EACCES || errno==EAGAIN)
		{
			printf("2222222222lockfile %s, %s\n", LOCKFILE, strerror(errno));
			close(fd);
			return 1;
		}
		
		printf("lockfile %s, %s\n", LOCKFILE, strerror(errno));
		exit(1);
	}

	printf("1111111111111111lockfile %s, %s\n", LOCKFILE, strerror(errno));
	ftruncate(fd, 0);
	sprintf(buf, "%ld", (long)getpid());
	write(fd, buf, strlen(buf)+1);

	return 0;
}
示例#6
0
static int lockfile(int fd) {
	struct flock fl = {.l_type = F_WRLCK, .l_start = 0, .l_whence = SEEK_SET, .l_len = 0};

	return fcntl(fd,F_SETLK, &fl);
}

static int already_running(void) {
	int fd;
	char *buf;

	fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
	if (fd < 0) {
		syslog(LOG_ERR,"can't open %s: %s",LOCKFILE,strerror(errno));
		exit(1);
	}
	if (lockfile(fd) < 0) {
		if (errno == EACCES || errno == EAGAIN) {
			close(fd);
			return 1;
		}
		syslog(LOG_ERR, "can't lock %s: %s",LOCKFILE, strerror(errno));
		exit(1);
	}
	ftruncate(fd,0);
	buf = g_strdup_printf("%ld",(long) getpid());
	write(fd,buf,strlen(buf)+1);
	g_free(buf);
	return 0;
}
示例#7
0
文件: bee-dep.c 项目: fpellanda/bee
void unlock(void)
{
    FILE *f;
    struct flock flo;

    f = lockfile();

    if (!f)
        return;

    flo.l_start  = flo.l_len = 0;
    flo.l_whence = SEEK_SET;
    flo.l_type   = F_UNLCK;

    rewind(f);

    if (ftruncate(fileno(f), 0) == -1) {
        perror("bee-dep: unlock: ftruncate");
        exit(EXIT_FAILURE);
    }

    if (fcntl(fileno(f), F_SETLK, &flo) == -1) {
        perror("bee-dep: unlock: fcntl");
        exit(EXIT_FAILURE);
    }

    if (fclose(f) == EOF) {
        perror("bee-dep: unlock: fclose");
        exit(EXIT_FAILURE);
    }
}
示例#8
0
/*
 * 检测守护进程是否已经运行
 */
int already_running() {
    int fd;
    char buf[16];

    fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
    if (fd < 0) {
        syslog(LOG_ERR, "cannt open %s: %s", LOCKFILE, strerror(errno));
        exit(1);
    } else {
        printf("open lockfile fail\n");
    }

    // 注意设置LOCK_NB为非阻塞的,未获取锁时返回出错
    // 为什么lockfile不能上锁??? 其他进程实例仍然可以加锁
    if (lockfile(fd) < 0) {
        printf("lock file fali, maybe daemon has already run?\n");
        if (errno == EACCES || error == EAGAIN) {
            close(fd);
        }
        syslog(LOG_ERR, "cannot lock %s, %s" , LOCKFILE, strerror(errno));
        exit(1);
    } else {
        printf("lock file success\n");
    }

    printf("start daemon\n");
    // 向文件中写入pid
    ftruncate(fd, 0);
    sprintf(buf, "%ld", (long)getpid());
    write(fd, buf, strlen(buf) + 1);
}
示例#9
0
文件: main.c 项目: danfrincu/booth
static int do_site(void)
{
	int fd;
	int rv = -1;

	if (!cl.debug) {
		if (daemon(0, 0) < 0) {
			perror("daemon error");
			exit(EXIT_FAILURE);
		}
	}

	setup_logging();
	fd = lockfile();
	if (fd < 0)
		return fd;

	log_info("BOOTH cluster site daemon started");
	set_scheduler();
	set_oom_adj(-16);

	rv = loop(SITE);
	if (rv < 0)
		goto fail;

	unlink_lockfile(fd);
	close_logging();

	return 0;

fail:
	return -1;
}
示例#10
0
/* return 0 if locking succeeded, -1 if failed */
static int
lockmnttab(void)
{
#ifdef MOUNT_TABLE_ON_FILE
  /* if mnttab file is locked, all is well */
  if (mntent_lock_fd >= 0)
    return 0;

  /* need to lock mnttab file. first, open the file */
  mntent_lock_fd = open(mtlckname, O_RDWR | O_CREAT, 0600);
  if (mntent_lock_fd < 0) {
    plog(XLOG_ERROR, "Unable to open/creat %s: %m", mtlckname);
    return -1;
  }

  /* if succeeded in opening the file, try to lock it */
  if (lockfile(mntent_lock_fd, F_WRLCK) < 0) {
    close(mntent_lock_fd);
    mntent_lock_fd = -1;
#ifdef DEBUG
    dlog("lock %s failed: %m", mtlckname);
#endif /* DEBUG */
    return -1;
  }
#else /* not MOUNT_TABLE_ON_FILE */
  /* fake lock for in-kernel mount table */
#endif /* not MOUNT_TABLE_ON_FILE */

  /* finally, succeeded in also locking the file */
  return 0;
}
示例#11
0
KEYRING *pgpdb_open(char *keyring, BUFFER *encryptkey, int writer, int type)
{
  KEYRING *keydb;

  assert(! ((writer) && (type == PGP_TYPE_UNDEFINED)));
  keydb = pgpdb_new(keyring, -1, encryptkey, type);
#ifdef DEBUG
  keydb->writer = writer;
#endif
  if (writer)
    keydb->lock = lockfile(keyring);
  keydb->filetype = pgp_readkeyring(keydb->db, keyring);
#if 0
  if (keydb->filetype == -1) {
    pgpdb_close(keydb);
    return (NULL);
  }
#endif /* if 0 */
  if (encryptkey && encryptkey->length && pgp_isconventional(keydb->db) &&
      pgp_decrypt(keydb->db, encryptkey, NULL, NULL, NULL) < 0) {
    user_delpass();
    return (NULL);
  }
  return (keydb);
}
示例#12
0
int already_running(void)
{
	int fd;
	char buf[16];

	fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
	if(fd < 0) {
		syslog(LOG_ERR, "can't open %s : %s", LOCKFILE, strerror(errno));
		exit(1);
	}

	if(lockfile(fd) < 0) {
		if(errno == EACCES || errno == EAGAIN) {
			close(fd);
			return(1);
		}

		syslog(LOG_ERR, "can't lock %s : %s", LOCKFILE, strerror(errno));
		exit(1);
	}

	/*
	 * Truncate the file so that when you write out the buffer, nothing that
	 * was longer than the buffer is still left in the file.
	 */
	ftruncate(fd, 0);
	sprintf(buf, "%ld", (long)getpid());
	write(fd, buf, strlen(buf) + 1);
	return(0);
}
示例#13
0
void main(int argc,char **argv)
{
  int fdlock;
  unsigned long delay;
  (void) umask(022);
  sig_pipeignore();
  when = now();

  getconfopt(argc,argv,options,1,0);
  if (flagreturn < 0)
    /* default to returning timed-out messages */
    flagreturn = !getconf_isset("noreturnposts");

  getconf_line(&modtime,"modtime",0);
  if (!stralloc_0(&modtime)) die_nomem();
  scan_ulong(modtime.s,&delay);
  if (!delay) delay = DELAY_DEFAULT;
  else if (delay < DELAY_MIN) delay = DELAY_MIN;
  else if (delay > DELAY_MAX) delay = DELAY_MAX;
  older = (unsigned long) when - 3600L * delay;	/* delay is in hours */

  fdlock = lockfile("mod/lock");

  dodir("mod/pending/",flagreturn);
  dodir("mod/accepted/",0);
  dodir("mod/rejected/",0);
  dodir("mod/unconfirmed/",0);
  _exit(0);
}
示例#14
0
int already_running(const char *filename)
{
	int fd;
	char buf[16];

	fd = open(filename, O_RDWR | O_CREAT, LOCKMODE);
	if (fd < 0) {
		printf("can't open %s: %m\n", filename);
		exit(1);
	}

	/* 鍏堣幏鍙栨枃浠堕攣 */
	if (lockfile(fd) == -1) {
		if (errno == EACCES || errno == EAGAIN) {
			printf("file: %s already locked", filename);
			close(fd);
			return 1;
		}
		printf("can't lock %s: %m\n", filename);
		exit(1);
	}

	/* 鍐欏叆杩愯瀹炰緥鐨刾id */
	ftruncate(fd, 0);
	sprintf(buf, "%ld", (long)getpid());
	write(fd, buf, strlen(buf) + 1);
	return 0;
}
int already_running(void)
{
     int        fd = 0;
     char       buf[16] = {0};

     fd = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);
     if (fd < 0)
     {
         syslog(LOG_ERR, "can't open %s: %s", LOCKFILE, strerror(errno));
         exit(1);
     }

     if (lockfile(fd) < 0)
     {
          //??????
          if (errno == EACCES || errno == EAGAIN)
          {
              syslog(LOG_ERR, "daemon is already_running");
              close(fd);
              return 1;
          }

          syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno));
          exit(1);
     }

     ftruncate(fd, 0);      //???P90
     sprintf(buf, "%ld", (long)getpid());
     write(fd, buf, strlen(buf) + 1);
     syslog(LOG_ERR, "write /var/run/daemon.pid: %s", buf);

     return 0;
}
示例#16
0
文件: daemon.c 项目: 52M/xSocks
int
already_running(const char *pidfile) {
	int	  fd;
    char  buf[16];

    fd = open(pidfile, O_RDWR | O_CREAT, LOCKMODE);
	if (fd < 0) {
		logger_stderr("open \"%s\" failed (%d: %s)", pidfile, errno, strerror(errno));
		exit(1);
	}
	if (lockfile(fd) < 0) {
		if (errno == EACCES || errno == EAGAIN) {
			close(fd);
			return(1);
		}
		logger_stderr("can't lock %s: %s", pidfile, strerror(errno));
		exit(1);
	}

    /*
     * create pid file
     */
    if (ftruncate(fd, 0)) {
		logger_stderr("can't truncate %s: %s", pidfile, strerror(errno));
		exit(1);
    }
    sprintf(buf, "%ld\n", (long)getpid());
    if (write(fd, buf, strlen(buf)+1) == -1) {
		logger_stderr("can't write %s: %s", pidfile, strerror(errno));
		exit(1);
    }

	return(0);
}
示例#17
0
int
already_running(const char *fpathname)
{
	int		fd;
	char	buf[16];
	
	fd = open(fpathname, O_RDWR|O_CREAT, LOCKMODE);
	if (fd < 0) {
		syslog(LOG_ERR, "can't open %s: %s", fpathname, strerror(errno));
		exit(1);
	}
	if (lockfile(fd) < 0) {
		if (errno == EACCES || errno == EAGAIN) {
			close(fd);
			return(1);
		}
		syslog(LOG_ERR, "can't lock %s: %s", fpathname, strerror(errno));
		exit(1);
	}

	//cut file length to 0.
	ftruncate(fd, 0);	
	
	//write pid to file.
	sprintf(buf, "%ld", (long)getpid());
	write(fd, buf, strlen(buf)+1);	
	return(0);
}
示例#18
0
文件: process1.c 项目: mwaldt/CSC412
void fileaccess(int fd, int count){
   void lockfile (void);
   void unlockfile (void);

   int i, k, value; pid_t pid;
   char buff[MAXSIZE];

   shmid = shmget(SHMKEY, sizeof(syncvars), PERMS | IPC_CREAT);
   shmptr = (syncvars *) shmat(shmid, (void *) NULL, 0);

   pid = getpid();

   for(i = 0; i < count; i++){
      lockfile();
      while(shmptr->turn == 0 && shmptr->flag[0] == TRUE );

      //critical stuff
      lseek(fd, 0L, 0);
      k = read(fd, buff, MAXSIZE); buff[k] = '\0';
      sscanf(buff, "%d\n", &value);
      value++;

      sprintf(buff, "%10d\n", value);
      lseek(fd, 0l, 0);
      k = strlen(buff); write(fd, buff, k);
      printf("pid = %d, new value = %d\n", pid, value);
      unlockfile();
   }
}
示例#19
0
/* Converts old versions of the filedb to the newest. Returns 1 if all went
 * well and otherwise 0. The new db is first written to a temporary place
 * and then moved over to the original db's position.
 *
 * Note: Unfortunately there is a small time-frame where aren't locking the
 *       DB, but want to replace it with a new one, using movefile().
 *       TODO: Copy old db to tmp file and then build the new db directly
 *             in the original file. This solves the tiny locking problem.
 *
 * Also remember to check the returned *fdb_s on failure, as it could be
 * NULL.
 */
static int convert_old_db(FILE ** fdb_s, char *filedb)
{
    filedb_top fdbt;
    FILE *fdb_t;
    int ret = 0;                  /* Default to 'failure' */

    filedb_readtop(*fdb_s, &fdbt);
    /* Old DB version? */
    if (fdbt.version > 0 && fdbt.version < FILEDB_VERSION3) {
        char *tempdb;

        putlog(LOG_MISC, "*", "Converting old filedb %s to newest format.",
               filedb);
        /* Create temp DB name */
        tempdb = nmalloc(strlen(filedb) + 5);
        simple_sprintf(tempdb, "%s-tmp", filedb);

        fdb_t = fopen(tempdb, "w+b");       /* Open temp DB         */
        if (fdb_t) {
            filedb_initdb(fdb_t);     /* Initialise new DB    */

            /* Convert old database to new one, saving
             * in temporary db file
             */
            if (fdbt.version == FILEDB_VERSION1)
                convert_version1(*fdb_s, fdb_t);        /* v1 -> v3             */
            else
                convert_version2(*fdb_s, fdb_t);        /* v2 -> v3             */

            unlockfile(*fdb_s);
            fclose(fdb_t);
            fclose(*fdb_s);

            /* Move over db to new location */
            if (movefile(tempdb, filedb))
                putlog(LOG_MISC, "*", "(!) Moving file db from %s to %s failed.",
                       tempdb, filedb);

            *fdb_s = fopen(filedb, "r+b");    /* Reopen new db        */
            if (*fdb_s) {
                lockfile(*fdb_s);
                /* Now we should have recreated the original situation,
                 * with the file pointer just pointing to the new version
                 * of the DB instead of the original one.
                 */
                ret = 1;
            } else
                putlog(LOG_MISC, "*", "(!) Reopening db %s failed.", filedb);
        }
        my_free(tempdb);
        /* Database already at the newest version? */
    } else if (fdbt.version == FILEDB_VERSION3)
        ret = 1;
    else
        putlog(LOG_MISC, "*", "(!) Unknown db version: %d", fdbt.version);
    if (!ret)
        putlog(LOG_MISC, "*", "Conversion of filedb %s failed.", filedb);
    return ret;
}
示例#20
0
文件: ezmlm-get.c 项目: abh/ezmlm-idx
void lockup(void)
/* lock unless locked */
{
  if(!flaglocked) {
    fdlock = lockfile("lock");
    flaglocked = 1;
  }
}
示例#21
0
/* errstr may be NULL, this might be called before GTK is initialized */
gboolean
cf_lock( gtr_lockfile_state_t * tr_state, char ** errstr )
{
    char *         path = getLockFilename( );
    const gboolean didLock = lockfile( path, tr_state, errstr );

    if( didLock )
        gl_lockpath = g_strdup( path );
    g_atexit( cf_removelocks );
    g_free( path );
    return didLock;
}
示例#22
0
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
文件: bee-dep.c 项目: fpellanda/bee
void lock(void)
{
    FILE *f;
    struct flock flo;

    f = lockfile();

    flo.l_start  = flo.l_len = 0;
    flo.l_whence = SEEK_SET;
    flo.l_type   = F_WRLCK;

    if (fcntl(fileno(f), F_SETLKW, &flo) == -1) {
        perror("bee-dep: lock");
        exit(EXIT_FAILURE);
    }
}
示例#24
0
void get_lock_or_die(char *pipename, char* lockname, int pid)
{
  int lock_state;
  do {
    lock_state=lockfile(lockname,241);
    if (!lock_state) {
      fprintf(stderr,"Can't get lock on %s\n",lockname);
      exit(1);
    }
    if (lock_state==2) {
      if (!checkPid(pid)) {
        unlink(pipename);
        unlink(lockname);
        exit(1);
      };
    }
  } while (lock_state==2);
}
示例#25
0
int handle_upload(connection *con, uint32_t size, void* data) {
	uploadfilemsg_req *msg = (uploadfilemsg_req*)data;

	//cat & validate filename
	char realpath[MAX_STORAGED_PATH_LEN+1];
	assert(NULL != concatpath(realpath, msg->data, msg->filename_len));

	char *err;
        bool r = isvalidfile(&realpath[scontent_len], &err);
	if(!r){
		L_DEBUG("refuse invalid filename");
		return false;
	}
	
	L_DEBUG("recv upload file req %s\n", realpath);
	//lock file then write
	lockfile(realpath, scontent_len + msg->filename_len);
	
	//TODO
	assert(true == mkdirs(realpath));
	int fd = open(realpath ,O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
	//TODO deal with write result
	int w_len = write(fd, (void*)&msg->data[msg->filename_len], msg->data_len);
	fsync(fd);
	close(fd);

	unlockfile(realpath, scontent_len + msg->filename_len);
	

	//send res packet
	int rp_size =msg->filename_len + uploadfilemsg_res_packetsize;
	packet_header *head = generatebuffer(rp_size, CLIENT_PROTO_RES_UPLOAD_FILE);
	//TODO
	assert(NULL != head);
	uploadfilemsg_res *rp = (uploadfilemsg_res *)head->data;
	rp->filename_len = msg->filename_len;
	//TODO define result value
	rp->result = 0;
	memcpy(rp->data, msg->data, msg->filename_len);
	//TODO deal with send result
	sendpacket(con, head);
}
示例#26
0
int main(int argc,char *argv[])
{
  int fd;
  int match;
  unsigned long msgsize = 0L;
  int opt;
  stralloc line = {0};
  substdio ssin;
  char inputbuf[4096];
  unsigned long msgnum;
  unsigned long cumsize;


  opt = getconfopt(argc,argv,options,1,0);
  switch (argc - opt) {
    case 0:
      substdio_fdbuf(&ssin,read,0,inputbuf,sizeof inputbuf);
      break;
    case 1:
      if ((fd = open_read(argv[opt])) == -1)
        strerr_die2sys(111,FATAL,MSG1(ERR_OPEN,argv[opt]));
      substdio_fdbuf(&ssin,read,fd,inputbuf,sizeof inputbuf);
      break;
    default:
      die_usage();
  }

  lockfile("lock");

  getconf_ulong2(&msgnum,&cumsize,"num",0);
  
  fd = 0;
  while (getln(&ssin,&line,&match,'\n') == 0 && match) {
    if (line.len > 5
	&& byte_diff(line.s,5,"From ") == 0) {
      flushit(fd);
      ++msgnum;
      cumsize += (msgsize + 128L) >> 8;
      msgsize = 0L;
      fd = openone(msgnum);
    }
    else if (fd > 0) {
static
lockmnttab(void)
{
  if (mtlckf == 0) {		/* need lock on /etc/.mnt.lock */
    mtlckf = open(mtlckname, O_RDWR);
    if (mtlckf >= 0) {
      if (lockfile(mtlckf, F_WRLCK) < 0) {
	close(mtlckf);
	mtlckf = 0;
#ifdef DEBUG
	dlog("lock failed %m");
#endif /* DEBUG */
      } else {
	return 0;
      }
    }
  }
  plog(XLOG_ERROR, "Unable to lock %s: %m", mtlckname);
  return -1;
}
示例#28
0
int handle_download(connection *con, uint32_t size, void* data) {
	downloadfilemsg_req *msg = (downloadfilemsg_req *)data;
	
	//cat & validate filename
	char realpath[MAX_STORAGED_PATH_LEN+1];
	assert(NULL != concatpath(realpath, msg->data, msg->filename_len));

	char *err;
        bool r = isvalidfile(&realpath[scontent_len],&err);
	if(!r){
		L_DEBUG("refuse invalid filename");
		return false;
	}

	L_DEBUG("recv download file req %s\n", realpath);
	//get file length
	lockfile(realpath, scontent_len + msg->filename_len);
	int fd = open(realpath ,O_RDONLY);
	int rpdata_len = lseek(fd, 0, SEEK_END);
	//TODO
	assert(rpdata_len >= 0);

	//send res packet
	int rp_size =rpdata_len + downloadfilemsg_res_packetsize;
	packet_header *head = generatebuffer(rp_size, CLIENT_PROTO_RES_DOWNLOAD_FILE);
	//TODO
        assert(NULL != head);
	uploadfilemsg_res *rp = (downloadfilemsg_res *)head->data;
	rp->filename_len = msg->filename_len;
	memcpy(rp->data, msg->data, msg->filename_len);

	int r_len = read(fd, (void *)&rp->data[msg->filename_len], rpdata_len);	
	//TODO
	assert(r_len >= 0);
	close(fd);

	//TODO deal with send result
        sendpacket(con, head);

	unlockfile(realpath, scontent_len + msg->filename_len);
}
示例#29
0
文件: main.c 项目: aspiers/booth
static int do_server(int type)
{
	int fd = -1;
	int rv = -1;

	rv = setup(type);
	if (rv < 0)
		goto out;

	if (!daemonize) {
		if (daemon(0, 0) < 0) {
			perror("daemon error");
			exit(EXIT_FAILURE);
		}
	}

	/*
	  The lock cannot be obtained before the call to daemon(), otherwise
	  the lockfile would contain the pid of the parent, not the daemon.
	*/
	fd = lockfile();
	if (fd < 0)
		return fd;

	if (type == ARBITRATOR)
		log_info("BOOTH arbitrator daemon started");
	else if (type == SITE)
		log_info("BOOTH cluster site daemon started");

	set_scheduler();
	set_oom_adj(-16);

	rv = loop();

out:
	if (fd >= 0)
		unlink_lockfile(fd);

	return rv;
}
示例#30
0
int main(int argc,char *argv[])
{
  int fd;
  int match;
  unsigned long msgsize = 0L;
  int opt;

  opt = getconfopt(argc,argv,options,1,0);
  if (argc - opt != 1)
    die_usage();

  if ((fd = open_read(argv[opt])) == -1)
    strerr_die2sys(111,FATAL,MSG1(ERR_OPEN,argv[opt]));
  substdio_fdbuf(&ssin,read,fd,inputbuf,sizeof inputbuf);

  lockfile("lock");

  getconf_ulong2(&msgnum,&cumsize,"num",0);
  
  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_die2sys(111,FATAL,MSG1(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;
    }
  }