1/*
2 * Copyright (C) 2007 The Android Open Source Project
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 _INIT_INIT_H
18#define _INIT_INIT_H
19
20#include <cutils/list.h>
21
22#include <sys/stat.h>
23
24void handle_control_message(const char *msg, const char *arg);
25
26struct command
27{
28        /* list of commands in an action */
29    struct listnode clist;
30
31    int (*func)(int nargs, char **args);
32
33    int line;
34    const char *filename;
35
36    int nargs;
37    char *args[1];
38};
39
40struct action {
41        /* node in list of all actions */
42    struct listnode alist;
43        /* node in the queue of pending actions */
44    struct listnode qlist;
45        /* node in list of actions for a trigger */
46    struct listnode tlist;
47
48    unsigned hash;
49    const char *name;
50
51    struct listnode commands;
52    struct command *current;
53};
54
55struct socketinfo {
56    struct socketinfo *next;
57    const char *name;
58    const char *type;
59    uid_t uid;
60    gid_t gid;
61    int perm;
62    const char *socketcon;
63};
64
65struct svcenvinfo {
66    struct svcenvinfo *next;
67    const char *name;
68    const char *value;
69};
70
71#define SVC_DISABLED    0x01  /* do not autostart with class */
72#define SVC_ONESHOT     0x02  /* do not restart on exit */
73#define SVC_RUNNING     0x04  /* currently active */
74#define SVC_RESTARTING  0x08  /* waiting to restart */
75#define SVC_CONSOLE     0x10  /* requires console */
76#define SVC_CRITICAL    0x20  /* will reboot into recovery if keeps crashing */
77#define SVC_RESET       0x40  /* Use when stopping a process, but not disabling
78                                 so it can be restarted with its class */
79#define SVC_RC_DISABLED 0x80  /* Remember if the disabled flag was set in the rc script */
80#define SVC_RESTART     0x100 /* Use to safely restart (stop, wait, start) a service */
81#define SVC_DISABLED_START 0x200 /* a start was requested but it was disabled at the time */
82
83#define NR_SVC_SUPP_GIDS 12    /* twelve supplementary groups */
84
85#define COMMAND_RETRY_TIMEOUT 5
86
87struct service {
88        /* list of all services */
89    struct listnode slist;
90
91    const char *name;
92    const char *classname;
93
94    unsigned flags;
95    pid_t pid;
96    time_t time_started;    /* time of last start */
97    time_t time_crashed;    /* first crash within inspection window */
98    int nr_crashed;         /* number of times crashed within window */
99
100    uid_t uid;
101    gid_t gid;
102    gid_t supp_gids[NR_SVC_SUPP_GIDS];
103    size_t nr_supp_gids;
104
105    char *seclabel;
106
107    struct socketinfo *sockets;
108    struct svcenvinfo *envvars;
109
110    struct action onrestart;  /* Actions to execute on restart. */
111
112    /* keycodes for triggering this service via /dev/keychord */
113    int *keycodes;
114    int nkeycodes;
115    int keychord_id;
116
117    int ioprio_class;
118    int ioprio_pri;
119
120    int nargs;
121    /* "MUST BE AT THE END OF THE STRUCT" */
122    char *args[1];
123}; /*     ^-------'args' MUST be at the end of this struct! */
124
125void notify_service_state(const char *name, const char *state);
126
127struct service *service_find_by_name(const char *name);
128struct service *service_find_by_pid(pid_t pid);
129struct service *service_find_by_keychord(int keychord_id);
130void service_for_each(void (*func)(struct service *svc));
131void service_for_each_class(const char *classname,
132                            void (*func)(struct service *svc));
133void service_for_each_flags(unsigned matchflags,
134                            void (*func)(struct service *svc));
135void service_stop(struct service *svc);
136void service_reset(struct service *svc);
137void service_restart(struct service *svc);
138void service_start(struct service *svc, const char *dynamic_args);
139void property_changed(const char *name, const char *value);
140
141extern struct selabel_handle *sehandle;
142extern struct selabel_handle *sehandle_prop;
143extern int selinux_reload_policy(void);
144
145#endif	/* _INIT_INIT_H */
146