~ubuntu-branches/ubuntu/utopic/binkd/utopic-proposed

« back to all changes in this revision

Viewing changes to unix/daemonize.c

  • Committer: Bazaar Package Importer
  • Author(s): Marco d'Itri
  • Date: 2002-03-24 22:52:25 UTC
  • Revision ID: james.westby@ubuntu.com-20020324225225-7ru6itlapn03nl35
Tags: upstream-0.9.5a
ImportĀ upstreamĀ versionĀ 0.9.5a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: daemonize.c,v 2.2 2001/02/15 10:38:12 gul Exp $
 
3
 *
 
4
 * Revision history:
 
5
 * $Log: daemonize.c,v $
 
6
 * Revision 2.2  2001/02/15 10:38:12  gul
 
7
 * fix #include pathes
 
8
 *
 
9
 * Revision 2.1  2001/01/16 03:57:06  gul
 
10
 * Added HAVE_SYS_IOCTL_H
 
11
 *
 
12
 * Revision 2.0  2001/01/16 03:49:26  gul
 
13
 * *** empty log message ***
 
14
 *
 
15
 * Revision 1.1  2001/01/15 22:04:52  gul
 
16
 * Added -D switch (run as daemon)
 
17
 *
 
18
 */
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include <errno.h>
 
23
 
 
24
#ifdef HAVE_UNISTD_H
 
25
#include <unistd.h>
 
26
#endif
 
27
 
 
28
#ifdef HAVE_IOCTL_H
 
29
#include <sys/ioctl.h>
 
30
#endif
 
31
#include <sys/types.h>
 
32
 
 
33
#include "../tools.h"
 
34
#include "../daemonize.h"
 
35
 
 
36
/*                                                                      */
 
37
/* Daemonize binkd if we know how to do it                              */
 
38
/*                               Alex Semenyaka, Andrew Kolchoogin      */
 
39
 
 
40
int already_daemonized = 0;
 
41
 
 
42
int binkd_daemonize(int nochdir)
 
43
{
 
44
 
 
45
if (already_daemonized)
 
46
        return 0;
 
47
 
 
48
#ifdef HAVE_DAEMON
 
49
if (daemon(nochdir,0) == -1)
 
50
        { Log(2,"Daemon() failed, %s",strerror(errno)); return -1; }
 
51
 
 
52
#elif HAVE_SETSID
 
53
if (fork() != 0) exit(0);
 
54
if (setsid() == -1)
 
55
#ifdef ultrix
 
56
        /* Sendmail wisdom has been used */
 
57
        if ((setpgrp(0, 0) < 0) || (setsid() < 0))
 
58
#endif
 
59
        { Log(2,"Setsid() failed, %s",strerror(errno)); return -1; }
 
60
 
 
61
freopen("/dev/null","r",stdin);
 
62
freopen("/dev/null","w",stdout);
 
63
freopen("/dev/null","w",stderr);
 
64
 
 
65
if (!nochdir)
 
66
        chdir("/");
 
67
 
 
68
#elif HAVE_TIOCNOTTY
 
69
register int fd;
 
70
 
 
71
if (fork() != 0) exit(0);
 
72
if (setpgrp(0,0) <0)
 
73
        { Log(2,"Setpgrp failed, %s",strerror(errno)); return -1; }
 
74
 
 
75
if ((fd = open("/dev/tty", 2)) >= 0)
 
76
        {
 
77
                (void) ioctl(fd, TIOCNOTTY, (char*)0);
 
78
                (void) close(fd);
 
79
        }
 
80
   else
 
81
        { Log(2,"Cannot open /dev/tty, %s", strerror(errno)); return -1; }
 
82
 
 
83
if (!nochdir)
 
84
        chdir("/");
 
85
 
 
86
freopen("/dev/null","r",stdin);
 
87
freopen("/dev/null","w",stdout);
 
88
freopen("/dev/null","w",stderr);
 
89
 
 
90
#endif /* HAVE_TIOCNOTTY, HAVE_SETSID, HAVE_DAEMON */
 
91
 
 
92
/* BinkD is either daemonized here or OS does not support known methods to */
 
93
/* do it.                                                                  */
 
94
 
 
95
already_daemonized = 1;
 
96
 
 
97
return 0;
 
98
}
 
99