55d84fefd3396aa545a13665daae3facc07b654a
[gapil.git] / sources / Flock.c
1 /* Flock.c
2  * 
3  * Copyright (C) 2001 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * Program Flock.c: 
22  * Program to test file locking
23  *
24  * Author: Simone Piccardi
25  * Nov. 2002
26  *
27  * Usage: flock -h give all info's
28  *
29  * $Id: Flock.c,v 1.3 2002/11/18 23:54:01 piccardi Exp $
30  *
31  *****************************************************************************/
32 /*
33  * Include needed headers
34  */
35 #include <errno.h>       /* error definitions and routines */ 
36 #include <stdlib.h>      /* C standard library */
37 #include <unistd.h>      /* unix standard library */
38 #include <stdio.h>       /* standard I/O library */
39 #include <string.h>      /* string functions */
40 #include <fcntl.h>       /* string functions */
41 /* user defined header */
42 #include "macros.h"      /* some useful macros */
43
44 /* Help printing routine */
45 void usage(void);
46
47 int main(int argc, char *argv[])
48 {
49 /* 
50  * Variables definition  
51  */
52     int type = F_UNLCK;            /* lock type: default to unlock (invalid) */
53     off_t start = 0;             /* start of the locked region: default to 0 */
54     off_t len = 0;              /* length of the locked region: default to 0 */
55     int fd, res, i;                                    /* internal variables */
56     int bsd = 0;                          /* semantic type: default to POSIX */
57     int cmd = F_SETLK;              /* lock command: default to non-blocking */
58     struct flock lock;                                /* file lock structure */
59     /*
60      * Input section: decode command line parameters 
61      * Use getopt function
62      */
63     opterr = 0;  /* don't want writing to stderr */
64     while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
65         switch (i) {
66         /* 
67          * Handling options 
68          */ 
69         case 'h':                                            /* help option */
70             printf("Wrong -h option use\n");
71             usage();
72             return -1;
73             break;
74         case 's':                            /* take start point of the lock */
75             start = strtol(optarg, NULL, 10);               /* convert input */
76             break;
77         case 'l':                                 /* take length of the lock */
78             len = strtol(optarg, NULL, 10);                 /* convert input */
79             break;
80         case 'w':                                  /* set type to write lock */
81             type = F_WRLCK;
82             break;
83         case 'r':                                   /* set type to read lock */
84             type = F_RDLCK;
85             break;
86         case 'b':                                    /* set lock to blocking */
87             cmd = F_SETLKW;
88             break;
89         case 'f':                                     /* enable BSD semantic */
90             bsd = 1;
91             break;
92         case '?':                                    /* unrecognized options */
93             printf("Unrecognized options -%c\n",optopt);
94             usage();
95         default:                                       /* should not reached */
96             usage();
97         }
98     }
99     /* ***********************************************************
100      * 
101      *           Options processing completed
102      *
103      *                Main code beginning
104      * 
105      * ***********************************************************/
106     if ((argc - optind) != 1) {          /* There must be remaing parameters */
107         printf("Wrong number of arguments %d\n", argc - optind);
108         usage();
109     }
110     if (type == F_UNLCK) {            /* There must be a -w or -r option set */
111         printf("You should set a read or a write lock\n");
112         usage();
113     }
114     fd = open(argv[optind], O_RDONLY);         /* open the file to be locked */
115     if (fd < 0) {                                           /* on error exit */
116         perror("Wrong filename");
117         exit(1);
118     }
119     /* do lock */
120     if (bsd) {                                             /* if BSD locking */
121         /* rewrite cmd for suitables flock operation values */ 
122         if (cmd == F_SETLK) {                              /* if no-blocking */
123             cmd = LOCK_NB;              /* set the value for flock operation */
124         } else {                                                     /* else */
125             cmd = 0;                                      /* default is null */
126         }
127         if (type == F_RDLCK) cmd |= LOCK_SH;          /* set for shared lock */
128         if (type == F_WRLCK) cmd |= LOCK_EX;       /* set for exclusive lock */
129         res = flock(fd, cmd);                                /* esecute lock */
130     } else {                                             /* if POSIX locking */
131         /* setting flock structure */
132         lock.l_type = type;                       /* set type: read or write */
133         lock.l_whence = SEEK_SET;    /* start from the beginning of the file */
134         lock.l_start = start;          /* set the start of the locked region */
135         lock.l_len = len;             /* set the length of the locked region */
136         res = fcntl(fd, cmd, &lock);                              /* do lock */
137     }
138     /* check lock results */
139     if (res) {                                              /* on error exit */
140         perror("Failed lock");
141         exit(1);
142     } else {                                           /* else write message */
143         printf("Lock acquired\n");
144     }
145     pause();                       /* stop the process, use a signal to exit */
146     return 0;
147 }
148 /*
149  * routine to print usage info and exit
150  */
151 void usage(void) {
152     printf("Program flock: lock a region of a file \n");
153     printf("Usage:\n");
154     printf("  forktest [-h] [-s start] [-l len] [-w|-r] filename \n");
155     printf("  -h           print this help\n");
156     printf("  -s start     region starting byte\n");
157     printf("  -l len       region length (0 means all file)\n");
158     printf("  -w           write lock\n");
159     printf("  -r           read lock\n");
160     printf("  -b           block when locking impossible\n");
161     printf("  -f           enable BSD semantic\n");
162     exit(1);
163 }