chiark / gitweb /
001eb0e7cc33aed9a4c2860bb012f52fc8f3ae19
[elogind.git] / src / core / execute.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 typedef struct ExecStatus ExecStatus;
25 typedef struct ExecCommand ExecCommand;
26 typedef struct ExecContext ExecContext;
27
28 #include <linux/types.h>
29 #include <sys/time.h>
30 #include <sys/resource.h>
31 #include <sys/capability.h>
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <sched.h>
35
36 struct CGroupBonding;
37 struct CGroupAttribute;
38
39 #include "list.h"
40 #include "util.h"
41
42 typedef enum ExecInput {
43         EXEC_INPUT_NULL,
44         EXEC_INPUT_TTY,
45         EXEC_INPUT_TTY_FORCE,
46         EXEC_INPUT_TTY_FAIL,
47         EXEC_INPUT_SOCKET,
48         _EXEC_INPUT_MAX,
49         _EXEC_INPUT_INVALID = -1
50 } ExecInput;
51
52 typedef enum ExecOutput {
53         EXEC_OUTPUT_INHERIT,
54         EXEC_OUTPUT_NULL,
55         EXEC_OUTPUT_TTY,
56         EXEC_OUTPUT_SYSLOG,
57         EXEC_OUTPUT_SYSLOG_AND_CONSOLE,
58         EXEC_OUTPUT_KMSG,
59         EXEC_OUTPUT_KMSG_AND_CONSOLE,
60         EXEC_OUTPUT_JOURNAL,
61         EXEC_OUTPUT_JOURNAL_AND_CONSOLE,
62         EXEC_OUTPUT_SOCKET,
63         _EXEC_OUTPUT_MAX,
64         _EXEC_OUTPUT_INVALID = -1
65 } ExecOutput;
66
67 struct ExecStatus {
68         dual_timestamp start_timestamp;
69         dual_timestamp exit_timestamp;
70         pid_t pid;
71         int code;     /* as in siginfo_t::si_code */
72         int status;   /* as in sigingo_t::si_status */
73 };
74
75 struct ExecCommand {
76         char *path;
77         char **argv;
78         ExecStatus exec_status;
79         LIST_FIELDS(ExecCommand, command); /* useful for chaining commands */
80         bool ignore;
81 };
82
83 struct ExecContext {
84         char **environment;
85         char **environment_files;
86
87         struct rlimit *rlimit[RLIMIT_NLIMITS];
88         char *working_directory, *root_directory;
89
90         mode_t umask;
91         int oom_score_adjust;
92         int nice;
93         int ioprio;
94         int cpu_sched_policy;
95         int cpu_sched_priority;
96
97         cpu_set_t *cpuset;
98         unsigned cpuset_ncpus;
99
100         ExecInput std_input;
101         ExecOutput std_output;
102         ExecOutput std_error;
103
104         nsec_t timer_slack_nsec;
105
106         char *tcpwrap_name;
107
108         char *tty_path;
109
110         bool tty_reset;
111         bool tty_vhangup;
112         bool tty_vt_disallocate;
113
114         bool ignore_sigpipe;
115
116         /* Since resolving these names might might involve socket
117          * connections and we don't want to deadlock ourselves these
118          * names are resolved on execution only and in the child
119          * process. */
120         char *user;
121         char *group;
122         char **supplementary_groups;
123
124         char *pam_name;
125
126         char *utmp_id;
127
128         char **read_write_dirs, **read_only_dirs, **inaccessible_dirs;
129         unsigned long mount_flags;
130
131         uint64_t capability_bounding_set_drop;
132
133         cap_t capabilities;
134         int secure_bits;
135
136         int syslog_priority;
137         char *syslog_identifier;
138         bool syslog_level_prefix;
139
140         bool cpu_sched_reset_on_fork;
141         bool non_blocking;
142         bool private_tmp;
143         bool private_network;
144
145         bool no_new_privileges;
146
147         bool control_group_modify;
148         int control_group_persistent;
149
150         /* This is not exposed to the user but available
151          * internally. We need it to make sure that whenever we spawn
152          * /bin/mount it is run in the same process group as us so
153          * that the autofs logic detects that it belongs to us and we
154          * don't enter a trigger loop. */
155         bool same_pgrp;
156
157         uint32_t *syscall_filter;
158
159         bool oom_score_adjust_set:1;
160         bool nice_set:1;
161         bool ioprio_set:1;
162         bool cpu_sched_set:1;
163 };
164
165 int exec_spawn(ExecCommand *command,
166                char **argv,
167                const ExecContext *context,
168                int fds[], unsigned n_fds,
169                char **environment,
170                bool apply_permissions,
171                bool apply_chroot,
172                bool apply_tty_stdin,
173                bool confirm_spawn,
174                struct CGroupBonding *cgroup_bondings,
175                struct CGroupAttribute *cgroup_attributes,
176                const char *cgroup_suffix,
177                const char *unit_id,
178                int pipe_fd[2],
179                pid_t *ret);
180
181 void exec_command_done(ExecCommand *c);
182 void exec_command_done_array(ExecCommand *c, unsigned n);
183
184 void exec_command_free_list(ExecCommand *c);
185 void exec_command_free_array(ExecCommand **c, unsigned n);
186
187 char *exec_command_line(char **argv);
188
189 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix);
190 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix);
191 void exec_command_append_list(ExecCommand **l, ExecCommand *e);
192 int exec_command_set(ExecCommand *c, const char *path, ...);
193
194 void exec_context_init(ExecContext *c);
195 void exec_context_done(ExecContext *c);
196 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix);
197 void exec_context_tty_reset(const ExecContext *context);
198
199 int exec_context_load_environment(const ExecContext *c, char ***l);
200
201 bool exec_context_may_touch_console(ExecContext *c);
202
203 void exec_status_start(ExecStatus *s, pid_t pid);
204 void exec_status_exit(ExecStatus *s, ExecContext *context, pid_t pid, int code, int status);
205 void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix);
206
207 const char* exec_output_to_string(ExecOutput i);
208 ExecOutput exec_output_from_string(const char *s);
209
210 const char* exec_input_to_string(ExecInput i);
211 ExecInput exec_input_from_string(const char *s);