File: ink_lockfile.h

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 (117 lines) | stat: -rw-r--r-- 2,939 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
/** @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.
 */

/**************************************
 *
 * ink_lockfile.h
 *   Definitions for the Lockfile class.
 *
 * $Date: 2006-03-31 04:25:35 $
 *
 *
 */

#ifndef __INK_LOCKFILE_H__
#define __INK_LOCKFILE_H__

#ifndef INK_LOCKFILE_INCLUDE_REDUCED
#include "ink_resource.h"
#endif
#include "ink_port.h"
#include "ink_string.h"

#ifndef INK_LOCKFILE_INCLUDE_REDUCED
#define COP_LOCK       "cop.lock"
#define MANAGER_LOCK   "manager.lock"
#define SERVER_LOCK    "server.lock"
#endif

class Lockfile
{
public:
  Lockfile(void):fd(0)
  {
    fname[0] = '\0';
  }

  // coverity[uninit_member]
  Lockfile(const char *filename):fd(0)
  {
    ink_strncpy(fname, filename, sizeof(fname));
  }

  ~Lockfile(void)
  {
  }

  void SetLockfileName(const char *filename)
  {
    ink_strncpy(fname, filename, sizeof(fname));
  }

  const char *GetLockfileName(void)
  {
    return fname;
  }

  // Open()
  //
  // Tries to open a lock file, returning:
  //   -errno on error
  //   0 if someone is holding the lock (with holding_pid set)
  //   1 if we now have a writable lock file
  int Open(pid_t * holding_pid);

  // Get()
  //
  // Gets write access to a lock file, and if successful, truncates
  // file, and writes the current process ID.  Returns:
  //   -errno on error
  //   0 if someone is holding the lock (with holding_pid set)
  //   1 if we now have a writable lock file
  int Get(pid_t * holding_pid);

  // Close()
  //
  // Closes the file handle on the opened Lockfile.
  void Close(void);

  // Kill()
  // KillGroup()
  //
  // Ensures no one is holding the lock. It tries to open the lock file
  // and if that does not succeed, it kills the process holding the lock.
  // If the lock file open succeeds, it closes the lock file releasing
  // the lock.
  //
  // The intial signal can be used to generate a core from the process while
  // still ensuring it dies.
  void Kill(int sig, int initial_sig = 0, const char *pname = NULL);
  void KillGroup(int sig, int initial_sig = 0, const char *pname = NULL);

private:
  char fname[PATH_MAX];
  int fd;
};

#endif // __LOCK_FILE_H__