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 * $Id: Flock.c,v 1.5 2003/05/02 09:55:13 piccardi Exp $
31 *****************************************************************************/
33 * Include needed headers
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> /* C strings library */
40 #include <fcntl.h> /* file control functions */
43 /* user defined header */
44 #include "macros.h" /* some useful macros */
46 /* Help printing routine */
49 int main(int argc, char *argv[])
52 * Variables definition
54 int type = F_UNLCK; /* lock type: default to unlock (invalid) */
55 off_t start = 0; /* start of the locked region: default to 0 */
56 off_t len = 0; /* length of the locked region: default to 0 */
57 int fd, res, i; /* internal variables */
58 int bsd = 0; /* semantic type: default to POSIX */
59 int cmd = F_SETLK; /* lock command: default to non-blocking */
60 struct flock lock; /* file lock structure */
62 * Input section: decode command line parameters
65 opterr = 0; /* don't want writing to stderr */
66 while ( (i = getopt(argc, argv, "hs:l:wrbf")) != -1) {
71 case 'h': /* help option */
72 printf("Wrong -h option use\n");
76 case 's': /* take start point of the lock */
77 start = strtol(optarg, NULL, 10); /* convert input */
79 case 'l': /* take length of the lock */
80 len = strtol(optarg, NULL, 10); /* convert input */
82 case 'w': /* set type to write lock */
85 case 'r': /* set type to read lock */
88 case 'b': /* set lock to blocking */
91 case 'f': /* enable BSD semantic */
94 case '?': /* unrecognized options */
95 printf("Unrecognized options -%c\n",optopt);
97 default: /* should not reached */
101 /* ***********************************************************
103 * Options processing completed
105 * Main code beginning
107 * ***********************************************************/
108 if ((argc - optind) != 1) { /* There must be remaing parameters */
109 printf("Wrong number of arguments %d\n", argc - optind);
112 if (type == F_UNLCK) { /* There must be a -w or -r option set */
113 printf("You should set a read or a write lock\n");
116 fd = open(argv[optind], O_RDONLY); /* open the file to be locked */
117 if (fd < 0) { /* on error exit */
118 perror("Wrong filename");
122 if (bsd) { /* if BSD locking */
123 /* rewrite cmd for suitables flock operation values */
124 if (cmd == F_SETLK) { /* if no-blocking */
125 cmd = LOCK_NB; /* set the value for flock operation */
127 cmd = 0; /* default is null */
129 if (type == F_RDLCK) cmd |= LOCK_SH; /* set for shared lock */
130 if (type == F_WRLCK) cmd |= LOCK_EX; /* set for exclusive lock */
131 res = flock(fd, cmd); /* esecute lock */
132 } else { /* if POSIX locking */
133 /* setting flock structure */
134 lock.l_type = type; /* set type: read or write */
135 lock.l_whence = SEEK_SET; /* start from the beginning of the file */
136 lock.l_start = start; /* set the start of the locked region */
137 lock.l_len = len; /* set the length of the locked region */
138 res = fcntl(fd, cmd, &lock); /* do lock */
140 /* check lock results */
141 if (res) { /* on error exit */
142 perror("Failed lock");
144 } else { /* else write message */
145 printf("Lock acquired\n");
147 pause(); /* stop the process, use a signal to exit */
151 * routine to print usage info and exit
154 printf("Program flock: lock a region of a file \n");
156 printf(" forktest [-h] [-s start] [-l len] [-w|-r] filename \n");
157 printf(" -h print this help\n");
158 printf(" -s start region starting byte\n");
159 printf(" -l len region length (0 means all file)\n");
160 printf(" -w write lock\n");
161 printf(" -r read lock\n");
162 printf(" -b block when locking impossible\n");
163 printf(" -f enable BSD semantic\n");