File: server_lock.c

package info (click to toggle)
netatalk 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,716 kB
  • sloc: ansic: 85,115; sh: 10,385; perl: 1,703; makefile: 1,363
file content (147 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
 * All rights reserved. See COPYRIGHT.
 *
 * Copyright (c) 1990,1993 Regents of The University of Michigan.
 * All Rights Reserved.  See COPYRIGHT.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#include <sys/time.h>

#include <atalk/compat.h>
#include <atalk/util.h>

static struct itimerval itimer;

/* this creates an open lock file which hangs around until the program
 * dies. it returns the pid. due to problems w/ solaris, this has
 * been changed to do the kill() thing. */
pid_t server_lock(char *program, char *pidfile, int debug)
{
  char buf[10];
  FILE *pf;
  pid_t pid;
  int mask;
  
  mask = umask(022);
  /* check for pid. this can get fooled by stale pid's. */
  if ((pf = fopen(pidfile, "r"))) {
    if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
      fprintf( stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
	       program, pid);      
      fclose(pf);
      return -1;
    }
    fclose(pf);
  }

  if ((pf = fopen(pidfile, "w")) == NULL) {
    fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
	    pidfile);
    return -1;
  }
  umask(mask);

  /*
   * Disassociate from controlling tty.
   */
  if ( !debug ) {
    int		i;

    getitimer(ITIMER_PROF, &itimer);
    switch (pid = fork()) {
    case 0 :
      setitimer(ITIMER_PROF, &itimer, NULL);
      fclose(stdin);
      fclose(stdout);
      fclose(stderr);
      i = open( "/dev/null", O_RDWR );
      i = open( "/dev/null", O_RDWR );
      i = open( "/dev/null", O_RDWR );

#ifdef TIOCNOTTY
      if (( i = open( "/dev/tty", O_RDWR )) >= 0 ) {
	(void)ioctl( i, TIOCNOTTY, 0 );
	setpgid( 0, getpid());
	(void) close(i);
      }
#else
      setpgid( 0, getpid());
#endif
      break;
    case -1 :  /* error */
      perror( "fork" );
    default :  /* server */
      fclose(pf);
      return pid;
    }
  } 

  fprintf(pf, "%d\n", getpid());
  fclose(pf);
  return 0;
}

/*!
 * Check lockfile
 */
int check_lockfile(const char *program, const char *pidfile)
{
    char buf[10];
    FILE *pf;
    pid_t pid;

    /* check for pid. this can get fooled by stale pid's. */
    if ((pf = fopen(pidfile, "r"))) {
        if (fgets(buf, sizeof(buf), pf) && !kill(pid = atol(buf), 0)) {
            fprintf(stderr, "%s is already running (pid = %d), or the lock file is stale.\n",
                    program, pid);      
            fclose(pf);
            return -1;
        }
        fclose(pf);
    }
    return 0;
}

/*!
 * Check and create lockfile
 */
int create_lockfile(const char *program, const char *pidfile)
{
    char buf[10];
    FILE *pf;
    pid_t pid;
    int mask;
  
    if (check_lockfile(program, pidfile) != 0)
        return -1;

    /* Write PID to pidfile */
    mask = umask(022);
    if ((pf = fopen(pidfile, "w")) == NULL) {
        fprintf(stderr, "%s: can't open lock file, \"%s\"\n", program,
                pidfile);
        return -1;
    }
    umask(mask);
    fprintf(pf, "%d\n", getpid());
    fclose(pf);

    return 0;
}