File: lockfile.cc

package info (click to toggle)
trafficserver 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,428 kB
  • sloc: cpp: 222,273; sh: 12,193; ansic: 8,967; makefile: 1,809; perl: 1,158; java: 277; lex: 124; yacc: 63; sed: 6
file content (311 lines) | stat: -rw-r--r-- 7,532 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you 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.
 */

#include "ink_platform.h"
#include "ink_lockfile.h"

#if defined(linux)
#include "ink_killall.h"
#endif

#define LOCKFILE_BUF_LEN 16     // 16 bytes should be enought for a pid

int
Lockfile::Open(pid_t * holding_pid)
{
  char buf[LOCKFILE_BUF_LEN];
  pid_t val;
  int err;
  *holding_pid = 0;

#define FAIL(x) \
{ \
  if (fd > 0) \
    close (fd); \
  return (x); \
}

  struct flock lock;
  char *t;
  int size;

  fd = -1;

  // Try and open the Lockfile. Create it if it does not already
  // exist.
  do {
    fd = open(fname, O_RDWR | O_CREAT, 0644);
  } while ((fd < 0) && (errno == EINTR));

  if (fd < 0)
    return (-errno);

  // Lock it. Note that if we can't get the lock EAGAIN will be the
  // error we receive.
  lock.l_type = F_WRLCK;
  lock.l_start = 0;
  lock.l_whence = SEEK_SET;
  lock.l_len = 0;

  do {
    err = fcntl(fd, F_SETLK, &lock);
  } while ((err < 0) && (errno == EINTR));

  if (err < 0) {
    // We couldn't get the lock. Try and read the process id of the
    // process holding the lock from the lockfile.
    t = buf;

    for (size = 15; size > 0;) {
      do {
        err = read(fd, t, size);
      } while ((err < 0) && (errno == EINTR));

      if (err < 0)
        FAIL(-errno);
      if (err == 0)
        break;

      size -= err;
      t += err;
    }

    *t = '\0';

    // coverity[secure_coding]
    if (sscanf(buf, "%d\n", (int*)&val) != 1) {
      *holding_pid = 0;
    } else {
      *holding_pid = val;
    }
    FAIL(0);
  }
  // If we did get the lock, then set the close on exec flag so that
  // we don't accidently pass the file descriptor to a child process
  // when we do a fork/exec.
  do {
    err = fcntl(fd, F_GETFD, 0);
  } while ((err < 0) && (errno == EINTR));

  if (err < 0)
    FAIL(-errno);

  val = err | FD_CLOEXEC;

  do {
    err = fcntl(fd, F_SETFD, val);
  } while ((err < 0) && (errno == EINTR));

  if (err < 0)
    FAIL(-errno);

  // Return the file descriptor of the opened lockfile. When this file
  // descriptor is closed the lock will be released.

  return (1);                   // success

#undef FAIL
}

int
Lockfile::Get(pid_t * holding_pid)
{
  char buf[LOCKFILE_BUF_LEN];
  int err;
  *holding_pid = 0;


  fd = -1;

  // Open the Lockfile and get the lock. If we are successful, the
  // return value will be the file descriptor of the opened Lockfile.
  err = Open(holding_pid);
  if (err != 1)
    return err;

  if (fd < 0) {
    return -1;
  }
  // Truncate the Lockfile effectively erasing it.
  do {
    err = ftruncate(fd, 0);
  } while ((err < 0) && (errno == EINTR));

  if (err < 0) {
    close(fd);
    return (-errno);
  }
  // Write our process id to the Lockfile.
  snprintf(buf, sizeof(buf), "%d\n", (int) getpid());

  do {
    err = write(fd, buf, strlen(buf));
  } while ((err < 0) && (errno == EINTR));

  if (err != (int) strlen(buf)) {
    close(fd);
    return (-errno);
  }

  return (1);                   // success
}

void
Lockfile::Close(void)
{
  if (fd != -1) {
    close(fd);
  }
}

//-------------------------------------------------------------------------
// Lockfile::Kill() and Lockfile::KillAll()
//
// Open the lockfile. If we succeed it means there was no process
// holding the lock. We'll just close the file and release the lock
// in that case. If we don't succeed in getting the lock, the
// process id of the process holding the lock is returned. We
// repeatedly send the KILL signal to that process until doing so
// fails. That is, until kill says that the process id is no longer
// valid (we killed the process), or that we don't have permission
// to send a signal to that process id (the process holding the lock
// is dead and a new process has replaced it).
//
// INKqa11325 (Kevlar: linux machine hosed up if specific threads
// killed): Unfortunately, it's possible on Linux that the main PID of
// the process has been successfully killed (and is waiting to be
// reaped while in a defunct state), while some of the other threads
// of the process just don't want to go away.  Integrate ink_killall
// into Kill() and KillAll() just to make sure we really kill
// everything and so that we don't spin hard while trying to kill a
// defunct process.
//-------------------------------------------------------------------------

static void
lockfile_kill_internal(pid_t init_pid, int init_sig, pid_t pid, const char *pname, int sig)
{
  int err;

#if defined(linux)

  pid_t *pidv;
  int pidvcnt;

  // Need to grab pname's pid vector before we issue any kill signals.
  // Specifically, this prevents the race-condition in which
  // traffic_manager spawns a new traffic_server while we still think
  // we're killall'ing the old traffic_server.
  if (pname) {
    ink_killall_get_pidv_xmalloc(pname, &pidv, &pidvcnt);
  }

  if (init_sig > 0) {
    kill(init_pid, init_sig);
    // sleep for a bit and give time for the first signal to be
    // delivered
    sleep(1);
  }

  do {
    if ((err = kill(pid, sig)) == 0) {
      sleep(1);
    }
    if (pname && (pidvcnt > 0)) {
      ink_killall_kill_pidv(pidv, pidvcnt, sig);
      sleep(1);
    }
  } while ((err == 0) || ((err < 0) && (errno == EINTR)));

  if (pidv) {
    xfree(pidv);
  }

#else

  if (init_sig > 0) {
    kill(init_pid, init_sig);
    // sleep for a bit and give time for the first signal to be
    // delivered
    sleep(1);
  }

  do {
    err = kill(pid, sig);
  } while ((err == 0) || ((err < 0) && (errno == EINTR)));

#endif  // linux check

}

void
Lockfile::Kill(int sig, int initial_sig, const char *pname)
{
  int err;
  int pid;
  pid_t holding_pid;

  err = Open(&holding_pid);
  if (err == 1)                 // success getting the lock file
  {
    Close();
  } else if (err == 0)          // someone else has the lock
  {
    pid = holding_pid;
    if (pid != 0) {
      lockfile_kill_internal(pid, initial_sig, pid, pname, sig);
    }
  }
}

void
Lockfile::KillGroup(int sig, int initial_sig, const char *pname)
{
  int err;
  int pid;
  pid_t holding_pid;

  err = Open(&holding_pid);
  if (err == 1)                 // success getting the lock file
  {
    Close();
  } else if (err == 0)          // someone else has the lock
  {
    do {
      pid = getpgid(holding_pid);
    } while ((pid < 0) && (errno == EINTR));

    if (pid < 0) {
      pid = holding_pid;
    } else {
      pid = -pid;
    }

    if (pid != 0) {
      // We kill the holding_pid instead of the process_group
      // initially since there is no point trying to get core files
      // from a group since the core file of one overwrites the core
      // file of another one
      lockfile_kill_internal(holding_pid, initial_sig, pid, pname, sig);
    }
  }
}