summaryrefslogtreecommitdiff
path: root/src/mt/lock.cc
blob: 5b72967dcf311f8c115175720007bc6997486a1a (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
111
112
113
114
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <mt/lock.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/utsname.h>
#include <signal.h>
using namespace std;

const mstring LOCK=".lock";

void LockFile(const mstring& path,int prot) throw(merror_t)
{
  mstring name=path+LOCK; int fd;
  while((fd=open(name.c_str(),O_CREAT|O_WRONLY|O_TRUNC|O_EXCL,prot))==-1&&
        errno==EEXIST) sleep(1);
  if(fd<0) THROW("lock: could not create lock file "<<name);
  close(fd);
}

int remove(const char* path);
void UnlockFile(const mstring& path)
{
  remove((path+LOCK).c_str());
}

///////////////////////////////////////////////////////////////////

proclock_t::proclock_t(const mstring& p,int prot) : pid(0),path(p+LOCK)
{
  mstring buf,name=path;
  int fd=open(path.c_str(),O_CREAT|O_WRONLY|O_TRUNC|O_EXCL,prot);
  if(fd<0) {
    if(errno==EEXIST) {
      buf.load(path);
      int oldpid=atoi(buf);
      if(kill(oldpid,0)==0) THROW("proclock: already running as "<<buf)
      else fd=open(path.c_str(),O_CREAT|O_WRONLY|O_TRUNC,prot);
    }
    else THROW("proclock: could not create lock file "<<path);
  }
  pid=getpid();
  buf.convert("%d",pid);
  write(fd,buf.data(),buf.size());
  close(fd);
}

proclock_t::~proclock_t()
{
  if(getpid()==pid) remove(path.c_str());
}

///////////////////////////////////////////////////////////////////

nodelock_t::nodelock_t(const mstring& p,int prot) : pid(0),path(p+LOCK)
{
  struct utsname uts; uname(&uts);
  mstring buf;
  int fd=open(path.c_str(),O_CREAT|O_WRONLY|O_TRUNC|O_EXCL,prot);
  if(fd<0) {
    if(errno==EEXIST) {
      if(buf.load(path)>=0) {
        if(buf.nempty()&&buf!=sref(uts.nodename))
          THROW("nodelock: already locked on "<<buf);
      }
      else {
        fd=open(path.c_str(),O_CREAT|O_WRONLY|O_TRUNC,prot);
        pid=getpid();
        buf=uts.nodename;
        write(fd,buf.data(),buf.size());
        close(fd);
      }
    }
    else THROW("nodelock: could not create lock file "<<path);
  }
  else {
    pid=getpid();
    buf=uts.nodename;
    write(fd,buf.data(),buf.size());
    close(fd);
  }
}

nodelock_t::~nodelock_t()
{
  if(getpid()==pid) remove(path.c_str());
}

///////////////////////////////////////////////////////////////////

void ProcTerminate(const mstring& p,int stime)
{
  mstring buf,path=p+LOCK;
  if(buf.load(path)>=0) while(!kill(atoi(buf),SIGTERM)) sleep(stime);
}