int t54(char *name) { char file[MAX_PATH_LENGTH] = ""; struct flock lock; int fd, err; ENTER("fcntl should return 0 when succeed in getting flock"); snprintf(file, MAX_PATH_LENGTH, "%s/test_t54_file", lustre_path); t_echo_create(file, "fcntl should return 0 when succeed"); fd = open(file, O_RDWR); if (fd < 0) { printf("\nerror open file: %s\n", strerror(errno)); return(-1); } lock.l_type = F_WRLCK; lock.l_start = 0; lock.l_whence = 0; lock.l_len = 1; if ((err = t_fcntl(fd, F_SETLKW, &lock)) != 0) { fprintf(stderr, "fcntl returned: %d (%s)\n", err, strerror(err)); close(fd); t_unlink(file); return (-1); } lock.l_type = F_UNLCK; t_fcntl(fd, F_SETLKW, &lock); close(fd); t_unlink(file); LEAVE(); }
void* t2_thread1(void *arg) { struct flock *lock = ((th_data *)arg)->lock; int fd = ((th_data *)arg)->fd; printf("thread 1: set write lock (blocking)\n"); lock->l_type = F_WRLCK; t_fcntl(fd, F_SETLKW, lock); printf("thread 1: set write lock done\n"); t_fcntl(fd, F_GETLK, lock); printf("thread 1: unlock\n"); lock->l_type = F_UNLCK; t_fcntl(fd, F_SETLK, lock); printf("thread 1: unlock done\n"); return 0; }
int t2(int argc, char* argv[]) { struct flock lock = { .l_type = F_RDLCK, .l_whence = SEEK_SET, }; char file[MAX_PATH_LENGTH] = ""; int fd, rc; pthread_t th1, th2; th_data ta; snprintf(file, MAX_PATH_LENGTH, "%s/test_t2_file", argv[2]); fd = open(file, O_RDWR|O_CREAT, (mode_t)0666); if (fd < 0) { fprintf(stderr, "error open file: %s\n", file); return EXIT_FAILURE; } t_fcntl(fd, F_SETFL, O_APPEND); rc = t_fcntl(fd, F_GETFL); if ((rc & O_APPEND) == 0) { fprintf(stderr, "error get flag: ret %x\n", rc); return EXIT_FAILURE; } ta.lock = &lock; ta.fd = fd; rc = pthread_create(&th1, NULL, t2_thread1, &ta); if (rc) { fprintf(stderr, "error create thread 1\n"); rc = EXIT_FAILURE; goto out; } rc = pthread_create(&th2, NULL, t2_thread2, &ta); if (rc) { fprintf(stderr, "error create thread 2\n"); rc = EXIT_FAILURE; goto out; } (void)pthread_join(th1, NULL); (void)pthread_join(th2, NULL); out: t_unlink(file); close(fd); return rc; }
int t21(char *name) { char file[MAX_PATH_LENGTH] = ""; int fd, ret; struct flock lock = { .l_type = F_RDLCK, .l_whence = SEEK_SET, }; ENTER("basic fcntl support"); snprintf(file, MAX_PATH_LENGTH, "%s/test_t21_file", lustre_path); fd = open(file, O_RDWR|O_CREAT, (mode_t)0666); if (fd < 0) { printf("error open file: %s\n", file); return(-1); } t_fcntl(fd, F_SETFL, O_APPEND); ret = t_fcntl(fd, F_GETFL); if ((ret & O_APPEND) == 0) { printf("error get flag: ret %o\n", ret); return(-1); } t_fcntl(fd, F_SETLK, &lock); t_fcntl(fd, F_GETLK, &lock); lock.l_type = F_WRLCK; t_fcntl(fd, F_SETLKW, &lock); t_fcntl(fd, F_GETLK, &lock); lock.l_type = F_UNLCK; t_fcntl(fd, F_SETLK, &lock); close(fd); t_unlink(file); LEAVE(); }