summaryrefslogtreecommitdiff
path: root/storage/ndb/src/common/portlib/NdbDaemon.c
blob: 22d59925db7d9933bcc4d14c41df2bda7c705ee9 (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
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
/* Copyright (c) 2003-2005 MySQL AB
   Use is subject to license terms

   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; version 2 of the License.

   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 Street, Fifth Floor, Boston, MA  02110-1301, USA */

#include <ndb_global.h>
#include "NdbDaemon.h"

#define NdbDaemon_ErrorSize 500
long NdbDaemon_DaemonPid = 0;
int NdbDaemon_ErrorCode = 0;
char NdbDaemon_ErrorText[NdbDaemon_ErrorSize] = "";

int
NdbDaemon_Make(const char* lockfile, const char* logfile, unsigned flags)
{
  int lockfd = -1, logfd = -1, n;
  char buf[64];

  (void)flags; /* remove warning for unused parameter */

  /* Check that we have write access to lock file */
  assert(lockfile != NULL);
  lockfd = open(lockfile, O_CREAT|O_RDWR, 0644);
  if (lockfd == -1) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"%s: open for write failed: %s", lockfile, strerror(errno));
    return -1;
  }
  /* Read any old pid from lock file */
  buf[0] = 0;
  n = read(lockfd, buf, sizeof(buf));
  if (n < 0) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"%s: read failed: %s", lockfile, strerror(errno));
    return -1;
  }
  NdbDaemon_DaemonPid = atol(buf);
  if (lseek(lockfd, 0, SEEK_SET) == -1) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"%s: lseek failed: %s", lockfile, strerror(errno));
    return -1;
  }
#ifdef F_TLOCK
  /* Test for lock before becoming daemon */
  if (lockf(lockfd, F_TLOCK, 0) == -1) 
  {
    if (errno == EACCES || errno == EAGAIN) {   /* results may vary */
      snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	       "%s: already locked by pid=%ld", lockfile, NdbDaemon_DaemonPid);
      return -1;
    }
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
        "%s: lock test failed: %s", lockfile, strerror(errno));
    return -1;
  }
#endif
  /* Test open log file before becoming daemon */
  if (logfile != NULL) {
    logfd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0644);
    if (logfd == -1) {
      NdbDaemon_ErrorCode = errno;
      snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	  "%s: open for write failed: %s", logfile, strerror(errno));
      return -1;
    }
  }
#ifdef F_TLOCK
  if (lockf(lockfd, F_ULOCK, 0) == -1) 
  {
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	     "%s: fail to unlock", lockfile);
    return -1;
  }
#endif
  
  /* Fork */
  n = fork();
  if (n == -1) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"fork failed: %s", strerror(errno));
    return -1;
  }
  /* Exit if we are the parent */
  if (n != 0) {
    exit(0);
  }
  /* Running in child process */
  NdbDaemon_DaemonPid = getpid();
  /* Lock the lock file (likely to succeed due to test above) */
  if (lockf(lockfd, F_LOCK, 0) == -1) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"%s: lock failed: %s", lockfile, strerror(errno));
    return -1;
  }
  /* Become process group leader */
  if (setsid() == -1) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"setsid failed: %s", strerror(errno));
    return -1;
  }
  /* Write pid to lock file */
  if (ftruncate(lockfd, 0) == -1) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"%s: ftruncate failed: %s", lockfile, strerror(errno));
    return -1;
  }
  sprintf(buf, "%ld\n", NdbDaemon_DaemonPid);
  n = strlen(buf);
  if (write(lockfd, buf, n) != n) {
    NdbDaemon_ErrorCode = errno;
    snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	"%s: write failed: %s", lockfile, strerror(errno));
    return -1;
  }
  /* Do input/output redirections (assume fd 0,1,2 not in use) */
  close(0);
  open("/dev/null", O_RDONLY);
  if (logfile != 0) {
    dup2(logfd, 1);
    dup2(logfd, 2);
    close(logfd);
  }
  /* Success */
  return 0;
}

#if 0
int
NdbDaemon_Make(const char* lockfile, const char* logfile, unsigned flags)
{
  /* Fail */
  snprintf(NdbDaemon_ErrorText, NdbDaemon_ErrorSize,
	   "Daemon mode not implemented");
  return -1;
}
#endif

#ifdef NDB_DAEMON_TEST

int
main()
{
  if (NdbDaemon_Make("test.pid", "test.log", 0) == -1) {
    fprintf(stderr, "NdbDaemon_Make: %s\n", NdbDaemon_ErrorText);
    return 1;
  }
  sleep(10);
  return 0;
}

#endif