File: lockfile.c

package info (click to toggle)
flang 20181226-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 181,072 kB
  • sloc: cpp: 1,182,685; ansic: 598,652; objc: 103,775; f90: 57,054; python: 15,041; fortran: 13,601; lisp: 10,416; perl: 2,460; asm: 2,148; sh: 1,544; awk: 995; cs: 565; xml: 403; lex: 295; makefile: 225; pascal: 130
file content (193 lines) | stat: -rw-r--r-- 5,432 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file 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.
 *
 */

/*
 * Four routines to be able to lock access to a file in the Linux
 * approved manner.  This seems to be the only method that works
 * over NFS:
 *
 *  __pg_make_lock_file( char* dir )
 *	Creates a uniquely-named file in the given directory.
 *	To work, this must be created on the same filesystem
 *	as the file which we are attempting to lock.
 *	If there are multiple processes running each competing for the
 *	same lock, each gets a unique file here.
 *	return -1 if it can't get a unique file in 50 tries
 *  __pg_get_lock( char* lname )
 *	The argument is the name of the lock.
 *	Each process tries to create a hard link with this name
 *	to its own uniquely-named file from __pg_make_lock_file().
 *	The one that succeeds is the new lock owner.  The others
 *	fail and try again.  There is a fail-over to handle the case
 *	where the process with the lock dies, which is inherently unsafe,
 *	but we haven't come up with a better solution.
 *  __pg_release_lock( char* lname )
 *	The argument is the same name for the lock.
 *	The lock is released by deleting (calling unlink) for the
 *	hard link we had just created.
 *  __pg_delete_lock_file()
 *	Clean up by deleting the uniquely named file we had created earlier.
 */

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "lockfile.h"

/*
 * The name of the uniquely-named file and directory in which it is created
 */
static char *uname = NULL;
static char *udir = NULL;

/*
 * Some counts for debugging
 */
static long uwaiting;

int
__pg_make_lock_file(char *dir)
{
  pid_t pid;
  char hostname[999];
  int r;
  FILE *F;
  udir = (char *)malloc(strlen(dir) + 1);
  strcpy(udir, dir);
  pid = getpid();
  r = gethostname(hostname, 998);
  if (r != 0) {
    fprintf(stderr, "gethostname fails\n");
    exit(1);
  }
  uname = (char *)malloc(strlen(hostname) + strlen(udir) + 25);
  r = 0;
  do {
    ++r;
    /* create a unique file */
    sprintf(uname, "%s/lock.%s.%ld%d", udir, hostname, (long)pid, r);
    F = fopen(uname, "w");
  } while (F == NULL && r < 51);
  if (F == NULL) {
    return -1;
  }
  fprintf(F, "%s\n", uname);
  fclose(F);
  uwaiting = 0;
  return 0;
} /* __pg_make_lock_file */

void
__pg_get_lock(char *lname)
{
  int r;
  struct stat ss, ss1;
  int count;
  char *fullname;
  if (udir == NULL || uname == NULL)
    return;
  fullname = (char *)malloc(strlen(lname) + strlen(udir) +
                            2); /* +1 for / +1 for null terminator */
  strcpy(fullname, udir);
  strcat(fullname, "/");
  strcat(fullname, lname);
  memset(&ss1, 0, sizeof(ss1));
  count = 0;
  do {
    r = link(uname, fullname);
    if (r != 0) {
      /* link had some problem, see if the number of links
       * to the new file is now two */
      r = stat(uname, &ss);
      if (r == 0) {
        if (ss.st_nlink != 2) {
          if (ss.st_nlink != 1) {
            fprintf(stderr, "get_lock: %d links to %s\n", (int)ss.st_nlink,
                    uname);
            exit(1);
          }
          /* see if we should forcefully TAKE the lock */
          r = lstat(fullname, &ss);
          if (count == 0) {
            memcpy(&ss1, &ss, sizeof(ss));
            count = 1;
          } else if (ss1.st_dev != ss.st_dev || ss1.st_ino != ss.st_ino ||
                     ss1.st_mode != ss.st_mode || ss1.st_uid != ss.st_uid ||
                     ss1.st_gid != ss.st_gid || ss1.st_size != ss.st_size ||
                     ss1.st_mtime != ss.st_mtime) {
            /* some else got there first */
            memcpy(&ss1, &ss, sizeof(ss));
            count = 1;
          } else {
            ++count;
            if (count > 20) {
              /* we've waited long enough. */
              r = unlink(fullname);
              /* ignore errors on the unlink */
            }
          }
          sleep(1);
          r = 1;
        }
      }
      if (r)
        ++uwaiting;
    }
  } while (r != 0);
  free(fullname);
} /* __pg_get_lock */

void
__pg_release_lock(char *lname)
{
  int r;
  char *fullname;
  if (udir == NULL || uname == NULL)
    return;
  fullname = (char *)malloc(strlen(lname) + strlen(udir) + 2);
  strcpy(fullname, udir);
  strcat(fullname, "/");
  strcat(fullname, lname);
  /* release the lock by deleting the link */
  r = unlink(fullname);
  if (r != 0) {
    fprintf(stderr, "release_lock: unlink %s fails\n", lname);
    exit(1);
  }
  free(fullname);
} /* __pg_release_lock */

void
__pg_delete_lock_file()
{
  int r;
  /* delete the unique file */
  r = unlink(uname);
  if (r != 0) {
    fprintf(stderr, "delete_lock_file: unlink %s fails\n", uname);
    exit(1);
  }
  free(uname);
  free(udir);
  uname = NULL;
  udir = NULL;
} /* __pg_delete_lock_file */