Figure per il deadlock, e programma di prova per il file locking
[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.1 2002/11/16 14:11:55 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
42
43 #include "macros.h"
44
45 /* Help printing routine */
46 void usage(void);
47
48 int main(int argc, char *argv[])
49 {
50 /* 
51  * Variables definition  
52  */
53     int type = F_UNLCK;
54     off_t start;
55     off_t len;
56     int fd, res, i;
57     int cmd = F_SETLK ;
58     struct flock lock; 
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:wrb")) != -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 wait time for childen */
75             start = strtol(optarg, NULL, 10);    /* convert input */
76             break;
77         case 'l':   /* take wait time for childen */
78             len = strtol(optarg, NULL, 10);   /* convert input */
79             break;
80         case 'w':
81             type = F_WRLCK;
82             break;
83         case 'r':
84             type = F_RDLCK;
85             break;
86         case 'b':
87             cmd = F_SETLKW;
88             break;
89         case '?':   /* unrecognized options */
90             printf("Unrecognized options -%c\n",optopt);
91             usage();
92         default:    /* should not reached */
93             usage();
94         }
95     }
96     /* ***********************************************************
97      * 
98      *           Options processing completed
99      *
100      *                Main code beginning
101      * 
102      * ***********************************************************/
103     /* There must be remaing parameters */
104     if (type == F_UNLCK) {
105         printf("You should set a read or a write lock\n");
106         usage();
107     }
108     if ((argc - optind) != 1) {
109         printf("Wrong number of arguments %d\n", argc - optind);
110         usage();
111     }
112     fd = open(argv[optind], O_RDWR);
113     if (fd < 0) {
114         perror("Wrong filename");
115         exit(1);
116     }
117     /* setting lock structure */
118     lock.l_type = type;
119     lock.l_whence = SEEK_SET;
120     lock.l_start = start;
121     lock.l_len = len;
122     /* do lock */
123     res = fcntl(fd, cmd, &lock);
124     if (res) {
125         perror("Failed lock");
126         exit(1);
127     }
128     pause();
129     return 0;
130 }
131 /*
132  * routine to print usage info and exit
133  */
134 void usage(void) {
135     printf("Program flock: lock a region of a file \n");
136     printf("Usage:\n");
137     printf("  forktest [-h] [-s start] [-l len] [-w|-r] filename \n");
138     printf("  -h           print this help\n");
139     printf("  -s start     region starting byte\n");
140     printf("  -l len       region length (0 means all file)\n");
141     printf("  -w           write lock\n");
142     printf("  -r           read lock\n");
143     printf("  -b           block when locking impossible\n");
144     
145     exit(1);
146 }