Beispiel #1
0
int main(int argc, char *argv[])
{
	int fd;
	int ret;
	struct flock lock;
	char read_buf[32];

	// 打开或创建文件
	if ((fd = open("example_fc_lock", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) == -1) {
		my_err("open", __LINE__);
	}
	if (write(fd, "test lock", 10) != 10) {
		my_err("write", __LINE__);
	}

	// 初始化lock结构 锁整个文件
	memset(&lock, 0, sizeof(struct flock));
	lock.l_whence = SEEK_SET;
	lock.l_start = 0;
	lock.l_len = 0;

	// 设置读锁
	lock.l_type = F_RDLCK;
	if (lock_test(fd, &lock) == 0) {
		// 测试可以设置锁
		lock.l_type = F_RDLCK;
		lock_set(fd, &lock);
	}

	// 读数据
	lseek(fd, 0, SEEK_SET);
	if ((ret = read(fd, read_buf, 10)) < 0) {
		my_err("read", __LINE__);
	}
	read_buf[ret] = '\0';
	printf("%s\n", read_buf);

	// 按任意键
	getchar();

	// 设置写锁
	lock.l_type = F_WRLCK;
	if (lock_test(fd, &lock) == 0) {
		lock.l_type = F_WRLCK;
		lock_set(fd, &lock);
	}

	// 释放锁
	lock.l_type = F_UNLCK;
	lock_set(fd, &lock);
	close(fd);

	return 0;
}
Beispiel #2
0
int cstat_set_status(struct cstat *cstat)
{
	struct stat statp;
//logp("in set summary for %s\n", cstat->name);

	if(lstat(cstat->sdirs->lock->path, &statp))
	{
		if(lstat(cstat->sdirs->working, &statp))
			cstat->status=STATUS_IDLE;
		else
			cstat->status=STATUS_CLIENT_CRASHED;
		// It is not running, so free the running_detail.
		free_w(&cstat->running_detail);
	}
	else
	{
		if(!lock_test(cstat->sdirs->lock->path)) // Could have got lock.
		{
			cstat->status=STATUS_SERVER_CRASHED;
			// It is not running, so free the running_detail.
			free_w(&cstat->running_detail);
		}
		else
			cstat->status=STATUS_RUNNING;
	}

	return 0;
}
Beispiel #3
0
int main(int argc, char *argv[])
{
	int	fd;
	struct flock	lock;

	lock.l_type = F_WRLCK;
	lock.l_start = 0;
	lock.l_whence = SEEK_SET;
	lock.l_len = 0;
	
	/* 注意是5604 而不是2604 g+s,g-x, 设置T,S位的时候取掩码 */
	/* 需要挂载时,指定 -o mand选项 */
	if ((fd = open("./tmplock", O_RDWR|O_CREAT, 5604)) < 0) {
		fprintf(stderr, "open file error: %s\n", strerror(errno));
		return (errno);
	}
	if (lock_test(fd, F_WRLCK, 0, SEEK_SET, 0) != 0) {
		fprintf(stderr, "lock_test faile\n");
		return(errno);
	}

	if (fcntl(fd, F_SETLK, &lock) != 0) {
		fprintf(stderr, "lock file error: %s\n", strerror(errno));
		return(errno);
	}

	pause();
	
	return 0;
}
Beispiel #4
0
static void assert_can_get_lock(struct lock *lock)
{
	fail_unless(!lock_test(lockfile));
	lock_get_quick(lock);
	fail_unless(lock->status==GET_LOCK_GOT);
	fail_unless(!lock_release(lock));
	fail_unless(lock->status==GET_LOCK_NOT_GOT);
}
Beispiel #5
0
int main(void){
	int fd;
	int ret;
	struct flock lock;
	char read_buf[32];

	if((fd = open("example_65", O_CREAT|O_TRUNC|O_RDWR, S_IRWXU)) == -1){
		my_err("open", __LINE__);
	}
	if(write(fd, "test lock", 10) != 10){
		my_err("write", __LINE__);
	}
	memset(&lock, 0, sizeof(struct flock));
	lock.l_start = SEEK_SET;
	lock.l_whence = 0;
	lock.l_len = 0;

	lock.l_type = F_RDLCK;
	if(lock_test(fd, &lock) == 0){
		lock.l_type = F_RDLCK;
		lock_set(fd, &lock);
	}

	lseek(fd, 0, SEEK_SET);
	bzero(read_buf, 10);
	if((ret = read(fd, read_buf, 10)) < 0){
		printf("%d\n", errno);
		my_err("read", __LINE__);
	}

	read_buf[ret] = '\0';
	printf("%s\n", read_buf);

	getchar();

	lock.l_type = F_WRLCK;
	if(lock_test(fd, &lock) == 0){
		lock.l_type = F_WRLCK;
		lock_set(fd, &lock);
	}

	lock.l_type = F_UNLCK;
	lock_set(fd, &lock);
	close(fd);
	return 0;
}
Beispiel #6
0
static int connect_to_champ_chooser(struct sdirs *sdirs, struct conf **confs,
	int resume)
{
	int len;
	int s=-1;
	int tries=0;
	int tries_max=3;
	struct sockaddr_un remote;

	if(!lock_test(sdirs->champlock))
	{
		// Champ chooser is not running.
		// Try to fork a new champ chooser process.
		if(champ_chooser_fork(sdirs, confs, resume))
			return -1;
	}

	// Champ chooser should either be running now, or about to run.

	if((s=socket(AF_UNIX, SOCK_STREAM, 0))<0)
	{
		logp("socket error in %s: %s\n", __func__, strerror(errno));
		return -1;
	}

	memset(&remote, 0, sizeof(struct sockaddr_un));
	remote.sun_family=AF_UNIX;
	snprintf(remote.sun_path, sizeof(remote.sun_path),
		"%s", sdirs->champsock);
	len=strlen(remote.sun_path)+sizeof(remote.sun_family)+1;

	while(tries++<tries_max)
	{
		int sleeptimeleft=3;
		if(!connect(s, (struct sockaddr *)&remote, len))
		{
			logp("Connected to champ chooser.\n");
			return s;
		}

		// SIGCHLDs may be interrupting.
		sleeptimeleft=3;
		while(sleeptimeleft>0) sleeptimeleft=sleep(sleeptimeleft);
	}

	// Only log any error after all attempts failed, to make the logs
	// less worrying (most of the time, the first attempt will fail).
	logp("Could not connect to champ chooser on %s after %d attempts: %s\n",
		sdirs->champsock, tries_max, strerror(errno));

	return -1;
}
Beispiel #7
0
static void run_with_fork(int child_exit_early)
{
	int stat;
	struct lock *lock=setup();

	do_fork(child_exit_early);

	if(!child_exit_early)
	{
		sleep(1);
		fail_unless(lock_test(lockfile)==-1);
		lock_get_quick(lock);
		fail_unless(lock->status==GET_LOCK_NOT_GOT);
		wait(&stat);
	}

	// The child has exited, should now be able to get it.
	assert_can_get_lock(lock);
	tear_down(&lock, NULL);
}
Beispiel #8
0
void sys_log(char * string)
{
	int fd;
	char str[SIZE];
	time_t timep;
	struct tm *timenow;
	char log[SIZE];
	struct flock lock;

	memset(&lock, 0, sizeof(struct flock));
	lock.l_start = SEEK_SET;
	lock.l_whence = 0;
	lock.l_len = 0;

	lock.l_type = F_WRLCK;
	if (lock_test(fd, &lock) == 0)
	{
		lock.l_type = F_WRLCK;
		lock_set(fd, &lock);
	}
	strcpy(log, string);

	time (&timep);
	timenow = localtime(&timep);
	strcpy(str, asctime(timenow));
	fd = open("/home/qiong/shujia/Net/sys_log.txt", O_WRONLY | O_CREAT | O_APPEND, 0600);
	write(fd, "\n\n时间:", strlen("\n\n时间:"));
	write(fd, str, strlen(str)-1);
	write(fd, "\nip:", strlen("\nip:"));
	write(fd, ip_name, strlen(ip_name));
	write(fd, "\n操作:", strlen("\n操作:"));
	write(fd, log, strlen(log));

	lock.l_type = F_UNLCK;
	lock_set(fd, &lock);

	close(fd);
}
Beispiel #9
0
void cstat_set_run_status(struct cstat *cstat)
{
    struct stat statp;
    struct sdirs *sdirs=(struct sdirs *)cstat->sdirs;
    if(!cstat->permitted) return;

    if(lstat(sdirs->lock->path, &statp))
    {
        if(lstat(sdirs->working, &statp))
            cstat->run_status=RUN_STATUS_IDLE;
        else
            cstat->run_status=RUN_STATUS_CLIENT_CRASHED;
    }
    else
    {
        if(!lock_test(sdirs->lock->path)) // Could have got lock.
            cstat->run_status=RUN_STATUS_SERVER_CRASHED;
        else
            cstat->run_status=RUN_STATUS_RUNNING;
    }

    return;
}
Beispiel #10
0
int
main(int argc, char *argv[])
{
    glfs_t *fs = NULL;
    int ret = 0, i, status = 0;
    glfs_fd_t *fd1 = NULL;
    glfs_fd_t *fd2 = NULL;
    glfs_fd_t *fd3 = NULL;
    char *filename = "file_tmp";
    char *volname = NULL;
    char *logfile = NULL;
    char *hostname = NULL;

    if (argc != 4) {
        fprintf(stderr, "Invalid argument\n");
        exit(1);
    }

    hostname = argv[1];
    volname = argv[2];
    logfile = argv[3];

    fs = glfs_new(volname);
    if (!fs) {
        fprintf(stderr, "glfs_new: returned NULL\n");
        return -1;
    }

    ret = glfs_set_volfile_server(fs, "tcp", hostname, 24007);
    LOG_ERR("glfs_set_volfile_server", ret);

    ret = glfs_set_logging(fs, logfile, 7);
    LOG_ERR("glfs_set_logging", ret);

    ret = glfs_init(fs);
    LOG_ERR("glfs_init", ret);

    fd1 = glfs_creat(fs, filename, O_RDWR | O_SYNC, 0644);
    if (fd1 <= 0) {
        ret = -1;
        LOG_ERR("glfs_creat", ret);
    }
    fprintf(stderr, "glfs-create fd1 - %d\n", fd1);

    fd2 = glfs_dup(fd1);
    fprintf(stderr, "glfs-dup fd2 - %d\n", fd2);

    fd3 = glfs_open(fs, filename, O_RDWR | O_SYNC);
    if (fd2 <= 0) {
        ret = -1;
        LOG_ERR("glfs_open", ret);
    }
    fprintf(stderr, "glfs-open fd3 - %d\n", fd3);

    /* TEST 1: Conflicting ranges, same lk_owner
     * lock1 (0, 10, lownera)
     * lock2 (5, 10, lownera)
     * Expected: should not fail but get merged
     */
    ret = lock_test(fd1, fd2, false, 0, 10, lownera, 8, 5, 10, lownera, 8);
    LOG_ERR("==== glfs_lock_test_1", ret);

    /* TEST 2: Conflicting ranges, different lk_owner
     * lock1 (0, 10, lownera) - already taken
     * lock2 (5, 10, lownerb)
     * Expected: should fail and not get merged
     */
    ret = lock_test(NULL, fd2, true, 0, 10, lownera, 8, 5, 10, lownerb, 8);
    LOG_ERR("==== glfs_lock_test_2", ret);

    /* TEST 3: Different ranges, same lk_owner
     * lock1 (0, 10, lownera) - already taken
     * lock2 (30, 10, lownera)
     * Expected: should not fail
     */
    ret = lock_test(NULL, fd2, false, 0, 10, lownera, 8, 30, 10, lownera, 8);
    LOG_ERR("==== glfs_lock_test_3", ret);

    /* TEST 4: Conflicting ranges, different lk_owner
     * lock1 (0, 10, lownera) - already taken
     * lock2 (50, 10, lownerb)
     * Expected: should not fail
     */
    ret = lock_test(NULL, fd2, false, 0, 10, lownera, 8, 50, 10, lownerb, 8);
    LOG_ERR("==== glfs_lock_test_4", ret);

    /* TEST 5: Close fd1 & retry TEST2
     * lock1 (not applicable)
     * lock2 (5, 10, lownerb)
     * Expected: should succeed now
     */
    ret = glfs_close(fd1);
    LOG_ERR("glfs_close", ret);

    ret = lock_test(NULL, fd2, false, 0, 10, lownera, 8, 5, 10, lownerb, 8);
    LOG_ERR("==== glfs_lock_test_5", ret);

    /* TEST 6: Check closing fd1 doesn't flush fd2 locks
     * retry TEST 4 but with fd2 and fd3.
     * lock1 (50, 10, lownerb) - already taken
     * lock2 (55, 10, lownerc)
     * Expected: should fail
     */
    ret = lock_test(NULL, fd3, true, 50, 10, lownerb, 8, 55, 10, lownerc, 8);
    LOG_ERR("==== glfs_lock_test_6", ret);

err:
    ret = glfs_close(fd2);
    LOG_ERR("glfs_close", ret);

    ret = glfs_close(fd3);
    LOG_ERR("glfs_close", ret);

out:
    if (fs) {
        ret = glfs_fini(fs);
        fprintf(stderr, "glfs_fini(fs) returned %d\n", ret);
    }

    if (ret)
        exit(1);
    exit(0);
}