1117395Skan/* ----------------------------------------------------------------------- * 2132718Skan * 3117395Skan * Copyright 2003-2005 H. Peter Anvin - All Rights Reserved 4117395Skan * 5132718Skan * Permission is hereby granted, free of charge, to any person 6117395Skan * obtaining a copy of this software and associated documentation 7132718Skan * files (the "Software"), to deal in the Software without 8132718Skan * restriction, including without limitation the rights to use, 9132718Skan * copy, modify, merge, publish, distribute, sublicense, and/or 10132718Skan * sell copies of the Software, and to permit persons to whom 11117395Skan * the Software is furnished to do so, subject to the following 12132718Skan * conditions: 13132718Skan * 14132718Skan * The above copyright notice and this permission notice shall 15132718Skan * be included in all copies or substantial portions of the Software. 16117395Skan * 17132718Skan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18132718Skan * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19169689Skan * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20169689Skan * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21117395Skan * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22132718Skan * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23117395Skan * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24117395Skan * OTHER DEALINGS IN THE SOFTWARE. 25132718Skan * 26117395Skan * ----------------------------------------------------------------------- */ 27117395Skan 28132718Skan#include <errno.h> 29117395Skan#include <stdio.h> 30117395Skan#include <stdlib.h> 31132718Skan#include <unistd.h> 32117395Skan#include <getopt.h> 33117395Skan#include <signal.h> 34117395Skan#include <ctype.h> 35117395Skan#include <string.h> 36117395Skan#include <paths.h> 37117395Skan#include <sysexits.h> 38117395Skan#include <sys/types.h> 39117395Skan#include <sys/file.h> 40132718Skan#include <sys/time.h> 41117395Skan#include <sys/wait.h> 42117395Skan#include <fcntl.h> 43132718Skan 44117395Skan#define PACKAGE_STRING "util-linux-ng 2.18" 45117395Skan#define _(x) (x) 46132718Skan 47117395Skanstatic const struct option long_options[] = { 48 { "shared", 0, NULL, 's' }, 49 { "exclusive", 0, NULL, 'x' }, 50 { "unlock", 0, NULL, 'u' }, 51 { "nonblocking", 0, NULL, 'n' }, 52 { "nb", 0, NULL, 'n' }, 53 { "timeout", 1, NULL, 'w' }, 54 { "wait", 1, NULL, 'w' }, 55 { "close", 0, NULL, 'o' }, 56 { "help", 0, NULL, 'h' }, 57 { "version", 0, NULL, 'V' }, 58 { 0, 0, 0, 0 } 59}; 60 61const char *program; 62 63static void usage(int ex) 64{ 65 fputs("flock (" PACKAGE_STRING ")\n", stderr); 66 fprintf(stderr, 67 _("Usage: %1$s [-sxun][-w #] fd#\n" 68 " %1$s [-sxon][-w #] file [-c] command...\n" 69 " %1$s [-sxon][-w #] directory [-c] command...\n" 70 " -s --shared Get a shared lock\n" 71 " -x --exclusive Get an exclusive lock\n" 72 " -u --unlock Remove a lock\n" 73 " -n --nonblock Fail rather than wait\n" 74 " -w --timeout Wait for a limited amount of time\n" 75 " -o --close Close file descriptor before running command\n" 76 " -c --command Run a single command string through the shell\n" 77 " -h --help Display this text\n" 78 " -V --version Display version\n"), 79 program); 80 exit(ex); 81} 82 83 84static sig_atomic_t timeout_expired = 0; 85 86static void timeout_handler(int sig) 87{ 88 (void)sig; 89 90 timeout_expired = 1; 91} 92 93 94static char * strtotimeval(const char *str, struct timeval *tv) 95{ 96 char *s; 97 long fs; /* Fractional seconds */ 98 int i; 99 100 tv->tv_sec = strtol(str, &s, 10); 101 fs = 0; 102 103 if ( *s == '.' ) { 104 s++; 105 106 for ( i = 0 ; i < 6 ; i++ ) { 107 if ( !isdigit(*s) ) 108 break; 109 110 fs *= 10; 111 fs += *s++ - '0'; 112 } 113 114 for ( ; i < 6; i++ ) 115 fs *= 10; 116 117 while ( isdigit(*s) ) 118 s++; 119 } 120 121 tv->tv_usec = fs; 122 return s; 123} 124 125int main(int argc, char *argv[]) 126{ 127 struct itimerval timeout, old_timer; 128 int have_timeout = 0; 129 int type = LOCK_EX; 130 int block = 0; 131 int fd = -1; 132 int opt, ix; 133 int do_close = 0; 134 int err; 135 int status; 136 char *eon; 137 char **cmd_argv = NULL, *sh_c_argv[4]; 138 const char *filename = NULL; 139 struct sigaction sa, old_sa; 140 141 program = argv[0]; 142 143 if ( argc < 2 ) 144 usage(EX_USAGE); 145 146 memset(&timeout, 0, sizeof timeout); 147 148 optopt = 0; 149 while ( (opt = getopt_long(argc, argv, "+sexnouw:hV?", long_options, &ix)) != EOF ) { 150 switch(opt) { 151 case 's': 152 type = LOCK_SH; 153 break; 154 case 'e': 155 case 'x': 156 type = LOCK_EX; 157 break; 158 case 'u': 159 type = LOCK_UN; 160 break; 161 case 'o': 162 do_close = 1; 163 break; 164 case 'n': 165 block = LOCK_NB; 166 break; 167 case 'w': 168 have_timeout = 1; 169 eon = strtotimeval(optarg, &timeout.it_value); 170 if ( *eon ) 171 usage(EX_USAGE); 172 break; 173 case 'V': 174 printf("flock (%s)\n", PACKAGE_STRING); 175 exit(0); 176 default: 177 /* optopt will be set if this was an unrecognized option, i.e. *not* 'h' or '?' */ 178 usage(optopt ? EX_USAGE : 0); 179 break; 180 } 181 } 182 183 if ( argc > optind+1 ) { 184 /* Run command */ 185 186 if ( !strcmp(argv[optind+1], "-c") || 187 !strcmp(argv[optind+1], "--command") ) { 188 189 if ( argc != optind+3 ) { 190 fprintf(stderr, _("%s: %s requires exactly one command argument\n"), 191 program, argv[optind+1]); 192 exit(EX_USAGE); 193 } 194 195 cmd_argv = sh_c_argv; 196 197 cmd_argv[0] = getenv("SHELL"); 198 if ( !cmd_argv[0] || !*cmd_argv[0] ) 199 cmd_argv[0] = _PATH_BSHELL; 200 201 cmd_argv[1] = "-c"; 202 cmd_argv[2] = argv[optind+2]; 203 cmd_argv[3] = 0; 204 } else { 205 cmd_argv = &argv[optind+1]; 206 } 207 208 filename = argv[optind]; 209 fd = open(filename, O_RDONLY|O_NOCTTY|O_CREAT, 0666); 210 /* Linux doesn't like O_CREAT on a directory, even though it should be a 211 no-op */ 212 if (fd < 0 && errno == EISDIR) 213 fd = open(filename, O_RDONLY|O_NOCTTY); 214 215 if ( fd < 0 ) { 216 err = errno; 217 fprintf(stderr, _("%s: cannot open lock file %s: %s\n"), 218 program, argv[optind], strerror(err)); 219 exit((err == ENOMEM||err == EMFILE||err == ENFILE) ? EX_OSERR : 220 (err == EROFS||err == ENOSPC) ? EX_CANTCREAT : 221 EX_NOINPUT); 222 } 223 224 } else if (optind < argc) { 225 /* Use provided file descriptor */ 226 227 fd = (int)strtol(argv[optind], &eon, 10); 228 if ( *eon || !argv[optind] ) { 229 fprintf(stderr, _("%s: bad number: %s\n"), program, argv[optind]); 230 exit(EX_USAGE); 231 } 232 233 } else { 234 /* Bad options */ 235 236 fprintf(stderr, _("%s: requires file descriptor, file or directory\n"), 237 program); 238 exit(EX_USAGE); 239 } 240 241 242 if ( have_timeout ) { 243 if ( timeout.it_value.tv_sec == 0 && 244 timeout.it_value.tv_usec == 0 ) { 245 /* -w 0 is equivalent to -n; this has to be special-cased 246 because setting an itimer to zero means disabled! */ 247 248 have_timeout = 0; 249 block = LOCK_NB; 250 } else { 251 memset(&sa, 0, sizeof sa); 252 253 sa.sa_handler = timeout_handler; 254 sa.sa_flags = SA_RESETHAND; 255 sigaction(SIGALRM, &sa, &old_sa); 256 257 setitimer(ITIMER_REAL, &timeout, &old_timer); 258 } 259 } 260 261 while ( flock(fd, type|block) ) { 262 switch( (err = errno) ) { 263 case EWOULDBLOCK: /* -n option set and failed to lock */ 264 exit(1); 265 case EINTR: /* Signal received */ 266 if ( timeout_expired ) 267 exit(1); /* -w option set and failed to lock */ 268 continue; /* otherwise try again */ 269 default: /* Other errors */ 270 if ( filename ) 271 fprintf(stderr, "%s: %s: %s\n", program, filename, strerror(err)); 272 else 273 fprintf(stderr, "%s: %d: %s\n", program, fd, strerror(err)); 274 exit((err == ENOLCK||err == ENOMEM) ? EX_OSERR : EX_DATAERR); 275 } 276 } 277 278 if ( have_timeout ) { 279 setitimer(ITIMER_REAL, &old_timer, NULL); /* Cancel itimer */ 280 sigaction(SIGALRM, &old_sa, NULL); /* Cancel signal handler */ 281 } 282 283 status = 0; 284 285 if ( cmd_argv ) { 286 pid_t w, f; 287 288 /* Clear any inherited settings */ 289 signal(SIGCHLD, SIG_DFL); 290 f = fork(); 291 292 if ( f < 0 ) { 293 err = errno; 294 fprintf(stderr, _("%s: fork failed: %s\n"), program, strerror(err)); 295 exit(EX_OSERR); 296 } else if ( f == 0 ) { 297 if ( do_close ) 298 close(fd); 299 err = errno; 300 execvp(cmd_argv[0], cmd_argv); 301 /* execvp() failed */ 302 fprintf(stderr, "%s: %s: %s\n", program, cmd_argv[0], strerror(err)); 303 _exit((err == ENOMEM) ? EX_OSERR: EX_UNAVAILABLE); 304 } else { 305 do { 306 w = waitpid(f, &status, 0); 307 if (w == -1 && errno != EINTR) 308 break; 309 } while ( w != f ); 310 311 if (w == -1) { 312 err = errno; 313 status = EXIT_FAILURE; 314 fprintf(stderr, "%s: waitpid failed: %s\n", program, strerror(err)); 315 } else if ( WIFEXITED(status) ) 316 status = WEXITSTATUS(status); 317 else if ( WIFSIGNALED(status) ) 318 status = WTERMSIG(status) + 128; 319 else 320 status = EX_OSERR; /* WTF? */ 321 } 322 } 323 324 return status; 325} 326 327