00001 /** @file 00002 00003 Definitions for the Lockfile class. 00004 00005 @section license License 00006 00007 Licensed to the Apache Software Foundation (ASF) under one 00008 or more contributor license agreements. See the NOTICE file 00009 distributed with this work for additional information 00010 regarding copyright ownership. The ASF licenses this file 00011 to you under the Apache License, Version 2.0 (the 00012 "License"); you may not use this file except in compliance 00013 with the License. You may obtain a copy of the License at 00014 00015 http://www.apache.org/licenses/LICENSE-2.0 00016 00017 Unless required by applicable law or agreed to in writing, software 00018 distributed under the License is distributed on an "AS IS" BASIS, 00019 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 See the License for the specific language governing permissions and 00021 limitations under the License. 00022 */ 00023 00024 #ifndef __INK_LOCKFILE_H__ 00025 #define __INK_LOCKFILE_H__ 00026 00027 #include "ink_defs.h" 00028 #include "ink_string.h" 00029 00030 #define COP_LOCK "cop.lock" 00031 #define MANAGER_LOCK "manager.lock" 00032 #define SERVER_LOCK "server.lock" 00033 00034 class Lockfile 00035 { 00036 public: 00037 Lockfile(void):fd(0) 00038 { 00039 fname[0] = '\0'; 00040 } 00041 00042 // coverity[uninit_member] 00043 Lockfile(const char *filename):fd(0) 00044 { 00045 ink_strlcpy(fname, filename, sizeof(fname)); 00046 } 00047 00048 ~Lockfile(void) 00049 { 00050 } 00051 00052 void SetLockfileName(const char *filename) 00053 { 00054 ink_strlcpy(fname, filename, sizeof(fname)); 00055 } 00056 00057 const char *GetLockfileName(void) 00058 { 00059 return fname; 00060 } 00061 00062 // Open() 00063 // 00064 // Tries to open a lock file, returning: 00065 // -errno on error 00066 // 0 if someone is holding the lock (with holding_pid set) 00067 // 1 if we now have a writable lock file 00068 int Open(pid_t * holding_pid); 00069 00070 // Get() 00071 // 00072 // Gets write access to a lock file, and if successful, truncates 00073 // file, and writes the current process ID. Returns: 00074 // -errno on error 00075 // 0 if someone is holding the lock (with holding_pid set) 00076 // 1 if we now have a writable lock file 00077 int Get(pid_t * holding_pid); 00078 00079 // Close() 00080 // 00081 // Closes the file handle on the opened Lockfile. 00082 void Close(void); 00083 00084 // Kill() 00085 // KillGroup() 00086 // 00087 // Ensures no one is holding the lock. It tries to open the lock file 00088 // and if that does not succeed, it kills the process holding the lock. 00089 // If the lock file open succeeds, it closes the lock file releasing 00090 // the lock. 00091 // 00092 // The intial signal can be used to generate a core from the process while 00093 // still ensuring it dies. 00094 void Kill(int sig, int initial_sig = 0, const char *pname = NULL); 00095 void KillGroup(int sig, int initial_sig = 0, const char *pname = NULL); 00096 00097 private: 00098 char fname[PATH_NAME_MAX]; 00099 int fd; 00100 }; 00101 00102 #endif // __LOCK_FILE_H__