File: daemonprocess.h.html

package info (click to toggle)
rudiments 0.31-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,516 kB
  • ctags: 3,248
  • sloc: asm: 23,776; cpp: 22,792; sh: 7,769; ansic: 1,769; makefile: 1,054; xml: 169; perl: 19
file content (115 lines) | stat: -rw-r--r-- 8,757 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
<html>
<head>
<title>~/src/firstworks/rudiments-0.31/include/rudiments/daemonprocess.h.html</title>
<meta name="Generator" content="Vim/7.0">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#0000ff">// Copyright (c) 1999-2002 David Muse</font>
<font color="#0000ff">// See the COPYING file for more information.</font>

<font color="#a020f0">#ifndef RUDIMENTS_DAEMONPROCESS_H</font>
<font color="#a020f0">#define RUDIMENTS_DAEMONPROCESS_H</font>

<font color="#a020f0">#include </font><font color="#ff00ff">&lt;rudiments/private/daemonprocessincludes.h&gt;</font>

<font color="#0000ff">// Daemons are long running processes which often detach themselves from</font>
<font color="#0000ff">// the controlling terminal and run in the background.  They are frequently</font>
<font color="#0000ff">// started at boot time and run until the machine is shut down.</font>
<font color="#0000ff">//</font>
<font color="#0000ff">// Daemons typically perform &quot;housecleaning&quot; tasks or serve data to client</font>
<font color="#0000ff">// programs.  See the server class.</font>

<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
<font color="#2e8b57"><b>namespace</b></font> rudiments {
<font color="#a020f0">#endif</font>

<font color="#2e8b57"><b>class</b></font> daemonprocessprivate;

<font color="#2e8b57"><b>class</b></font> daemonprocess {
        <font color="#a52a2a"><b>public</b></font>:
                        daemonprocess();
                <font color="#2e8b57"><b>virtual</b></font>       ~daemonprocess();

                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>int64_t</b></font>        checkForPidFile(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *filename);
                        <font color="#0000ff">// Checks for filename &quot;filename&quot; and reads the</font>
                        <font color="#0000ff">// process id out of it, if it exists.  Returns</font>
                        <font color="#0000ff">// the process id on success or -1 on failure.</font>
                <font color="#2e8b57"><b>static</b></font> <font color="#2e8b57"><b>bool</b></font>   createPidFile(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *filename,
                                                mode_t permissions);
                        <font color="#0000ff">// Create's file &quot;filename&quot; with permissions</font>
                        <font color="#0000ff">// &quot;permissions&quot; and puts the current process</font>
                        <font color="#0000ff">// id in it.  Note that when you delete this</font>
                        <font color="#0000ff">// file during shutdown you must use the full</font>
                        <font color="#0000ff">// pathname since the detach() method below</font>
                        <font color="#0000ff">// changes directories to &quot;/&quot;.  Returns true on</font>
                        <font color="#0000ff">// success and false on failure.</font>

                <font color="#2e8b57"><b>bool</b></font>  detach() <font color="#2e8b57"><b>const</b></font>;
                        <font color="#0000ff">// Detach from the controlling terminal and</font>
                        <font color="#0000ff">// process and run in the background.  Also</font>
                        <font color="#0000ff">// change directories to &quot;/&quot; and set the file</font>
                        <font color="#0000ff">// creation mask such that all files are </font>
                        <font color="#0000ff">// created -rw-rw-rw and all directories </font>
                        <font color="#0000ff">// drwxrwxrwx.</font>

                <font color="#0000ff">// These methods allow the daemon to run as a different</font>
                <font color="#0000ff">// user or group than the one that started the process.</font>
                <font color="#0000ff">// They have no effect unless the process is started</font>
                <font color="#0000ff">// by the root user.</font>
                <font color="#0000ff">//</font>
                <font color="#0000ff">// These methods return 1 on success, 0 on failure and</font>
                <font color="#0000ff">// -1 on error.</font>
                <font color="#2e8b57"><b>int</b></font>   runAsUser(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *username) <font color="#2e8b57"><b>const</b></font>;
                        <font color="#0000ff">// Note that runAsUser() uses the passwdentry</font>
                        <font color="#0000ff">// class.  If you are using this method in a</font>
                        <font color="#0000ff">// multithreaded application, you may need to</font>
                        <font color="#0000ff">// supply the passwdentry classes a mutex.</font>
                        <font color="#0000ff">// See passwdentry.h for more detail.</font>
                <font color="#2e8b57"><b>int</b></font>   runAsGroup(<font color="#2e8b57"><b>const</b></font> <font color="#2e8b57"><b>char</b></font> *groupname) <font color="#2e8b57"><b>const</b></font>;
                        <font color="#0000ff">// Note that runAsGroup() uses the groupentry</font>
                        <font color="#0000ff">// class.  If you are using this method in a</font>
                        <font color="#0000ff">// multithreaded application, you may need to</font>
                        <font color="#0000ff">// supply the groupentry classes a mutex.</font>
                        <font color="#0000ff">// See groupentry.h for more detail.</font>
                <font color="#2e8b57"><b>int</b></font>   runAsUserId(uid_t uid) <font color="#2e8b57"><b>const</b></font>;
                <font color="#2e8b57"><b>int</b></font>   runAsGroupId(gid_t gid) <font color="#2e8b57"><b>const</b></font>;

                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>void</b></font>   handleShutDown(<font color="#2e8b57"><b>void</b></font> *shutdownfunction);
                                <font color="#0000ff">// This method allows you to designate a</font>
                                <font color="#0000ff">// function to run when the daemon is killed.</font>

                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>void</b></font>   handleCrash(<font color="#2e8b57"><b>void</b></font> *crashfunction);
                                <font color="#0000ff">// This method allows you to designate a</font>
                                <font color="#0000ff">// function to run if the daemon crashes.</font>

                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>void</b></font>   waitForChildren();
                                <font color="#0000ff">// This method causes the daemon to wait</font>
                                <font color="#0000ff">// on child processes which have exited,</font>
                                <font color="#0000ff">// preventing so-called &quot;zombie&quot; processes</font>
                                <font color="#0000ff">// from occurring.  This method is called</font>
                                <font color="#0000ff">// in the constructor and is thus the default</font>
                                <font color="#0000ff">// behavior of this class.</font>
                <font color="#2e8b57"><b>static</b></font>        <font color="#2e8b57"><b>void</b></font>   dontWaitForChildren();
                                <font color="#0000ff">// This method causes the daemon not to</font>
                                <font color="#0000ff">// wait on child processes which have exited.</font>
                                <font color="#0000ff">// Ordinarily, you'd want to wait on child</font>
                                <font color="#0000ff">// processes, but this interferes with the</font>
                                <font color="#0000ff">// behavior of WEXITSTATUS() after a call to</font>
                                <font color="#0000ff">// system() (and possibly other calls).  This</font>
                                <font color="#0000ff">// method allows you to disable waiting on</font>
                                <font color="#0000ff">// child processes.</font>

<font color="#a020f0">        #include </font><font color="#ff00ff">&lt;rudiments/private/daemonprocess.h&gt;</font>

};

<font color="#a020f0">#ifdef RUDIMENTS_NAMESPACE</font>
}
<font color="#a020f0">#endif</font>

<font color="#a020f0">#endif</font>
</pre>
</body>
</html>