daemon: Provide option to not chdir to root
[sliver-openvswitch.git] / lib / daemon.h
1 /*
2  * Copyright (c) 2008 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef DAEMON_H
18 #define DAEMON_H 1
19
20 #include <limits.h>
21 #include <stdbool.h>
22 #include <sys/types.h>
23
24 enum {
25     OPT_NO_CHDIR = UCHAR_MAX + 2048
26 };
27
28 #define DAEMON_LONG_OPTIONS                             \
29         {"detach",      no_argument, 0, 'D'},           \
30         {"no-chdir",    no_argument, 0, OPT_NO_CHDIR},  \
31         {"force",       no_argument, 0, 'f'},           \
32         {"pidfile",     optional_argument, 0, 'P'}
33
34 #define DAEMON_OPTION_HANDLERS                  \
35         case 'D':                               \
36             set_detach();                       \
37             break;                              \
38                                                 \
39         case OPT_NO_CHDIR:                      \
40             set_no_chdir();                     \
41             break;                              \
42                                                 \
43         case 'P':                               \
44             set_pidfile(optarg);                \
45             break;                              \
46                                                 \
47         case 'f':                               \
48             ignore_existing_pidfile();          \
49             break;
50
51 char *make_pidfile_name(const char *name);
52 void set_pidfile(const char *name);
53 const char *get_pidfile(void);
54 void set_no_chdir(void);
55 void set_detach(void);
56 void daemonize(void);
57 void die_if_already_running(void);
58 void ignore_existing_pidfile(void);
59 void daemon_usage(void);
60 pid_t read_pidfile(const char *name);
61
62 #endif /* daemon.h */