"Fossies" - the Fresh Open Source Software Archive

Member "glusterfs-8.6/tests/bugs/locks/issue-2443-crash.c" (20 Aug 2021, 1378 Bytes) of package /linux/misc/glusterfs-8.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <sys/file.h>
    2 #include <stdio.h>
    3 #include <string.h>
    4 #include <errno.h>
    5 #include <sys/types.h>
    6 #include <sys/stat.h>
    7 #include <fcntl.h>
    8 
    9 int
   10 main(int argc, char *argv[])
   11 {
   12     int fd = -1;
   13     char *filename = NULL;
   14     struct flock lock = {
   15         0,
   16     };
   17     int i = 0;
   18     int ret = -1;
   19 
   20     if (argc != 2) {
   21         fprintf(stderr, "Usage: %s <filename> ", argv[0]);
   22         goto out;
   23     }
   24 
   25     filename = argv[1];
   26 
   27     fd = open(filename, O_RDWR | O_CREAT, 0);
   28     if (fd < 0) {
   29         fprintf(stderr, "open (%s) failed (%s)\n", filename, strerror(errno));
   30         goto out;
   31     }
   32 
   33     lock.l_start = 0;
   34     lock.l_type = F_RDLCK;
   35     lock.l_whence = SEEK_SET;
   36     lock.l_len = 2;
   37 
   38     ret = fcntl(fd, F_SETLK, &lock);
   39     if (ret < 0) {
   40         fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
   41         goto out;
   42     }
   43 
   44     lock.l_start = 2;
   45     lock.l_type = F_WRLCK;
   46     lock.l_whence = SEEK_SET;
   47     lock.l_len = 2;
   48 
   49     ret = fcntl(fd, F_SETLK, &lock);
   50     if (ret < 0) {
   51         fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
   52         goto out;
   53     }
   54 
   55     lock.l_start = 0;
   56     lock.l_type = F_RDLCK;
   57     lock.l_whence = SEEK_SET;
   58     lock.l_len = 4;
   59 
   60     ret = fcntl(fd, F_SETLK, &lock);
   61     if (ret < 0) {
   62         fprintf(stderr, "fcntl setlk failed (%s)\n", strerror(errno));
   63         goto out;
   64     }
   65 out:
   66     return ret;
   67 }