3 * Copyright (C) 2001 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
22 * Program to test file locking
24 * Author: Simone Piccardi
27 * Usage: flock -h give all info's
29 *****************************************************************************/
31 * Include needed headers
33 #include <errno.h> /* error definitions and routines */
34 #include <stdlib.h> /* C standard library */
35 #include <unistd.h> /* unix standard library */
36 #include <stdio.h> /* standard I/O library */
37 #include <string.h> /* C strings library */
38 #include <fcntl.h> /* file control functions */
41 /* user defined header */
42 #include "macros.h" /* some useful macros */
44 /* Help printing routine */
47 int main(int argc, char *argv[])
50 * Variables definition
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 */
60 * Input section: decode command line parameters
63 opterr = 0; /* don't want writing to stderr */
64 while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
69 case 'h': /* help option */
70 printf("Wrong -h option use\n");
74 case 's': /* take start point of the lock */
75 start = strtol(optarg, NULL, 10); /* convert input */
77 case 'l': /* take length of the lock */
78 len = strtol(optarg, NULL, 10); /* convert input */
80 case 'w': /* set type to write lock */
83 case 'r': /* set type to read lock */
86 case 'b': /* set lock to blocking */
89 case 'f': /* enable BSD semantic */
92 case '?': /* unrecognized options */
93 printf("Unrecognized options -%c\n",optopt);
95 default: /* should not reached */
99 /* ***********************************************************
101 * Options processing completed
103 * Main code beginning
105 * ***********************************************************/
106 if ((argc - optind) != 1) { /* There must be remaing parameters */
107 printf("Wrong number of arguments %d\n", argc - optind);
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");
114 fd = open(argv[optind], O_RDONLY); /* open the file to be locked */
115 if (fd < 0) { /* on error exit */
116 perror("Wrong filename");
120 if (bsd) { /* BSD locking */
121 /* rewrite cmd for suitables flock operation values */
122 if (cmd == F_SETLK) { /* if no-blocking set for flock operation */
124 } else { /* else default is null */
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 { /* 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 */
138 /* check lock results */
139 if (res) { /* on error exit */
140 perror("Failed lock");
142 } else { /* else write message */
143 printf("Lock acquired\n");
145 pause(); /* stop the process, use a signal to exit */
149 * routine to print usage info and exit
152 printf("Program flock: lock a region of a file \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");