aboutsummaryrefslogtreecommitdiffstats
path: root/pgaas/src/testlock/testlock.c
blob: 41bea591461b3afd762d333e20c730b6c8897615 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
    Usage: testlock [-t timeout] [-s] filename command args ...

    Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this code except in compliance
    with the License. You may obtain a copy of the License
    at http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software  
    distributed under the License is distributed on an "AS IS" BASIS,  
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or  
    implied. See the License for the specific language governing  
    permissions and limitations under the License. 

*/

#include <sys/file.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>

void usage(const char *prog0, const char *msg) 
{
  if (msg) fprintf(stderr, "%s\n", msg);
  fprintf(stderr, "Usage: %s [-v] [-t timeout] [-s] [-r retcode] lock-filename command args ...\n", prog0);
  fprintf(stderr, "-t ##\thow long to wait for a lock to be freed. 0 means exit immediately\n");
  fprintf(stderr, "-s\tsilently ignore errors with locking\n");
  fprintf(stderr, "-r ##\texit with this code when the lock fails\n");
  fprintf(stderr, "-v\tbe verbose\n");
  fprintf(stderr, "Note:\tlock-filename is created (if it does not exist) and truncated before locking.\n");
  fprintf(stderr, "\tlock-filename is not removed after the command finishes\n");
  exit(1);
}

int main(int argc, char **argv) 
{
  const char *prog0 = argv[0];

  int c;
  int timeout = -1;
  bool silent = false;
  bool verbose = false;
  int nolockret = 99;

  while ((c = getopt(argc, argv, "t:r:sv?")) != -1) {
    switch (c) {
    case 's': silent = true; break;
    case 't': timeout = atoi(optarg); break;
    case 'r': nolockret = atoi(optarg); break;
    case 'v': verbose = true; break;
    default: usage(prog0, NULL);
    }
  }

  argc -= optind;
  argv += optind;

  if (argc < 1) {
    usage(prog0, "Missing lock filename");
  } else if (argc < 2) {
    usage(prog0, "Missing command to run");
  }

  const char *lockFilename = *argv++;
  if (verbose) printf("lockfilename=%s\n", lockFilename);

  int lockfd = creat(lockFilename, 0666);
  if (lockfd < 0) {
    fprintf(stderr, "Cannot open %s: %s\n", lockFilename, strerror(errno));
    exit(2);
  }

  if (timeout < 0) {
    /* wait forever */
    lockf(lockfd, F_LOCK, 0);
  } else {
    /* try each second (for up to timeout seconds) to get the lock */
    int lockret = lockf(lockfd, F_TLOCK, 0);
    int count = 0;
    while ((lockret < 0) && (count++ < timeout)) {
      sleep(1);
      lockret = lockf(lockfd, F_TLOCK, 0);
    }
    if (lockret < 0) {
      if (!silent) {
	fprintf(stderr, "Cannot lock %s: %s\n", lockFilename, strerror(errno));
      }
      exit(nolockret);
    }
  }

  /* now execute the given command */
  if (verbose) {
    char **a = argv;
    printf("calling program '%s'\n", *a);
    while (*++a) {
      printf("with argument '%s'\n", *a);
    }
  }
  execvp(argv[0], argv);
}