File: daemonprocess.h

package info (click to toggle)
librudiments0 0.27-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,528 kB
  • ctags: 2,284
  • sloc: cpp: 14,657; sh: 7,547; ansic: 2,664; makefile: 945; xml: 15
file content (94 lines) | stat: -rw-r--r-- 3,486 bytes parent folder | download
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
// Copyright (c) 1999-2002 David Muse
// See the COPYING file for more information.

#ifndef RUDIMENTS_DAEMONPROCESS_H
#define RUDIMENTS_DAEMONPROCESS_H

#include <rudiments/private/daemonprocessincludes.h>

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

class daemonprocess {
	public:
				daemonprocess();
			virtual	~daemonprocess();

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

			void	detach() const;
				// Detach from the controlling terminal and
				// process and run in the background.  Also
				// change directories to "/" and set the file
				// creation mask such that all files are 
				// created -rw-rw-rw and all directories 
				// drwxrwxrwx.

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

		static	void	handleShutDown(void *shutdownfunction);
				// This method allows you to designate a
				// function to run when the daemon is killed.

		static	void	handleCrash(void *crashfunction);
				// This method allows you to designate a
				// function to run if the daemon crashes.

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

	#include <rudiments/private/daemonprocess.h>

};

#endif