2010-02-03 13:03:47 +01:00
/***
This file is part of systemd .
Copyright 2010 Lennart Poettering
systemd is free software ; you can redistribute it and / or modify it
2012-04-12 00:20:58 +02:00
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
2010-02-03 13:03:47 +01:00
( at your option ) any later version .
systemd is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
2012-04-12 00:20:58 +02:00
Lesser General Public License for more details .
2010-02-03 13:03:47 +01:00
2012-04-12 00:20:58 +02:00
You should have received a copy of the GNU Lesser General Public License
2010-02-03 13:03:47 +01:00
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
2010-01-26 04:18:44 +01:00
# include <errno.h>
# include <fcntl.h>
2015-10-04 17:36:19 +02:00
# include <glob.h>
# include <grp.h>
# include <poll.h>
2010-01-27 06:17:51 +01:00
# include <signal.h>
2015-10-04 17:36:19 +02:00
# include <string.h>
2016-01-30 17:26:39 +01:00
# include <sys/capability.h>
2016-08-03 18:44:51 +02:00
# include <sys/eventfd.h>
2016-06-03 17:58:18 +02:00
# include <sys/mman.h>
2015-10-04 17:36:19 +02:00
# include <sys/personality.h>
2010-01-30 01:55:42 +01:00
# include <sys/prctl.h>
2016-10-26 17:52:53 +02:00
# include <sys/shm.h>
2015-10-04 17:36:19 +02:00
# include <sys/socket.h>
2010-02-12 02:00:18 +01:00
# include <sys/stat.h>
2016-10-26 17:52:53 +02:00
# include <sys/types.h>
2015-10-04 17:36:19 +02:00
# include <sys/un.h>
# include <unistd.h>
2015-08-23 13:14:04 +02:00
# include <utmpx.h>
2010-01-23 01:52:57 +01:00
2017-10-03 10:41:51 +02:00
# if HAVE_PAM
2010-06-16 21:54:17 +02:00
# include <security/pam_appl.h>
# endif
2017-10-03 10:41:51 +02:00
# if HAVE_SELINUX
2014-02-06 10:05:16 +01:00
# include <selinux/selinux.h>
# endif
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
2014-02-12 18:28:21 +01:00
# include <seccomp.h>
# endif
2017-10-03 10:41:51 +02:00
# if HAVE_APPARMOR
2014-02-20 16:19:44 +01:00
# include <sys/apparmor.h>
# endif
2015-05-29 20:14:11 +02:00
# include "sd-messages.h"
2015-10-04 17:36:19 +02:00
# include "af-list.h"
2015-10-27 03:01:06 +01:00
# include "alloc-util.h"
2017-10-03 10:41:51 +02:00
# if HAVE_APPARMOR
2015-10-25 13:14:12 +01:00
# include "apparmor-util.h"
# endif
2015-10-04 17:36:19 +02:00
# include "async.h"
# include "barrier.h"
# include "cap-list.h"
2015-10-26 23:32:16 +01:00
# include "capability-util.h"
2017-09-28 19:13:44 +02:00
# include "chown-recursive.h"
2011-03-17 04:02:35 +01:00
# include "def.h"
2013-02-11 03:46:08 +01:00
# include "env-util.h"
2014-02-12 18:28:21 +01:00
# include "errno-list.h"
2015-10-25 13:14:12 +01:00
# include "execute.h"
2015-10-04 17:36:19 +02:00
# include "exit-status.h"
2015-10-25 13:14:12 +01:00
# include "fd-util.h"
2015-10-04 17:36:19 +02:00
# include "fileio.h"
2016-11-07 16:14:59 +01:00
# include "format-util.h"
2015-10-26 21:16:26 +01:00
# include "fs-util.h"
2015-10-27 01:48:17 +01:00
# include "glob-util.h"
2015-10-25 14:08:25 +01:00
# include "io-util.h"
2015-10-04 17:36:19 +02:00
# include "ioprio.h"
2017-09-28 19:13:44 +02:00
# include "label.h"
2015-10-04 17:36:19 +02:00
# include "log.h"
# include "macro.h"
# include "missing.h"
# include "mkdir.h"
# include "namespace.h"
2015-10-26 16:18:16 +01:00
# include "parse-util.h"
2015-10-04 17:36:19 +02:00
# include "path-util.h"
2015-04-10 19:10:00 +02:00
# include "process-util.h"
2015-10-26 19:40:43 +01:00
# include "rlimit-util.h"
2015-10-04 17:36:19 +02:00
# include "rm-rf.h"
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
2015-10-25 13:14:12 +01:00
# include "seccomp-util.h"
# endif
2015-10-04 17:36:19 +02:00
# include "securebits.h"
2017-08-07 16:40:25 +02:00
# include "securebits-util.h"
2015-10-04 17:36:19 +02:00
# include "selinux-util.h"
2015-05-29 20:14:11 +02:00
# include "signal-util.h"
2015-10-04 17:36:19 +02:00
# include "smack-util.h"
2016-08-02 12:28:51 +02:00
# include "special.h"
2015-10-26 22:31:05 +01:00
# include "string-table.h"
2015-10-24 22:58:24 +02:00
# include "string-util.h"
2015-10-04 17:36:19 +02:00
# include "strv.h"
2015-10-27 00:40:25 +01:00
# include "syslog-util.h"
2015-10-04 17:36:19 +02:00
# include "terminal-util.h"
# include "unit.h"
2015-10-25 22:32:30 +01:00
# include "user-util.h"
2015-10-04 17:36:19 +02:00
# include "util.h"
# include "utmp-wtmp.h"
2010-01-23 01:52:57 +01:00
2012-05-24 02:22:35 +02:00
# define IDLE_TIMEOUT_USEC (5*USEC_PER_SEC)
systemd: do not output status messages once gettys are running
Make Type=idle communication bidirectional: when bootup is finished,
the manager, as before, signals idling Type=idle jobs to continue.
However, if the boot takes too long, idling jobs signal the manager
that they have had enough, wait a tiny bit more, and continue, taking
ownership of the console. The manager, when signalled that Type=idle
jobs are done, makes a note and will not write to the console anymore.
This is a cosmetic issue, but quite noticable, so let's just fix it.
Based on Harald Hoyer's patch.
https://bugs.freedesktop.org/show_bug.cgi?id=54247
http://unix.stackexchange.com/questions/51805/systemd-messages-after-starting-login/
2013-07-16 03:34:57 +02:00
# define IDLE_TIMEOUT2_USEC (1*USEC_PER_SEC)
2012-05-22 19:26:13 +02:00
2010-04-13 18:50:43 +02:00
/* This assumes there is a 'tty' group */
# define TTY_MODE 0620
2013-12-16 20:00:09 +01:00
# define SNDBUF_SIZE (8*1024*1024)
2010-01-26 04:18:44 +01:00
static int shift_fds ( int fds [ ] , unsigned n_fds ) {
int start , restart_from ;
if ( n_fds < = 0 )
return 0 ;
2010-04-06 23:35:59 +02:00
/* Modifies the fds array! (sorts it) */
2010-01-26 04:18:44 +01:00
assert ( fds ) ;
start = 0 ;
for ( ; ; ) {
int i ;
restart_from = - 1 ;
for ( i = start ; i < ( int ) n_fds ; i + + ) {
int nfd ;
/* Already at right index? */
if ( fds [ i ] = = i + 3 )
continue ;
2015-09-08 19:14:10 +02:00
nfd = fcntl ( fds [ i ] , F_DUPFD , i + 3 ) ;
if ( nfd < 0 )
2010-01-26 04:18:44 +01:00
return - errno ;
2014-03-18 19:22:43 +01:00
safe_close ( fds [ i ] ) ;
2010-01-26 04:18:44 +01:00
fds [ i ] = nfd ;
/* Hmm, the fd we wanted isn't free? Then
2014-12-10 20:00:08 +01:00
* let ' s remember that and try again from here */
2010-01-26 04:18:44 +01:00
if ( nfd ! = i + 3 & & restart_from < 0 )
restart_from = i ;
}
if ( restart_from < 0 )
break ;
start = restart_from ;
}
return 0 ;
}
2017-06-08 15:41:26 +02:00
static int flags_fds ( const int fds [ ] , unsigned n_storage_fds , unsigned n_socket_fds , bool nonblock ) {
unsigned i , n_fds ;
2010-04-06 21:53:39 +02:00
int r ;
2010-01-27 06:18:45 +01:00
2017-06-08 15:41:26 +02:00
n_fds = n_storage_fds + n_socket_fds ;
2010-01-27 06:18:45 +01:00
if ( n_fds < = 0 )
return 0 ;
assert ( fds ) ;
2017-05-12 11:32:53 +02:00
/* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags.
* O_NONBLOCK only applies to socket activation though . */
2010-01-27 06:18:45 +01:00
for ( i = 0 ; i < n_fds ; i + + ) {
2017-05-12 11:32:53 +02:00
if ( i < n_socket_fds ) {
r = fd_nonblock ( fds [ i ] , nonblock ) ;
if ( r < 0 )
return r ;
}
2010-01-27 06:18:45 +01:00
2010-02-12 02:00:18 +01:00
/* We unconditionally drop FD_CLOEXEC from the fds,
* since after all we want to pass these fds to our
* children */
2010-01-27 06:18:45 +01:00
2015-09-08 19:14:10 +02:00
r = fd_cloexec ( fds [ i ] , false ) ;
if ( r < 0 )
2010-04-06 21:53:39 +02:00
return r ;
2010-01-27 06:18:45 +01:00
}
return 0 ;
}
2016-01-28 16:25:39 +01:00
static const char * exec_context_tty_path ( const ExecContext * context ) {
2010-04-13 02:06:27 +02:00
assert ( context ) ;
2016-01-28 16:25:39 +01:00
if ( context - > stdio_as_fds )
return NULL ;
2010-04-13 02:06:27 +02:00
if ( context - > tty_path )
return context - > tty_path ;
return " /dev/console " ;
}
2016-01-28 16:25:39 +01:00
static void exec_context_tty_reset ( const ExecContext * context , const ExecParameters * p ) {
const char * path ;
2011-05-18 01:07:31 +02:00
assert ( context ) ;
2016-01-28 16:25:39 +01:00
path = exec_context_tty_path ( context ) ;
2011-05-18 01:07:31 +02:00
2016-01-28 16:25:39 +01:00
if ( context - > tty_vhangup ) {
if ( p & & p - > stdin_fd > = 0 )
( void ) terminal_vhangup_fd ( p - > stdin_fd ) ;
else if ( path )
( void ) terminal_vhangup ( path ) ;
}
2011-05-18 01:07:31 +02:00
2016-01-28 16:25:39 +01:00
if ( context - > tty_reset ) {
if ( p & & p - > stdin_fd > = 0 )
( void ) reset_terminal_fd ( p - > stdin_fd , true ) ;
else if ( path )
( void ) reset_terminal ( path ) ;
}
if ( context - > tty_vt_disallocate & & path )
( void ) vt_disallocate ( path ) ;
2011-05-18 01:07:31 +02:00
}
2016-07-27 15:25:55 +02:00
static bool is_terminal_input ( ExecInput i ) {
return IN_SET ( i ,
EXEC_INPUT_TTY ,
EXEC_INPUT_TTY_FORCE ,
EXEC_INPUT_TTY_FAIL ) ;
}
2013-02-28 01:35:47 +01:00
static bool is_terminal_output ( ExecOutput o ) {
2016-07-27 15:25:55 +02:00
return IN_SET ( o ,
EXEC_OUTPUT_TTY ,
EXEC_OUTPUT_SYSLOG_AND_CONSOLE ,
EXEC_OUTPUT_KMSG_AND_CONSOLE ,
EXEC_OUTPUT_JOURNAL_AND_CONSOLE ) ;
}
2017-09-01 02:04:27 +02:00
static bool is_syslog_output ( ExecOutput o ) {
return IN_SET ( o ,
EXEC_OUTPUT_SYSLOG ,
EXEC_OUTPUT_SYSLOG_AND_CONSOLE ) ;
}
static bool is_kmsg_output ( ExecOutput o ) {
return IN_SET ( o ,
EXEC_OUTPUT_KMSG ,
EXEC_OUTPUT_KMSG_AND_CONSOLE ) ;
}
2016-07-27 15:25:55 +02:00
static bool exec_context_needs_term ( const ExecContext * c ) {
assert ( c ) ;
/* Return true if the execution context suggests we should set $TERM to something useful. */
if ( is_terminal_input ( c - > std_input ) )
return true ;
if ( is_terminal_output ( c - > std_output ) )
return true ;
if ( is_terminal_output ( c - > std_error ) )
return true ;
return ! ! c - > tty_path ;
2013-02-28 01:35:47 +01:00
}
2010-04-13 02:06:27 +02:00
static int open_null_as ( int flags , int nfd ) {
int fd , r ;
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
assert ( nfd > = 0 ) ;
2010-01-28 02:06:20 +01:00
2013-11-27 20:23:18 +01:00
fd = open ( " /dev/null " , flags | O_NOCTTY ) ;
if ( fd < 0 )
2010-01-28 02:06:20 +01:00
return - errno ;
2010-04-13 02:06:27 +02:00
if ( fd ! = nfd ) {
r = dup2 ( fd , nfd ) < 0 ? - errno : nfd ;
2014-03-18 19:22:43 +01:00
safe_close ( fd ) ;
2010-04-13 02:06:27 +02:00
} else
r = nfd ;
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
return r ;
2010-01-28 02:06:20 +01:00
}
2015-01-01 04:40:41 +01:00
static int connect_journal_socket ( int fd , uid_t uid , gid_t gid ) {
2017-07-14 18:58:57 +02:00
static const union sockaddr_union sa = {
2013-03-25 00:59:00 +01:00
. un . sun_family = AF_UNIX ,
. un . sun_path = " /run/systemd/journal/stdout " ,
} ;
2015-01-01 04:40:41 +01:00
uid_t olduid = UID_INVALID ;
gid_t oldgid = GID_INVALID ;
int r ;
2017-07-14 18:57:04 +02:00
if ( gid_is_valid ( gid ) ) {
2015-01-01 04:40:41 +01:00
oldgid = getgid ( ) ;
2017-07-14 18:58:57 +02:00
if ( setegid ( gid ) < 0 )
2015-01-01 04:40:41 +01:00
return - errno ;
}
2017-07-14 18:57:04 +02:00
if ( uid_is_valid ( uid ) ) {
2015-01-01 04:40:41 +01:00
olduid = getuid ( ) ;
2017-07-14 18:58:57 +02:00
if ( seteuid ( uid ) < 0 ) {
2015-01-01 04:40:41 +01:00
r = - errno ;
goto restore_gid ;
}
}
2017-07-14 18:58:57 +02:00
r = connect ( fd , & sa . sa , SOCKADDR_UN_LEN ( sa . un ) ) < 0 ? - errno : 0 ;
2015-01-01 04:40:41 +01:00
/* If we fail to restore the uid or gid, things will likely
fail later on . This should only happen if an LSM interferes . */
2017-07-14 18:57:04 +02:00
if ( uid_is_valid ( uid ) )
2015-01-01 04:40:41 +01:00
( void ) seteuid ( olduid ) ;
restore_gid :
2017-07-14 18:57:04 +02:00
if ( gid_is_valid ( gid ) )
2015-01-01 04:40:41 +01:00
( void ) setegid ( oldgid ) ;
return r ;
}
2016-06-14 16:50:35 +02:00
static int connect_logger_as (
2016-07-02 04:57:21 +02:00
Unit * unit ,
2016-06-14 16:50:35 +02:00
const ExecContext * context ,
2017-08-01 10:28:20 +02:00
const ExecParameters * params ,
2016-06-14 16:50:35 +02:00
ExecOutput output ,
const char * ident ,
int nfd ,
uid_t uid ,
gid_t gid ) {
2015-01-01 04:40:41 +01:00
int fd , r ;
2010-01-28 02:06:20 +01:00
assert ( context ) ;
2017-08-01 10:28:20 +02:00
assert ( params ) ;
2010-04-13 02:06:27 +02:00
assert ( output < _EXEC_OUTPUT_MAX ) ;
assert ( ident ) ;
assert ( nfd > = 0 ) ;
2010-01-28 02:06:20 +01:00
2012-01-05 21:39:08 +01:00
fd = socket ( AF_UNIX , SOCK_STREAM , 0 ) ;
if ( fd < 0 )
2010-04-13 02:06:27 +02:00
return - errno ;
2010-01-28 02:06:20 +01:00
2015-01-01 04:40:41 +01:00
r = connect_journal_socket ( fd , uid , gid ) ;
if ( r < 0 )
return r ;
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
if ( shutdown ( fd , SHUT_RD ) < 0 ) {
2014-03-18 19:22:43 +01:00
safe_close ( fd ) ;
2010-04-13 02:06:27 +02:00
return - errno ;
}
2010-01-28 02:06:20 +01:00
2016-06-14 16:50:35 +02:00
( void ) fd_inc_sndbuf ( fd , SNDBUF_SIZE ) ;
2013-12-16 20:00:09 +01:00
2010-04-13 02:06:27 +02:00
dprintf ( fd ,
2012-06-21 22:40:47 +02:00
" %s \n "
2010-04-13 02:06:27 +02:00
" %s \n "
" %i \n "
2012-01-05 21:39:08 +01:00
" %i \n "
" %i \n "
" %i \n "
2010-05-16 01:46:35 +02:00
" %i \n " ,
2017-07-14 18:59:41 +02:00
context - > syslog_identifier ? : ident ,
2017-08-01 10:28:20 +02:00
params - > flags & EXEC_PASS_LOG_UNIT ? unit - > id : " " ,
2012-01-05 21:39:08 +01:00
context - > syslog_priority ,
! ! context - > syslog_level_prefix ,
2017-09-01 02:04:27 +02:00
is_syslog_output ( output ) ,
is_kmsg_output ( output ) ,
2013-02-28 01:35:47 +01:00
is_terminal_output ( output ) ) ;
2010-04-13 02:06:27 +02:00
2016-06-14 16:50:35 +02:00
if ( fd = = nfd )
return nfd ;
r = dup2 ( fd , nfd ) < 0 ? - errno : nfd ;
safe_close ( fd ) ;
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
return r ;
}
static int open_terminal_as ( const char * path , mode_t mode , int nfd ) {
int fd , r ;
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
assert ( path ) ;
assert ( nfd > = 0 ) ;
2010-01-28 02:06:20 +01:00
2015-09-08 19:14:10 +02:00
fd = open_terminal ( path , mode | O_NOCTTY ) ;
if ( fd < 0 )
2010-04-13 02:06:27 +02:00
return fd ;
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
if ( fd ! = nfd ) {
r = dup2 ( fd , nfd ) < 0 ? - errno : nfd ;
2014-03-18 19:22:43 +01:00
safe_close ( fd ) ;
2010-04-13 02:06:27 +02:00
} else
r = nfd ;
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
return r ;
}
2010-01-28 02:06:20 +01:00
2010-07-08 04:09:17 +02:00
static int fixup_input ( ExecInput std_input , int socket_fd , bool apply_tty_stdin ) {
if ( is_terminal_input ( std_input ) & & ! apply_tty_stdin )
return EXEC_INPUT_NULL ;
2010-01-28 02:06:20 +01:00
2010-05-19 21:50:34 +02:00
if ( std_input = = EXEC_INPUT_SOCKET & & socket_fd < 0 )
2010-04-15 06:19:54 +02:00
return EXEC_INPUT_NULL ;
2010-05-19 21:50:34 +02:00
return std_input ;
2010-04-15 06:19:54 +02:00
}
2010-05-19 21:50:34 +02:00
static int fixup_output ( ExecOutput std_output , int socket_fd ) {
2010-04-15 06:19:54 +02:00
2010-05-19 21:50:34 +02:00
if ( std_output = = EXEC_OUTPUT_SOCKET & & socket_fd < 0 )
2010-04-15 06:19:54 +02:00
return EXEC_OUTPUT_INHERIT ;
2010-05-19 21:50:34 +02:00
return std_output ;
2010-04-15 06:19:54 +02:00
}
2015-10-07 23:07:39 +02:00
static int setup_input (
const ExecContext * context ,
const ExecParameters * params ,
2016-10-18 02:05:49 +02:00
int socket_fd ,
int named_iofds [ 3 ] ) {
2015-10-07 23:07:39 +02:00
2010-04-15 06:19:54 +02:00
ExecInput i ;
assert ( context ) ;
2015-10-07 23:07:39 +02:00
assert ( params ) ;
if ( params - > stdin_fd > = 0 ) {
if ( dup2 ( params - > stdin_fd , STDIN_FILENO ) < 0 )
return - errno ;
/* Try to make this the controlling tty, if it is a tty, and reset it */
( void ) ioctl ( STDIN_FILENO , TIOCSCTTY , context - > std_input = = EXEC_INPUT_TTY_FORCE ) ;
( void ) reset_terminal_fd ( STDIN_FILENO , true ) ;
return STDIN_FILENO ;
}
2010-04-15 06:19:54 +02:00
2016-07-26 17:40:35 +02:00
i = fixup_input ( context - > std_input , socket_fd , params - > flags & EXEC_APPLY_TTY_STDIN ) ;
2010-04-15 06:19:54 +02:00
switch ( i ) {
2010-01-28 02:06:20 +01:00
2010-04-13 02:06:27 +02:00
case EXEC_INPUT_NULL :
return open_null_as ( O_RDONLY , STDIN_FILENO ) ;
case EXEC_INPUT_TTY :
case EXEC_INPUT_TTY_FORCE :
case EXEC_INPUT_TTY_FAIL : {
int fd , r ;
2010-01-28 02:06:20 +01:00
2016-01-28 16:25:39 +01:00
fd = acquire_terminal ( exec_context_tty_path ( context ) ,
2013-08-28 14:01:30 +02:00
i = = EXEC_INPUT_TTY_FAIL ,
i = = EXEC_INPUT_TTY_FORCE ,
false ,
2014-07-29 12:23:31 +02:00
USEC_INFINITY ) ;
2013-08-28 14:01:30 +02:00
if ( fd < 0 )
2010-04-13 02:06:27 +02:00
return fd ;
if ( fd ! = STDIN_FILENO ) {
r = dup2 ( fd , STDIN_FILENO ) < 0 ? - errno : STDIN_FILENO ;
2014-03-18 19:22:43 +01:00
safe_close ( fd ) ;
2010-04-13 02:06:27 +02:00
} else
r = STDIN_FILENO ;
return r ;
}
2010-04-15 06:19:54 +02:00
case EXEC_INPUT_SOCKET :
return dup2 ( socket_fd , STDIN_FILENO ) < 0 ? - errno : STDIN_FILENO ;
2016-10-18 02:05:49 +02:00
case EXEC_INPUT_NAMED_FD :
( void ) fd_nonblock ( named_iofds [ STDIN_FILENO ] , false ) ;
return dup2 ( named_iofds [ STDIN_FILENO ] , STDIN_FILENO ) < 0 ? - errno : STDIN_FILENO ;
2010-04-13 02:06:27 +02:00
default :
assert_not_reached ( " Unknown input type " ) ;
}
}
2015-10-07 23:07:39 +02:00
static int setup_output (
Unit * unit ,
const ExecContext * context ,
const ExecParameters * params ,
int fileno ,
int socket_fd ,
2016-10-18 02:05:49 +02:00
int named_iofds [ 3 ] ,
2015-10-07 23:07:39 +02:00
const char * ident ,
2016-06-14 16:50:45 +02:00
uid_t uid ,
gid_t gid ,
dev_t * journal_stream_dev ,
ino_t * journal_stream_ino ) {
2015-10-07 23:07:39 +02:00
2010-04-15 06:19:54 +02:00
ExecOutput o ;
ExecInput i ;
2013-02-15 22:43:23 +01:00
int r ;
2010-04-15 06:19:54 +02:00
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
assert ( unit ) ;
2010-04-13 02:06:27 +02:00
assert ( context ) ;
2015-10-07 23:07:39 +02:00
assert ( params ) ;
2010-04-13 02:06:27 +02:00
assert ( ident ) ;
2016-06-14 16:50:45 +02:00
assert ( journal_stream_dev ) ;
assert ( journal_stream_ino ) ;
2010-04-13 02:06:27 +02:00
2015-10-07 23:07:39 +02:00
if ( fileno = = STDOUT_FILENO & & params - > stdout_fd > = 0 ) {
if ( dup2 ( params - > stdout_fd , STDOUT_FILENO ) < 0 )
return - errno ;
return STDOUT_FILENO ;
}
if ( fileno = = STDERR_FILENO & & params - > stderr_fd > = 0 ) {
if ( dup2 ( params - > stderr_fd , STDERR_FILENO ) < 0 )
return - errno ;
return STDERR_FILENO ;
}
2016-07-26 17:40:35 +02:00
i = fixup_input ( context - > std_input , socket_fd , params - > flags & EXEC_APPLY_TTY_STDIN ) ;
2010-05-19 21:50:34 +02:00
o = fixup_output ( context - > std_output , socket_fd ) ;
2010-04-15 06:19:54 +02:00
2013-02-15 23:36:23 +01:00
if ( fileno = = STDERR_FILENO ) {
ExecOutput e ;
e = fixup_output ( context - > std_error , socket_fd ) ;
2010-04-13 02:06:27 +02:00
2013-02-15 23:36:23 +01:00
/* This expects the input and output are already set up */
/* Don't change the stderr file descriptor if we inherit all
* the way and are not on a tty */
if ( e = = EXEC_OUTPUT_INHERIT & &
o = = EXEC_OUTPUT_INHERIT & &
i = = EXEC_INPUT_NULL & &
! is_terminal_input ( context - > std_input ) & &
getppid ( ) ! = 1 )
return fileno ;
/* Duplicate from stdout if possible */
2016-10-18 02:05:49 +02:00
if ( ( e = = o & & e ! = EXEC_OUTPUT_NAMED_FD ) | | e = = EXEC_OUTPUT_INHERIT )
2013-02-15 23:36:23 +01:00
return dup2 ( STDOUT_FILENO , fileno ) < 0 ? - errno : fileno ;
2010-01-28 02:06:20 +01:00
2013-02-15 23:36:23 +01:00
o = e ;
2010-04-13 02:06:27 +02:00
2013-02-15 23:36:23 +01:00
} else if ( o = = EXEC_OUTPUT_INHERIT ) {
2010-07-12 22:04:59 +02:00
/* If input got downgraded, inherit the original value */
if ( i = = EXEC_INPUT_NULL & & is_terminal_input ( context - > std_input ) )
2016-01-28 16:25:39 +01:00
return open_terminal_as ( exec_context_tty_path ( context ) , O_WRONLY , fileno ) ;
2010-07-12 22:04:59 +02:00
2010-07-07 04:37:42 +02:00
/* If the input is connected to anything that's not a /dev/null, inherit that... */
2010-05-20 01:08:13 +02:00
if ( i ! = EXEC_INPUT_NULL )
2013-02-15 23:36:23 +01:00
return dup2 ( STDIN_FILENO , fileno ) < 0 ? - errno : fileno ;
2010-01-28 02:06:20 +01:00
2010-07-07 04:37:42 +02:00
/* If we are not started from PID 1 we just inherit STDOUT from our parent process. */
if ( getppid ( ) ! = 1 )
2013-02-15 23:36:23 +01:00
return fileno ;
2010-01-30 01:55:42 +01:00
2013-02-15 23:36:23 +01:00
/* We need to open /dev/null here anew, to get the right access mode. */
return open_null_as ( O_WRONLY , fileno ) ;
2010-01-28 02:06:20 +01:00
}
2010-01-30 01:55:42 +01:00
2013-02-15 23:36:23 +01:00
switch ( o ) {
2010-04-13 02:06:27 +02:00
case EXEC_OUTPUT_NULL :
2013-02-15 23:36:23 +01:00
return open_null_as ( O_WRONLY , fileno ) ;
2010-04-13 02:06:27 +02:00
case EXEC_OUTPUT_TTY :
2010-04-15 06:19:54 +02:00
if ( is_terminal_input ( i ) )
2013-02-15 23:36:23 +01:00
return dup2 ( STDIN_FILENO , fileno ) < 0 ? - errno : fileno ;
2010-04-13 02:06:27 +02:00
/* We don't reset the terminal if this is just about output */
2016-01-28 16:25:39 +01:00
return open_terminal_as ( exec_context_tty_path ( context ) , O_WRONLY , fileno ) ;
2010-04-13 02:06:27 +02:00
case EXEC_OUTPUT_SYSLOG :
2011-02-15 01:27:53 +01:00
case EXEC_OUTPUT_SYSLOG_AND_CONSOLE :
2010-05-19 21:49:03 +02:00
case EXEC_OUTPUT_KMSG :
2011-02-15 01:27:53 +01:00
case EXEC_OUTPUT_KMSG_AND_CONSOLE :
2012-01-05 23:54:45 +01:00
case EXEC_OUTPUT_JOURNAL :
case EXEC_OUTPUT_JOURNAL_AND_CONSOLE :
2017-08-01 10:28:20 +02:00
r = connect_logger_as ( unit , context , params , o , ident , fileno , uid , gid ) ;
2013-02-15 22:43:23 +01:00
if ( r < 0 ) {
2017-09-26 17:42:17 +02:00
log_unit_warning_errno ( unit , r , " Failed to connect %s to the journal socket, ignoring: %m " , fileno = = STDOUT_FILENO ? " stdout " : " stderr " ) ;
2013-02-15 23:36:23 +01:00
r = open_null_as ( O_WRONLY , fileno ) ;
2016-06-14 16:50:45 +02:00
} else {
struct stat st ;
/* If we connected this fd to the journal via a stream, patch the device/inode into the passed
* parameters , but only then . This is useful so that we can set $ JOURNAL_STREAM that permits
2017-09-15 08:26:38 +02:00
* services to detect whether they are connected to the journal or not .
*
* If both stdout and stderr are connected to a stream then let ' s make sure to store the data
* about STDERR as that ' s usually the best way to do logging . */
2016-06-14 16:50:45 +02:00
2017-09-15 08:26:38 +02:00
if ( fstat ( fileno , & st ) > = 0 & &
( * journal_stream_ino = = 0 | | fileno = = STDERR_FILENO ) ) {
2016-06-14 16:50:45 +02:00
* journal_stream_dev = st . st_dev ;
* journal_stream_ino = st . st_ino ;
}
2013-02-15 22:43:23 +01:00
}
return r ;
2010-04-15 06:19:54 +02:00
case EXEC_OUTPUT_SOCKET :
assert ( socket_fd > = 0 ) ;
2013-02-15 23:36:23 +01:00
return dup2 ( socket_fd , fileno ) < 0 ? - errno : fileno ;
2010-01-30 01:55:42 +01:00
2016-10-18 02:05:49 +02:00
case EXEC_OUTPUT_NAMED_FD :
( void ) fd_nonblock ( named_iofds [ fileno ] , false ) ;
return dup2 ( named_iofds [ fileno ] , fileno ) < 0 ? - errno : fileno ;
2010-01-30 01:55:42 +01:00
default :
2010-04-13 02:06:27 +02:00
assert_not_reached ( " Unknown error type " ) ;
2010-01-30 01:55:42 +01:00
}
2010-01-28 02:06:20 +01:00
}
2010-04-13 18:50:43 +02:00
static int chown_terminal ( int fd , uid_t uid ) {
struct stat st ;
assert ( fd > = 0 ) ;
2016-06-09 10:01:16 +02:00
/* Before we chown/chmod the TTY, let's ensure this is actually a tty */
if ( isatty ( fd ) < 1 )
return 0 ;
2010-04-13 18:50:43 +02:00
/* This might fail. What matters are the results. */
2010-05-10 03:34:31 +02:00
( void ) fchown ( fd , uid , - 1 ) ;
( void ) fchmod ( fd , TTY_MODE ) ;
2010-04-13 18:50:43 +02:00
if ( fstat ( fd , & st ) < 0 )
return - errno ;
2010-04-13 21:13:49 +02:00
if ( st . st_uid ! = uid | | ( st . st_mode & 0777 ) ! = TTY_MODE )
2010-04-13 18:50:43 +02:00
return - EPERM ;
return 0 ;
}
2016-11-02 10:38:22 +01:00
static int setup_confirm_stdio ( const char * vc , int * _saved_stdin , int * _saved_stdout ) {
2015-10-08 14:33:53 +02:00
_cleanup_close_ int fd = - 1 , saved_stdin = - 1 , saved_stdout = - 1 ;
int r ;
2010-04-13 02:06:27 +02:00
assert ( _saved_stdin ) ;
assert ( _saved_stdout ) ;
2012-06-26 12:16:18 +02:00
saved_stdin = fcntl ( STDIN_FILENO , F_DUPFD , 3 ) ;
if ( saved_stdin < 0 )
return - errno ;
2010-04-13 02:06:27 +02:00
2012-06-26 12:16:18 +02:00
saved_stdout = fcntl ( STDOUT_FILENO , F_DUPFD , 3 ) ;
2015-10-08 14:33:53 +02:00
if ( saved_stdout < 0 )
return - errno ;
2010-04-13 02:06:27 +02:00
2016-11-02 10:38:22 +01:00
fd = acquire_terminal ( vc , false , false , false , DEFAULT_CONFIRM_USEC ) ;
2015-10-08 14:33:53 +02:00
if ( fd < 0 )
return fd ;
2010-04-13 02:06:27 +02:00
2012-06-26 12:16:18 +02:00
r = chown_terminal ( fd , getuid ( ) ) ;
if ( r < 0 )
2015-10-08 14:33:53 +02:00
return r ;
2010-04-13 18:50:43 +02:00
2015-10-08 14:33:53 +02:00
r = reset_terminal_fd ( fd , true ) ;
if ( r < 0 )
return r ;
2010-04-13 02:06:27 +02:00
2015-10-08 14:33:53 +02:00
if ( dup2 ( fd , STDIN_FILENO ) < 0 )
return - errno ;
if ( dup2 ( fd , STDOUT_FILENO ) < 0 )
return - errno ;
2010-04-13 02:06:27 +02:00
if ( fd > = 2 )
2014-03-18 19:22:43 +01:00
safe_close ( fd ) ;
2015-10-08 14:33:53 +02:00
fd = - 1 ;
2010-04-13 02:06:27 +02:00
* _saved_stdin = saved_stdin ;
* _saved_stdout = saved_stdout ;
2015-10-08 14:33:53 +02:00
saved_stdin = saved_stdout = - 1 ;
2010-04-13 02:06:27 +02:00
2015-10-08 14:33:53 +02:00
return 0 ;
2010-04-13 02:06:27 +02:00
}
2016-11-10 10:07:42 +01:00
static void write_confirm_error_fd ( int err , int fd , const Unit * u ) {
2016-11-02 13:51:02 +01:00
assert ( err < 0 ) ;
if ( err = = - ETIMEDOUT )
2016-11-10 10:07:42 +01:00
dprintf ( fd , " Confirmation question timed out for %s, assuming positive response. \n " , u - > id ) ;
2016-11-02 13:51:02 +01:00
else {
errno = - err ;
2016-11-10 10:07:42 +01:00
dprintf ( fd , " Couldn't ask confirmation for %s: %m, assuming positive response. \n " , u - > id ) ;
2016-11-02 13:51:02 +01:00
}
}
2016-11-10 10:07:42 +01:00
static void write_confirm_error ( int err , const char * vc , const Unit * u ) {
2014-03-18 19:22:43 +01:00
_cleanup_close_ int fd = - 1 ;
2010-04-13 02:06:27 +02:00
2016-11-02 13:51:02 +01:00
assert ( vc ) ;
2010-04-13 02:06:27 +02:00
2016-11-02 10:38:22 +01:00
fd = open_terminal ( vc , O_WRONLY | O_NOCTTY | O_CLOEXEC ) ;
2012-06-26 12:16:18 +02:00
if ( fd < 0 )
2016-11-02 13:51:02 +01:00
return ;
2010-04-13 02:06:27 +02:00
2016-11-10 10:07:42 +01:00
write_confirm_error_fd ( err , fd , u ) ;
2012-06-26 12:16:18 +02:00
}
2010-04-13 02:06:27 +02:00
2015-10-08 14:33:53 +02:00
static int restore_confirm_stdio ( int * saved_stdin , int * saved_stdout ) {
2012-06-26 12:16:18 +02:00
int r = 0 ;
2010-04-13 02:06:27 +02:00
2012-06-26 12:16:18 +02:00
assert ( saved_stdin ) ;
assert ( saved_stdout ) ;
release_terminal ( ) ;
if ( * saved_stdin > = 0 )
2010-04-13 02:06:27 +02:00
if ( dup2 ( * saved_stdin , STDIN_FILENO ) < 0 )
2012-06-26 12:16:18 +02:00
r = - errno ;
2010-04-13 02:06:27 +02:00
2012-06-26 12:16:18 +02:00
if ( * saved_stdout > = 0 )
2010-04-13 02:06:27 +02:00
if ( dup2 ( * saved_stdout , STDOUT_FILENO ) < 0 )
2012-06-26 12:16:18 +02:00
r = - errno ;
2010-04-13 02:06:27 +02:00
2015-10-08 14:33:53 +02:00
* saved_stdin = safe_close ( * saved_stdin ) ;
* saved_stdout = safe_close ( * saved_stdout ) ;
2012-06-26 12:16:18 +02:00
return r ;
}
2016-11-02 13:51:02 +01:00
enum {
CONFIRM_PRETEND_FAILURE = - 1 ,
CONFIRM_PRETEND_SUCCESS = 0 ,
CONFIRM_EXECUTE = 1 ,
} ;
2016-11-12 14:55:12 +01:00
static int ask_for_confirmation ( const char * vc , Unit * u , const char * cmdline ) {
2012-06-26 12:16:18 +02:00
int saved_stdout = - 1 , saved_stdin = - 1 , r ;
2016-11-07 17:14:59 +01:00
_cleanup_free_ char * e = NULL ;
2016-11-02 13:51:02 +01:00
char c ;
2012-06-26 12:16:18 +02:00
2016-11-02 13:51:02 +01:00
/* For any internal errors, assume a positive response. */
2016-11-02 10:38:22 +01:00
r = setup_confirm_stdio ( vc , & saved_stdin , & saved_stdout ) ;
2016-11-02 13:51:02 +01:00
if ( r < 0 ) {
2016-11-10 10:07:42 +01:00
write_confirm_error ( r , vc , u ) ;
2016-11-02 13:51:02 +01:00
return CONFIRM_EXECUTE ;
}
2012-06-26 12:16:18 +02:00
2016-11-15 09:29:04 +01:00
/* confirm_spawn might have been disabled while we were sleeping. */
if ( manager_is_confirm_spawn_disabled ( u - > manager ) ) {
r = 1 ;
goto restore_stdio ;
}
2012-06-26 12:16:18 +02:00
2016-11-07 17:14:59 +01:00
e = ellipsize ( cmdline , 60 , 100 ) ;
if ( ! e ) {
log_oom ( ) ;
r = CONFIRM_EXECUTE ;
goto restore_stdio ;
}
2012-06-26 12:16:18 +02:00
2016-11-07 17:14:59 +01:00
for ( ; ; ) {
2016-11-17 18:22:43 +01:00
r = ask_char ( & c , " yfshiDjcn " , " Execute %s? [y, f, s – h for help] " , e ) ;
2016-11-07 17:14:59 +01:00
if ( r < 0 ) {
2016-11-10 10:07:42 +01:00
write_confirm_error_fd ( r , STDOUT_FILENO , u ) ;
2016-11-07 17:14:59 +01:00
r = CONFIRM_EXECUTE ;
goto restore_stdio ;
}
2012-06-26 12:16:18 +02:00
2016-11-07 17:14:59 +01:00
switch ( c ) {
2016-11-15 09:29:04 +01:00
case ' c ' :
printf ( " Resuming normal execution. \n " ) ;
manager_disable_confirm_spawn ( ) ;
r = 1 ;
break ;
2016-11-12 15:08:29 +01:00
case ' D ' :
unit_dump ( u , stdout , " " ) ;
continue ; /* ask again */
2016-11-07 17:14:59 +01:00
case ' f ' :
printf ( " Failing execution. \n " ) ;
r = CONFIRM_PRETEND_FAILURE ;
break ;
case ' h ' :
2016-11-15 09:29:04 +01:00
printf ( " c - continue, proceed without asking anymore \n "
" D - dump, show the state of the unit \n "
2016-11-12 15:08:29 +01:00
" f - fail, don't execute the command and pretend it failed \n "
2016-11-07 17:14:59 +01:00
" h - help \n "
2016-11-12 14:55:12 +01:00
" i - info, show a short summary of the unit \n "
2016-11-13 16:28:04 +01:00
" j - jobs, show jobs that are in progress \n "
2016-11-07 17:14:59 +01:00
" s - skip, don't execute the command and pretend it succeeded \n "
" y - yes, execute the command \n " ) ;
2016-11-12 15:08:29 +01:00
continue ; /* ask again */
2016-11-12 14:55:12 +01:00
case ' i ' :
printf ( " Description: %s \n "
" Unit: %s \n "
" Command: %s \n " ,
u - > id , u - > description , cmdline ) ;
continue ; /* ask again */
2016-11-13 16:28:04 +01:00
case ' j ' :
manager_dump_jobs ( u - > manager , stdout , " " ) ;
continue ; /* ask again */
2016-11-17 18:22:43 +01:00
case ' n ' :
/* 'n' was removed in favor of 'f'. */
printf ( " Didn't understand 'n', did you mean 'f'? \n " ) ;
continue ; /* ask again */
2016-11-07 17:14:59 +01:00
case ' s ' :
printf ( " Skipping execution. \n " ) ;
r = CONFIRM_PRETEND_SUCCESS ;
break ;
case ' y ' :
r = CONFIRM_EXECUTE ;
break ;
default :
assert_not_reached ( " Unhandled choice " ) ;
}
2016-11-02 13:51:02 +01:00
break ;
}
2012-06-26 12:16:18 +02:00
2016-11-02 13:51:02 +01:00
restore_stdio :
2012-06-26 12:16:18 +02:00
restore_confirm_stdio ( & saved_stdin , & saved_stdout ) ;
return r ;
2010-04-13 02:06:27 +02:00
}
2016-10-23 23:24:14 +02:00
static int get_fixed_user ( const ExecContext * c , const char * * user ,
uid_t * uid , gid_t * gid ,
const char * * home , const char * * shell ) {
2010-02-14 22:43:08 +01:00
int r ;
2016-10-23 23:24:14 +02:00
const char * name ;
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
assert ( c ) ;
2010-02-14 22:43:08 +01:00
2017-02-09 11:43:44 +01:00
if ( ! c - > user )
return 0 ;
2016-10-23 23:24:14 +02:00
/* Note that we don't set $HOME or $SHELL if they are not particularly enlightening anyway
* ( i . e . are " / " or " /bin/nologin " ) . */
2010-02-14 22:43:08 +01:00
2017-02-09 11:43:44 +01:00
name = c - > user ;
2016-10-23 23:24:14 +02:00
r = get_user_creds_clean ( & name , uid , gid , home , shell ) ;
if ( r < 0 )
return r ;
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
* user = name ;
return 0 ;
}
static int get_fixed_group ( const ExecContext * c , const char * * group , gid_t * gid ) {
int r ;
const char * name ;
assert ( c ) ;
if ( ! c - > group )
return 0 ;
name = c - > group ;
r = get_group_creds ( & name , gid ) ;
if ( r < 0 )
return r ;
* group = name ;
return 0 ;
}
2016-11-02 22:42:40 +01:00
static int get_supplementary_groups ( const ExecContext * c , const char * user ,
const char * group , gid_t gid ,
gid_t * * supplementary_gids , int * ngids ) {
2016-10-23 23:24:14 +02:00
char * * i ;
int r , k = 0 ;
int ngroups_max ;
bool keep_groups = false ;
gid_t * groups = NULL ;
_cleanup_free_ gid_t * l_gids = NULL ;
assert ( c ) ;
2016-11-02 17:51:35 +01:00
/*
* If user is given , then lookup GID and supplementary groups list .
* We avoid NSS lookups for gid = 0. Also we have to initialize groups
2016-11-02 22:42:40 +01:00
* here and as early as possible so we keep the list of supplementary
* groups of the caller .
2016-11-02 17:51:35 +01:00
*/
if ( user & & gid_is_valid ( gid ) & & gid ! = 0 ) {
/* First step, initialize groups from /etc/groups */
if ( initgroups ( user , gid ) < 0 )
return - errno ;
keep_groups = true ;
}
2017-10-04 11:33:30 +02:00
if ( strv_isempty ( c - > supplementary_groups ) )
2016-10-23 23:24:14 +02:00
return 0 ;
2016-10-24 13:13:06 +02:00
/*
* If SupplementaryGroups = was passed then NGROUPS_MAX has to
* be positive , otherwise fail .
*/
errno = 0 ;
ngroups_max = ( int ) sysconf ( _SC_NGROUPS_MAX ) ;
if ( ngroups_max < = 0 ) {
if ( errno > 0 )
return - errno ;
else
return - EOPNOTSUPP ; /* For all other values */
}
2016-10-23 23:24:14 +02:00
l_gids = new ( gid_t , ngroups_max ) ;
if ( ! l_gids )
return - ENOMEM ;
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
if ( keep_groups ) {
/*
* Lookup the list of groups that the user belongs to , we
* avoid NSS lookups here too for gid = 0.
*/
k = ngroups_max ;
if ( getgrouplist ( user , gid , l_gids , & k ) < 0 )
return - EINVAL ;
} else
k = 0 ;
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
STRV_FOREACH ( i , c - > supplementary_groups ) {
const char * g ;
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
if ( k > = ngroups_max )
return - E2BIG ;
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
g = * i ;
r = get_group_creds ( & g , l_gids + k ) ;
if ( r < 0 )
return r ;
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
k + + ;
}
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
/*
* Sets ngids to zero to drop all supplementary groups , happens
* when we are under root and SupplementaryGroups = is empty .
*/
if ( k = = 0 ) {
* ngids = 0 ;
return 0 ;
}
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
/* Otherwise get the final list of supplementary groups */
groups = memdup ( l_gids , sizeof ( gid_t ) * k ) ;
if ( ! groups )
return - ENOMEM ;
* supplementary_gids = groups ;
* ngids = k ;
groups = NULL ;
return 0 ;
}
2017-10-12 08:10:25 +02:00
static int enforce_groups ( gid_t gid , gid_t * supplementary_gids , int ngids ) {
2016-10-23 23:24:14 +02:00
int r ;
2017-10-12 08:10:25 +02:00
/* Handle SupplementaryGroups= if it is not empty */
if ( ngids > 0 ) {
2016-10-23 23:24:14 +02:00
r = maybe_setgroups ( ngids , supplementary_gids ) ;
if ( r < 0 )
2016-10-06 17:54:12 +02:00
return r ;
2016-10-23 23:24:14 +02:00
}
2010-02-14 22:43:08 +01:00
2016-10-23 23:24:14 +02:00
if ( gid_is_valid ( gid ) ) {
/* Then set our gids */
if ( setresgid ( gid , gid , gid ) < 0 )
return - errno ;
2010-02-14 22:43:08 +01:00
}
return 0 ;
}
static int enforce_user ( const ExecContext * context , uid_t uid ) {
assert ( context ) ;
2016-10-23 23:24:14 +02:00
if ( ! uid_is_valid ( uid ) )
return 0 ;
2016-02-12 23:29:57 +01:00
/* Sets (but doesn't look up) the uid and make sure we keep the
2010-02-14 22:43:08 +01:00
* capabilities while doing so . */
2016-02-12 23:29:57 +01:00
if ( context - > capability_ambient_set ! = 0 ) {
2010-02-14 22:43:08 +01:00
/* First step: If we need to keep capabilities but
* drop privileges we need to make sure we keep our
2013-03-30 06:40:11 +01:00
* caps , while we drop privileges . */
2010-03-31 16:25:33 +02:00
if ( uid ! = 0 ) {
2013-03-30 06:40:11 +01:00
int sb = context - > secure_bits | 1 < < SECURE_KEEP_CAPS ;
2010-03-31 16:25:33 +02:00
if ( prctl ( PR_GET_SECUREBITS ) ! = sb )
if ( prctl ( PR_SET_SECUREBITS , sb ) < 0 )
return - errno ;
}
2010-02-14 22:43:08 +01:00
}
2016-02-12 23:29:57 +01:00
/* Second step: actually set the uids */
2010-02-14 22:43:08 +01:00
if ( setresuid ( uid , uid , uid ) < 0 )
return - errno ;
/* At this point we should have all necessary capabilities but
are otherwise a normal user . However , the caps might got
corrupted due to the setresuid ( ) so we need clean them up
later . This is done outside of this call . */
return 0 ;
}
2017-10-03 10:41:51 +02:00
# if HAVE_PAM
2010-06-16 21:54:17 +02:00
static int null_conv (
int num_msg ,
const struct pam_message * * msg ,
struct pam_response * * resp ,
void * appdata_ptr ) {
/* We don't support conversations */
return PAM_CONV_ERR ;
}
2016-08-26 17:40:42 +02:00
# endif
2010-06-16 21:54:17 +02:00
static int setup_pam (
const char * name ,
const char * user ,
2012-05-17 21:17:42 +02:00
uid_t uid ,
2016-10-06 16:03:01 +02:00
gid_t gid ,
2010-06-16 21:54:17 +02:00
const char * tty ,
2016-06-13 12:50:12 +02:00
char * * * env ,
2010-06-16 21:54:17 +02:00
int fds [ ] , unsigned n_fds ) {
2017-10-03 10:41:51 +02:00
# if HAVE_PAM
2016-08-26 17:40:42 +02:00
2010-06-16 21:54:17 +02:00
static const struct pam_conv conv = {
. conv = null_conv ,
. appdata_ptr = NULL
} ;
2015-09-23 00:51:20 +02:00
_cleanup_ ( barrier_destroy ) Barrier barrier = BARRIER_NULL ;
2010-06-16 21:54:17 +02:00
pam_handle_t * handle = NULL ;
2015-06-17 14:31:49 +02:00
sigset_t old_ss ;
2016-01-22 12:06:39 +01:00
int pam_code = PAM_SUCCESS , r ;
2016-07-07 12:41:52 +02:00
char * * nv , * * e = NULL ;
2010-06-16 21:54:17 +02:00
bool close_session = false ;
pid_t pam_pid = 0 , parent_pid ;
2013-08-28 14:01:30 +02:00
int flags = 0 ;
2010-06-16 21:54:17 +02:00
assert ( name ) ;
assert ( user ) ;
2016-06-13 12:50:12 +02:00
assert ( env ) ;
2010-06-16 21:54:17 +02:00
/* We set up PAM in the parent process, then fork. The child
2011-02-21 15:32:17 +01:00
* will then stay around until killed via PR_GET_PDEATHSIG or
2010-06-16 21:54:17 +02:00
* systemd via the cgroup logic . It will then remove the PAM
* session again . The parent process will exec ( ) the actual
* daemon . We do things this way to ensure that the main PID
* of the daemon is the one we initially fork ( ) ed . */
2016-01-22 12:06:39 +01:00
r = barrier_create ( & barrier ) ;
if ( r < 0 )
2015-09-23 00:51:20 +02:00
goto fail ;
2015-01-06 06:29:40 +01:00
if ( log_get_max_level ( ) < LOG_DEBUG )
2013-08-28 14:01:30 +02:00
flags | = PAM_SILENT ;
2013-08-28 13:54:43 +02:00
pam_code = pam_start ( name , user , & conv , & handle ) ;
if ( pam_code ! = PAM_SUCCESS ) {
2010-06-16 21:54:17 +02:00
handle = NULL ;
goto fail ;
}
2013-08-28 13:54:43 +02:00
if ( tty ) {
pam_code = pam_set_item ( handle , PAM_TTY , tty ) ;
if ( pam_code ! = PAM_SUCCESS )
2010-06-16 21:54:17 +02:00
goto fail ;
2013-08-28 13:54:43 +02:00
}
2010-06-16 21:54:17 +02:00
2016-07-07 12:41:52 +02:00
STRV_FOREACH ( nv , * env ) {
pam_code = pam_putenv ( handle , * nv ) ;
2016-06-13 12:50:12 +02:00
if ( pam_code ! = PAM_SUCCESS )
goto fail ;
}
2013-08-28 14:01:30 +02:00
pam_code = pam_acct_mgmt ( handle , flags ) ;
2013-08-28 13:54:43 +02:00
if ( pam_code ! = PAM_SUCCESS )
2010-06-16 21:54:17 +02:00
goto fail ;
2013-08-28 14:01:30 +02:00
pam_code = pam_open_session ( handle , flags ) ;
2013-08-28 13:54:43 +02:00
if ( pam_code ! = PAM_SUCCESS )
2010-06-16 21:54:17 +02:00
goto fail ;
close_session = true ;
2013-08-28 13:54:43 +02:00
e = pam_getenvlist ( handle ) ;
if ( ! e ) {
2010-06-16 21:54:17 +02:00
pam_code = PAM_BUF_ERR ;
goto fail ;
}
/* Block SIGTERM, so that we know that it won't get lost in
* the child */
2015-05-31 23:55:55 +02:00
2015-06-15 20:13:23 +02:00
assert_se ( sigprocmask_many ( SIG_BLOCK , & old_ss , SIGTERM , - 1 ) > = 0 ) ;
2010-06-16 21:54:17 +02:00
2017-07-20 16:19:18 +02:00
parent_pid = getpid_cached ( ) ;
2010-06-16 21:54:17 +02:00
2013-08-28 13:54:43 +02:00
pam_pid = fork ( ) ;
2016-01-22 12:06:39 +01:00
if ( pam_pid < 0 ) {
r = - errno ;
2010-06-16 21:54:17 +02:00
goto fail ;
2016-01-22 12:06:39 +01:00
}
2010-06-16 21:54:17 +02:00
if ( pam_pid = = 0 ) {
2016-01-22 12:06:39 +01:00
int sig , ret = EXIT_PAM ;
2010-06-16 21:54:17 +02:00
/* The child's job is to reset the PAM session on
* termination */
2015-09-23 00:51:20 +02:00
barrier_set_role ( & barrier , BARRIER_CHILD ) ;
2010-06-16 21:54:17 +02:00
/* This string must fit in 10 chars (i.e. the length
2012-02-01 22:33:15 +01:00
* of " /sbin/init " ) , to look pretty in / bin / ps */
rename_process ( " (sd-pam) " ) ;
2010-06-16 21:54:17 +02:00
/* Make sure we don't keep open the passed fds in this
child . We assume that otherwise only those fds are
open here that have been opened by PAM . */
close_many ( fds , n_fds ) ;
2012-05-17 21:17:42 +02:00
/* Drop privileges - we don't need any to pam_close_session
* and this will make PR_SET_PDEATHSIG work in most cases .
* If this fails , ignore the error - but expect sd - pam threads
* to fail to exit normally */
2016-10-06 16:03:01 +02:00
2016-10-06 17:54:12 +02:00
r = maybe_setgroups ( 0 , NULL ) ;
if ( r < 0 )
log_warning_errno ( r , " Failed to setgroups() in sd-pam: %m " ) ;
2016-10-06 16:03:01 +02:00
if ( setresgid ( gid , gid , gid ) < 0 )
log_warning_errno ( errno , " Failed to setresgid() in sd-pam: %m " ) ;
2012-05-17 21:17:42 +02:00
if ( setresuid ( uid , uid , uid ) < 0 )
2016-10-06 16:03:01 +02:00
log_warning_errno ( errno , " Failed to setresuid() in sd-pam: %m " ) ;
2012-05-17 21:17:42 +02:00
2015-05-31 23:55:55 +02:00
( void ) ignore_signals ( SIGPIPE , - 1 ) ;
2012-05-17 21:17:42 +02:00
/* Wait until our parent died. This will only work if
* the above setresuid ( ) succeeds , otherwise the kernel
* will not allow unprivileged parents kill their privileged
* children this way . We rely on the control groups kill logic
2010-06-16 21:54:17 +02:00
* to do the rest for us . */
if ( prctl ( PR_SET_PDEATHSIG , SIGTERM ) < 0 )
goto child_finish ;
2015-09-23 00:51:20 +02:00
/* Tell the parent that our setup is done. This is especially
* important regarding dropping privileges . Otherwise , unit
2017-02-19 19:48:59 +01:00
* setup might race against our setresuid ( 2 ) call .
*
* If the parent aborted , we ' ll detect this below , hence ignore
* return failure here . */
( void ) barrier_place ( & barrier ) ;
2015-09-23 00:51:20 +02:00
2017-02-19 19:48:59 +01:00
/* Check if our parent process might already have died? */
2010-06-16 21:54:17 +02:00
if ( getppid ( ) = = parent_pid ) {
2015-06-17 14:31:49 +02:00
sigset_t ss ;
assert_se ( sigemptyset ( & ss ) > = 0 ) ;
assert_se ( sigaddset ( & ss , SIGTERM ) > = 0 ) ;
2011-06-30 04:15:39 +02:00
for ( ; ; ) {
if ( sigwait ( & ss , & sig ) < 0 ) {
if ( errno = = EINTR )
continue ;
goto child_finish ;
}
2010-06-16 21:54:17 +02:00
2011-06-30 04:15:39 +02:00
assert ( sig = = SIGTERM ) ;
break ;
}
2010-06-16 21:54:17 +02:00
}
2011-06-30 04:15:39 +02:00
/* If our parent died we'll end the session */
2013-08-28 13:54:43 +02:00
if ( getppid ( ) ! = parent_pid ) {
2013-08-28 14:01:30 +02:00
pam_code = pam_close_session ( handle , flags ) ;
2013-08-28 13:54:43 +02:00
if ( pam_code ! = PAM_SUCCESS )
2010-06-16 21:54:17 +02:00
goto child_finish ;
2013-08-28 13:54:43 +02:00
}
2010-06-16 21:54:17 +02:00
2016-01-22 12:06:39 +01:00
ret = 0 ;
2010-06-16 21:54:17 +02:00
child_finish :
2013-08-28 14:01:30 +02:00
pam_end ( handle , pam_code | flags ) ;
2016-01-22 12:06:39 +01:00
_exit ( ret ) ;
2010-06-16 21:54:17 +02:00
}
2015-09-23 00:51:20 +02:00
barrier_set_role ( & barrier , BARRIER_PARENT ) ;
2010-06-16 21:54:17 +02:00
/* If the child was forked off successfully it will do all the
* cleanups , so forget about the handle here . */
handle = NULL ;
2011-06-30 02:15:01 +02:00
/* Unblock SIGTERM again in the parent */
2015-06-15 20:13:23 +02:00
assert_se ( sigprocmask ( SIG_SETMASK , & old_ss , NULL ) > = 0 ) ;
2010-06-16 21:54:17 +02:00
/* We close the log explicitly here, since the PAM modules
* might have opened it , but we don ' t want this fd around . */
closelog ( ) ;
2015-09-23 00:51:20 +02:00
/* Synchronously wait for the child to initialize. We don't care for
* errors as we cannot recover . However , warn loudly if it happens . */
if ( ! barrier_place_and_sync ( & barrier ) )
log_error ( " PAM initialization failed " ) ;
2016-06-13 12:50:12 +02:00
strv_free ( * env ) ;
* env = e ;
2011-06-30 04:31:34 +02:00
2010-06-16 21:54:17 +02:00
return 0 ;
fail :
2013-08-28 14:01:30 +02:00
if ( pam_code ! = PAM_SUCCESS ) {
log_error ( " PAM failed: %s " , pam_strerror ( handle , pam_code ) ) ;
2016-01-22 12:06:39 +01:00
r = - EPERM ; /* PAM errors do not map to errno */
} else
log_error_errno ( r , " PAM failed: %m " ) ;
2011-11-17 00:16:22 +01:00
2010-06-16 21:54:17 +02:00
if ( handle ) {
if ( close_session )
2013-08-28 14:01:30 +02:00
pam_code = pam_close_session ( handle , flags ) ;
2010-06-16 21:54:17 +02:00
2013-08-28 14:01:30 +02:00
pam_end ( handle , pam_code | flags ) ;
2010-06-16 21:54:17 +02:00
}
strv_free ( e ) ;
closelog ( ) ;
2016-01-22 12:06:39 +01:00
return r ;
2016-08-26 17:40:42 +02:00
# else
return 0 ;
2010-06-16 21:54:17 +02:00
# endif
2016-08-26 17:40:42 +02:00
}
2010-06-16 21:54:17 +02:00
2012-02-01 22:33:15 +01:00
static void rename_process_from_path ( const char * path ) {
char process_name [ 11 ] ;
const char * p ;
size_t l ;
/* This resulting string must fit in 10 chars (i.e. the length
* of " /sbin/init " ) to look pretty in / bin / ps */
2013-12-07 03:29:55 +01:00
p = basename ( path ) ;
2012-02-01 22:33:15 +01:00
if ( isempty ( p ) ) {
rename_process ( " (...) " ) ;
return ;
}
l = strlen ( p ) ;
if ( l > 8 ) {
/* The end of the process name is usually more
* interesting , since the first bit might just be
* " systemd- " */
p = p + l - 8 ;
l = 8 ;
}
process_name [ 0 ] = ' ( ' ;
memcpy ( process_name + 1 , p , l ) ;
process_name [ 1 + l ] = ' ) ' ;
process_name [ 1 + l + 1 ] = 0 ;
rename_process ( process_name ) ;
}
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
static bool context_has_address_families ( const ExecContext * c ) {
assert ( c ) ;
return c - > address_families_whitelist | |
! set_isempty ( c - > address_families ) ;
}
static bool context_has_syscall_filters ( const ExecContext * c ) {
assert ( c ) ;
return c - > syscall_whitelist | |
! set_isempty ( c - > syscall_filter ) ;
}
static bool context_has_no_new_privileges ( const ExecContext * c ) {
assert ( c ) ;
if ( c - > no_new_privileges )
return true ;
if ( have_effective_cap ( CAP_SYS_ADMIN ) ) /* if we are privileged, we don't need NNP */
return false ;
/* We need NNP if we have any form of seccomp and are unprivileged */
return context_has_address_families ( c ) | |
c - > memory_deny_write_execute | |
c - > restrict_realtime | |
exec_context_restrict_namespaces_set ( c ) | |
c - > protect_kernel_tunables | |
c - > protect_kernel_modules | |
c - > private_devices | |
context_has_syscall_filters ( c ) | |
2017-07-04 14:48:18 +02:00
! set_isempty ( c - > syscall_archs ) | |
c - > lock_personality ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
}
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
2014-02-12 18:28:21 +01:00
2016-08-22 21:40:58 +02:00
static bool skip_seccomp_unavailable ( const Unit * u , const char * msg ) {
2016-10-21 20:03:51 +02:00
if ( is_seccomp_available ( ) )
return false ;
log_unit_debug ( u , " SECCOMP features not detected in the kernel, skipping %s " , msg ) ;
return true ;
2016-08-22 21:40:58 +02:00
}
2017-08-09 16:09:04 +02:00
static int apply_syscall_filter ( const Unit * u , const ExecContext * c , bool needs_ambient_hack ) {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
uint32_t negative_action , default_action , action ;
2017-08-09 16:09:04 +02:00
int r ;
2012-07-17 04:17:53 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
assert ( u ) ;
2014-02-12 01:29:54 +01:00
assert ( c ) ;
2012-07-17 04:17:53 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( ! context_has_syscall_filters ( c ) )
2016-08-22 21:40:58 +02:00
return 0 ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( skip_seccomp_unavailable ( u , " SystemCallFilter= " ) )
return 0 ;
2014-02-18 22:14:00 +01:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
negative_action = c - > syscall_errno = = 0 ? SCMP_ACT_KILL : SCMP_ACT_ERRNO ( c - > syscall_errno ) ;
2014-02-18 22:14:00 +01:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( c - > syscall_whitelist ) {
default_action = negative_action ;
action = SCMP_ACT_ALLOW ;
2014-02-25 20:32:27 +01:00
} else {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
default_action = SCMP_ACT_ALLOW ;
action = negative_action ;
2014-02-13 00:24:00 +01:00
}
2012-07-17 04:17:53 +02:00
2017-08-09 16:09:04 +02:00
if ( needs_ambient_hack ) {
r = seccomp_filter_set_add ( c - > syscall_filter , c - > syscall_whitelist , syscall_filter_sets + SYSCALL_FILTER_SET_SETUID ) ;
if ( r < 0 )
return r ;
}
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_load_syscall_filter_set_raw ( default_action , c - > syscall_filter , action ) ;
2014-02-25 20:37:03 +01:00
}
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
static int apply_syscall_archs ( const Unit * u , const ExecContext * c ) {
assert ( u ) ;
2014-02-25 20:37:03 +01:00
assert ( c ) ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( set_isempty ( c - > syscall_archs ) )
2016-08-22 21:40:58 +02:00
return 0 ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( skip_seccomp_unavailable ( u , " SystemCallArchitectures= " ) )
return 0 ;
2014-02-25 20:37:03 +01:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_restrict_archs ( c - > syscall_archs ) ;
}
2014-02-25 20:37:03 +01:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
static int apply_address_families ( const Unit * u , const ExecContext * c ) {
assert ( u ) ;
assert ( c ) ;
2014-02-25 20:37:03 +01:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( ! context_has_address_families ( c ) )
return 0 ;
2014-02-25 20:37:03 +01:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( skip_seccomp_unavailable ( u , " RestrictAddressFamilies= " ) )
return 0 ;
2014-02-25 20:37:03 +01:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_restrict_address_families ( c - > address_families , c - > address_families_whitelist ) ;
2012-07-17 04:17:53 +02:00
}
2014-02-25 20:37:03 +01:00
2016-08-22 21:40:58 +02:00
static int apply_memory_deny_write_execute ( const Unit * u , const ExecContext * c ) {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
assert ( u ) ;
2016-06-03 17:58:18 +02:00
assert ( c ) ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( ! c - > memory_deny_write_execute )
2016-08-22 21:40:58 +02:00
return 0 ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( skip_seccomp_unavailable ( u , " MemoryDenyWriteExecute= " ) )
return 0 ;
2016-06-03 17:58:18 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_memory_deny_write_execute ( ) ;
2016-06-03 17:58:18 +02:00
}
2016-08-22 21:40:58 +02:00
static int apply_restrict_realtime ( const Unit * u , const ExecContext * c ) {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
assert ( u ) ;
2016-06-23 01:45:45 +02:00
assert ( c ) ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( ! c - > restrict_realtime )
2016-08-22 21:40:58 +02:00
return 0 ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( skip_seccomp_unavailable ( u , " RestrictRealtime= " ) )
return 0 ;
2016-06-23 01:45:45 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_restrict_realtime ( ) ;
2016-06-23 01:45:45 +02:00
}
2016-10-27 09:39:20 +02:00
static int apply_protect_sysctl ( const Unit * u , const ExecContext * c ) {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
assert ( u ) ;
2016-08-22 18:43:59 +02:00
assert ( c ) ;
/* Turn off the legacy sysctl() system call. Many distributions turn this off while building the kernel, but
* let ' s protect even those systems where this is left on in the kernel . */
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( ! c - > protect_kernel_tunables )
2016-08-22 18:43:59 +02:00
return 0 ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( skip_seccomp_unavailable ( u , " ProtectKernelTunables= " ) )
return 0 ;
2016-08-22 18:43:59 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_protect_sysctl ( ) ;
2016-08-22 18:43:59 +02:00
}
2016-10-27 09:39:20 +02:00
static int apply_protect_kernel_modules ( const Unit * u , const ExecContext * c ) {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
assert ( u ) ;
2016-10-12 13:31:21 +02:00
assert ( c ) ;
2016-10-21 20:12:33 +02:00
/* Turn off module syscalls on ProtectKernelModules=yes */
2016-10-12 13:31:21 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( ! c - > protect_kernel_modules )
return 0 ;
2016-10-12 13:31:21 +02:00
if ( skip_seccomp_unavailable ( u , " ProtectKernelModules= " ) )
return 0 ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_load_syscall_filter_set ( SCMP_ACT_ALLOW , syscall_filter_sets + SYSCALL_FILTER_SET_MODULE , SCMP_ACT_ERRNO ( EPERM ) ) ;
2016-10-12 13:31:21 +02:00
}
2016-10-27 09:39:20 +02:00
static int apply_private_devices ( const Unit * u , const ExecContext * c ) {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
assert ( u ) ;
2016-08-26 16:39:04 +02:00
assert ( c ) ;
2016-09-25 12:52:27 +02:00
/* If PrivateDevices= is set, also turn off iopl and all @raw-io syscalls. */
2016-08-26 16:39:04 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( ! c - > private_devices )
return 0 ;
2016-08-26 16:39:04 +02:00
if ( skip_seccomp_unavailable ( u , " PrivateDevices= " ) )
return 0 ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
return seccomp_load_syscall_filter_set ( SCMP_ACT_ALLOW , syscall_filter_sets + SYSCALL_FILTER_SET_RAW_IO , SCMP_ACT_ERRNO ( EPERM ) ) ;
2016-08-26 16:39:04 +02:00
}
2016-11-02 03:25:19 +01:00
static int apply_restrict_namespaces ( Unit * u , const ExecContext * c ) {
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
assert ( u ) ;
2016-11-02 03:25:19 +01:00
assert ( c ) ;
if ( ! exec_context_restrict_namespaces_set ( c ) )
return 0 ;
if ( skip_seccomp_unavailable ( u , " RestrictNamespaces= " ) )
return 0 ;
return seccomp_restrict_namespaces ( c - > restrict_namespaces ) ;
}
2017-07-04 14:48:18 +02:00
static int apply_lock_personality ( const Unit * u , const ExecContext * c ) {
2017-08-09 20:40:26 +02:00
unsigned long personality ;
int r ;
2017-07-04 14:48:18 +02:00
assert ( u ) ;
assert ( c ) ;
if ( ! c - > lock_personality )
return 0 ;
if ( skip_seccomp_unavailable ( u , " LockPersonality= " ) )
return 0 ;
2017-08-09 20:40:26 +02:00
personality = c - > personality ;
/* If personality is not specified, use either PER_LINUX or PER_LINUX32 depending on what is currently set. */
if ( personality = = PERSONALITY_INVALID ) {
r = opinionated_personality ( & personality ) ;
if ( r < 0 )
return r ;
}
2017-07-04 14:48:18 +02:00
return seccomp_lock_personality ( personality ) ;
}
2014-02-12 01:29:54 +01:00
# endif
2012-07-17 04:17:53 +02:00
systemd: do not output status messages once gettys are running
Make Type=idle communication bidirectional: when bootup is finished,
the manager, as before, signals idling Type=idle jobs to continue.
However, if the boot takes too long, idling jobs signal the manager
that they have had enough, wait a tiny bit more, and continue, taking
ownership of the console. The manager, when signalled that Type=idle
jobs are done, makes a note and will not write to the console anymore.
This is a cosmetic issue, but quite noticable, so let's just fix it.
Based on Harald Hoyer's patch.
https://bugs.freedesktop.org/show_bug.cgi?id=54247
http://unix.stackexchange.com/questions/51805/systemd-messages-after-starting-login/
2013-07-16 03:34:57 +02:00
static void do_idle_pipe_dance ( int idle_pipe [ 4 ] ) {
assert ( idle_pipe ) ;
2015-09-11 18:14:11 +02:00
idle_pipe [ 1 ] = safe_close ( idle_pipe [ 1 ] ) ;
idle_pipe [ 2 ] = safe_close ( idle_pipe [ 2 ] ) ;
systemd: do not output status messages once gettys are running
Make Type=idle communication bidirectional: when bootup is finished,
the manager, as before, signals idling Type=idle jobs to continue.
However, if the boot takes too long, idling jobs signal the manager
that they have had enough, wait a tiny bit more, and continue, taking
ownership of the console. The manager, when signalled that Type=idle
jobs are done, makes a note and will not write to the console anymore.
This is a cosmetic issue, but quite noticable, so let's just fix it.
Based on Harald Hoyer's patch.
https://bugs.freedesktop.org/show_bug.cgi?id=54247
http://unix.stackexchange.com/questions/51805/systemd-messages-after-starting-login/
2013-07-16 03:34:57 +02:00
if ( idle_pipe [ 0 ] > = 0 ) {
int r ;
r = fd_wait_for_event ( idle_pipe [ 0 ] , POLLHUP , IDLE_TIMEOUT_USEC ) ;
if ( idle_pipe [ 3 ] > = 0 & & r = = 0 /* timeout */ ) {
2015-09-11 18:15:08 +02:00
ssize_t n ;
systemd: do not output status messages once gettys are running
Make Type=idle communication bidirectional: when bootup is finished,
the manager, as before, signals idling Type=idle jobs to continue.
However, if the boot takes too long, idling jobs signal the manager
that they have had enough, wait a tiny bit more, and continue, taking
ownership of the console. The manager, when signalled that Type=idle
jobs are done, makes a note and will not write to the console anymore.
This is a cosmetic issue, but quite noticable, so let's just fix it.
Based on Harald Hoyer's patch.
https://bugs.freedesktop.org/show_bug.cgi?id=54247
http://unix.stackexchange.com/questions/51805/systemd-messages-after-starting-login/
2013-07-16 03:34:57 +02:00
/* Signal systemd that we are bored and want to continue. */
2015-09-11 18:15:08 +02:00
n = write ( idle_pipe [ 3 ] , " x " , 1 ) ;
if ( n > 0 )
2015-03-14 03:20:53 +01:00
/* Wait for systemd to react to the signal above. */
fd_wait_for_event ( idle_pipe [ 0 ] , POLLHUP , IDLE_TIMEOUT2_USEC ) ;
systemd: do not output status messages once gettys are running
Make Type=idle communication bidirectional: when bootup is finished,
the manager, as before, signals idling Type=idle jobs to continue.
However, if the boot takes too long, idling jobs signal the manager
that they have had enough, wait a tiny bit more, and continue, taking
ownership of the console. The manager, when signalled that Type=idle
jobs are done, makes a note and will not write to the console anymore.
This is a cosmetic issue, but quite noticable, so let's just fix it.
Based on Harald Hoyer's patch.
https://bugs.freedesktop.org/show_bug.cgi?id=54247
http://unix.stackexchange.com/questions/51805/systemd-messages-after-starting-login/
2013-07-16 03:34:57 +02:00
}
2015-09-11 18:14:11 +02:00
idle_pipe [ 0 ] = safe_close ( idle_pipe [ 0 ] ) ;
systemd: do not output status messages once gettys are running
Make Type=idle communication bidirectional: when bootup is finished,
the manager, as before, signals idling Type=idle jobs to continue.
However, if the boot takes too long, idling jobs signal the manager
that they have had enough, wait a tiny bit more, and continue, taking
ownership of the console. The manager, when signalled that Type=idle
jobs are done, makes a note and will not write to the console anymore.
This is a cosmetic issue, but quite noticable, so let's just fix it.
Based on Harald Hoyer's patch.
https://bugs.freedesktop.org/show_bug.cgi?id=54247
http://unix.stackexchange.com/questions/51805/systemd-messages-after-starting-login/
2013-07-16 03:34:57 +02:00
}
2015-09-11 18:14:11 +02:00
idle_pipe [ 3 ] = safe_close ( idle_pipe [ 3 ] ) ;
systemd: do not output status messages once gettys are running
Make Type=idle communication bidirectional: when bootup is finished,
the manager, as before, signals idling Type=idle jobs to continue.
However, if the boot takes too long, idling jobs signal the manager
that they have had enough, wait a tiny bit more, and continue, taking
ownership of the console. The manager, when signalled that Type=idle
jobs are done, makes a note and will not write to the console anymore.
This is a cosmetic issue, but quite noticable, so let's just fix it.
Based on Harald Hoyer's patch.
https://bugs.freedesktop.org/show_bug.cgi?id=54247
http://unix.stackexchange.com/questions/51805/systemd-messages-after-starting-login/
2013-07-16 03:34:57 +02:00
}
2013-12-18 17:41:16 +01:00
static int build_environment (
2016-08-02 12:28:51 +02:00
Unit * u ,
2014-08-23 15:28:37 +02:00
const ExecContext * c ,
2016-01-28 16:25:39 +01:00
const ExecParameters * p ,
2013-12-18 17:41:16 +01:00
unsigned n_fds ,
const char * home ,
const char * username ,
const char * shell ,
2016-06-14 16:50:45 +02:00
dev_t journal_stream_dev ,
ino_t journal_stream_ino ,
2013-12-18 17:41:16 +01:00
char * * * ret ) {
_cleanup_strv_free_ char * * our_env = NULL ;
unsigned n_env = 0 ;
char * x ;
2016-08-30 23:18:46 +02:00
assert ( u ) ;
2013-12-18 17:41:16 +01:00
assert ( c ) ;
assert ( ret ) ;
2016-08-30 23:18:46 +02:00
our_env = new0 ( char * , 14 ) ;
2013-12-18 17:41:16 +01:00
if ( ! our_env )
return - ENOMEM ;
if ( n_fds > 0 ) {
2015-10-04 17:36:19 +02:00
_cleanup_free_ char * joined = NULL ;
2017-07-20 16:19:18 +02:00
if ( asprintf ( & x , " LISTEN_PID= " PID_FMT , getpid_cached ( ) ) < 0 )
2013-12-18 17:41:16 +01:00
return - ENOMEM ;
our_env [ n_env + + ] = x ;
if ( asprintf ( & x , " LISTEN_FDS=%u " , n_fds ) < 0 )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
2015-10-04 17:36:19 +02:00
2016-01-28 16:25:39 +01:00
joined = strv_join ( p - > fd_names , " : " ) ;
2015-10-04 17:36:19 +02:00
if ( ! joined )
return - ENOMEM ;
2016-10-23 17:43:27 +02:00
x = strjoin ( " LISTEN_FDNAMES= " , joined ) ;
2015-10-04 17:36:19 +02:00
if ( ! x )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
2013-12-18 17:41:16 +01:00
}
2016-08-04 22:11:29 +02:00
if ( ( p - > flags & EXEC_SET_WATCHDOG ) & & p - > watchdog_usec > 0 ) {
2017-07-20 16:19:18 +02:00
if ( asprintf ( & x , " WATCHDOG_PID= " PID_FMT , getpid_cached ( ) ) < 0 )
2013-12-22 22:14:05 +01:00
return - ENOMEM ;
our_env [ n_env + + ] = x ;
2016-01-28 16:25:39 +01:00
if ( asprintf ( & x , " WATCHDOG_USEC= " USEC_FMT , p - > watchdog_usec ) < 0 )
2013-12-22 22:14:05 +01:00
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
2016-08-02 12:28:51 +02:00
/* If this is D-Bus, tell the nss-systemd module, since it relies on being able to use D-Bus look up dynamic
* users via PID 1 , possibly dead - locking the dbus daemon . This way it will not use D - Bus to resolve names , but
* check the database directly . */
2017-08-01 10:43:04 +02:00
if ( p - > flags & EXEC_NSS_BYPASS_BUS ) {
2016-08-02 12:28:51 +02:00
x = strdup ( " SYSTEMD_NSS_BYPASS_BUS=1 " ) ;
if ( ! x )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
2013-12-18 17:41:16 +01:00
if ( home ) {
x = strappend ( " HOME= " , home ) ;
if ( ! x )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
if ( username ) {
x = strappend ( " LOGNAME= " , username ) ;
if ( ! x )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
x = strappend ( " USER= " , username ) ;
if ( ! x )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
if ( shell ) {
x = strappend ( " SHELL= " , shell ) ;
if ( ! x )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
2016-08-30 23:18:46 +02:00
if ( ! sd_id128_is_null ( u - > invocation_id ) ) {
if ( asprintf ( & x , " INVOCATION_ID= " SD_ID128_FORMAT_STR , SD_ID128_FORMAT_VAL ( u - > invocation_id ) ) < 0 )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
2016-07-27 15:25:55 +02:00
if ( exec_context_needs_term ( c ) ) {
const char * tty_path , * term = NULL ;
tty_path = exec_context_tty_path ( c ) ;
/* If we are forked off PID 1 and we are supposed to operate on /dev/console, then let's try to inherit
* the $ TERM set for PID 1. This is useful for containers so that the $ TERM the container manager
* passes to PID 1 ends up all the way in the console login shown . */
if ( path_equal ( tty_path , " /dev/console " ) & & getppid ( ) = = 1 )
term = getenv ( " TERM " ) ;
if ( ! term )
term = default_term_for_tty ( tty_path ) ;
2013-12-18 17:41:16 +01:00
2016-07-27 15:25:55 +02:00
x = strappend ( " TERM= " , term ) ;
2013-12-18 17:41:16 +01:00
if ( ! x )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
2016-06-14 16:50:45 +02:00
if ( journal_stream_dev ! = 0 & & journal_stream_ino ! = 0 ) {
if ( asprintf ( & x , " JOURNAL_STREAM= " DEV_FMT " : " INO_FMT , journal_stream_dev , journal_stream_ino ) < 0 )
return - ENOMEM ;
our_env [ n_env + + ] = x ;
}
2013-12-18 17:41:16 +01:00
our_env [ n_env + + ] = NULL ;
2016-06-14 16:50:45 +02:00
assert ( n_env < = 12 ) ;
2013-12-18 17:41:16 +01:00
* ret = our_env ;
our_env = NULL ;
return 0 ;
}
2015-09-07 08:06:53 +02:00
static int build_pass_environment ( const ExecContext * c , char * * * ret ) {
_cleanup_strv_free_ char * * pass_env = NULL ;
size_t n_env = 0 , n_bufsize = 0 ;
char * * i ;
STRV_FOREACH ( i , c - > pass_environment ) {
_cleanup_free_ char * x = NULL ;
char * v ;
v = getenv ( * i ) ;
if ( ! v )
continue ;
2016-10-23 17:43:27 +02:00
x = strjoin ( * i , " = " , v ) ;
2015-09-07 08:06:53 +02:00
if ( ! x )
return - ENOMEM ;
2017-09-10 12:16:44 +02:00
2015-09-07 08:06:53 +02:00
if ( ! GREEDY_REALLOC ( pass_env , n_bufsize , n_env + 2 ) )
return - ENOMEM ;
2017-09-10 12:16:44 +02:00
2015-09-07 08:06:53 +02:00
pass_env [ n_env + + ] = x ;
pass_env [ n_env ] = NULL ;
x = NULL ;
}
* ret = pass_env ;
pass_env = NULL ;
return 0 ;
}
2015-05-13 16:34:02 +02:00
static bool exec_needs_mount_namespace (
const ExecContext * context ,
const ExecParameters * params ,
ExecRuntime * runtime ) {
assert ( context ) ;
assert ( params ) ;
2016-12-23 14:26:05 +01:00
if ( context - > root_image )
return true ;
2016-07-07 11:17:00 +02:00
if ( ! strv_isempty ( context - > read_write_paths ) | |
! strv_isempty ( context - > read_only_paths ) | |
! strv_isempty ( context - > inaccessible_paths ) )
2015-05-13 16:34:02 +02:00
return true ;
2016-11-23 22:21:40 +01:00
if ( context - > n_bind_mounts > 0 )
return true ;
2015-05-13 16:34:02 +02:00
if ( context - > mount_flags ! = 0 )
return true ;
if ( context - > private_tmp & & runtime & & ( runtime - > tmp_dir | | runtime - > var_tmp_dir ) )
return true ;
if ( context - > private_devices | |
context - > protect_system ! = PROTECT_SYSTEM_NO | |
2016-08-22 18:43:59 +02:00
context - > protect_home ! = PROTECT_HOME_NO | |
context - > protect_kernel_tunables | |
2016-10-12 14:11:16 +02:00
context - > protect_kernel_modules | |
2016-08-22 18:43:59 +02:00
context - > protect_control_groups )
2015-05-13 16:34:02 +02:00
return true ;
2017-03-05 21:39:43 +01:00
if ( context - > mount_apivfs & & ( context - > root_image | | context - > root_directory ) )
2016-12-22 23:34:35 +01:00
return true ;
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
if ( context - > dynamic_user & &
( ! strv_isempty ( context - > directories [ EXEC_DIRECTORY_RUNTIME ] . paths ) | |
! strv_isempty ( context - > directories [ EXEC_DIRECTORY_STATE ] . paths ) | |
! strv_isempty ( context - > directories [ EXEC_DIRECTORY_CACHE ] . paths ) | |
! strv_isempty ( context - > directories [ EXEC_DIRECTORY_LOGS ] . paths ) ) )
return true ;
2015-05-13 16:34:02 +02:00
return false ;
}
2016-08-03 18:44:51 +02:00
static int setup_private_users ( uid_t uid , gid_t gid ) {
_cleanup_free_ char * uid_map = NULL , * gid_map = NULL ;
_cleanup_close_pair_ int errno_pipe [ 2 ] = { - 1 , - 1 } ;
_cleanup_close_ int unshare_ready_fd = - 1 ;
_cleanup_ ( sigkill_waitp ) pid_t pid = 0 ;
uint64_t c = 1 ;
siginfo_t si ;
ssize_t n ;
int r ;
/* Set up a user namespace and map root to root, the selected UID/GID to itself, and everything else to
* nobody . In order to be able to write this mapping we need CAP_SETUID in the original user namespace , which
* we however lack after opening the user namespace . To work around this we fork ( ) a temporary child process ,
* which waits for the parent to create the new user namespace while staying in the original namespace . The
* child then writes the UID mapping , under full privileges . The parent waits for the child to finish and
* continues execution normally . */
2017-01-31 17:31:47 +01:00
if ( uid ! = 0 & & uid_is_valid ( uid ) ) {
r = asprintf ( & uid_map ,
" 0 0 1 \n " /* Map root → root */
UID_FMT " " UID_FMT " 1 \n " , /* Map $UID → $UID */
uid , uid ) ;
if ( r < 0 )
return - ENOMEM ;
} else {
2016-10-21 20:05:49 +02:00
uid_map = strdup ( " 0 0 1 \n " ) ; /* The case where the above is the same */
2017-01-31 17:31:47 +01:00
if ( ! uid_map )
return - ENOMEM ;
}
2016-08-03 18:44:51 +02:00
2017-01-31 17:31:47 +01:00
if ( gid ! = 0 & & gid_is_valid ( gid ) ) {
r = asprintf ( & gid_map ,
" 0 0 1 \n " /* Map root → root */
GID_FMT " " GID_FMT " 1 \n " , /* Map $GID → $GID */
gid , gid ) ;
if ( r < 0 )
return - ENOMEM ;
} else {
2016-08-03 18:44:51 +02:00
gid_map = strdup ( " 0 0 1 \n " ) ; /* The case where the above is the same */
2017-01-31 17:31:47 +01:00
if ( ! gid_map )
return - ENOMEM ;
}
2016-08-03 18:44:51 +02:00
/* Create a communication channel so that the parent can tell the child when it finished creating the user
* namespace . */
unshare_ready_fd = eventfd ( 0 , EFD_CLOEXEC ) ;
if ( unshare_ready_fd < 0 )
return - errno ;
/* Create a communication channel so that the child can tell the parent a proper error code in case it
* failed . */
if ( pipe2 ( errno_pipe , O_CLOEXEC ) < 0 )
return - errno ;
pid = fork ( ) ;
if ( pid < 0 )
return - errno ;
if ( pid = = 0 ) {
_cleanup_close_ int fd = - 1 ;
const char * a ;
pid_t ppid ;
/* Child process, running in the original user namespace. Let's update the parent's UID/GID map from
* here , after the parent opened its own user namespace . */
ppid = getppid ( ) ;
errno_pipe [ 0 ] = safe_close ( errno_pipe [ 0 ] ) ;
/* Wait until the parent unshared the user namespace */
if ( read ( unshare_ready_fd , & c , sizeof ( c ) ) < 0 ) {
r = - errno ;
goto child_fail ;
}
/* Disable the setgroups() system call in the child user namespace, for good. */
a = procfs_file_alloca ( ppid , " setgroups " ) ;
fd = open ( a , O_WRONLY | O_CLOEXEC ) ;
if ( fd < 0 ) {
if ( errno ! = ENOENT ) {
r = - errno ;
goto child_fail ;
}
/* If the file is missing the kernel is too old, let's continue anyway. */
} else {
if ( write ( fd , " deny \n " , 5 ) < 0 ) {
r = - errno ;
goto child_fail ;
}
fd = safe_close ( fd ) ;
}
/* First write the GID map */
a = procfs_file_alloca ( ppid , " gid_map " ) ;
fd = open ( a , O_WRONLY | O_CLOEXEC ) ;
if ( fd < 0 ) {
r = - errno ;
goto child_fail ;
}
if ( write ( fd , gid_map , strlen ( gid_map ) ) < 0 ) {
r = - errno ;
goto child_fail ;
}
fd = safe_close ( fd ) ;
/* The write the UID map */
a = procfs_file_alloca ( ppid , " uid_map " ) ;
fd = open ( a , O_WRONLY | O_CLOEXEC ) ;
if ( fd < 0 ) {
r = - errno ;
goto child_fail ;
}
if ( write ( fd , uid_map , strlen ( uid_map ) ) < 0 ) {
r = - errno ;
goto child_fail ;
}
_exit ( EXIT_SUCCESS ) ;
child_fail :
( void ) write ( errno_pipe [ 1 ] , & r , sizeof ( r ) ) ;
_exit ( EXIT_FAILURE ) ;
}
errno_pipe [ 1 ] = safe_close ( errno_pipe [ 1 ] ) ;
if ( unshare ( CLONE_NEWUSER ) < 0 )
return - errno ;
/* Let the child know that the namespace is ready now */
if ( write ( unshare_ready_fd , & c , sizeof ( c ) ) < 0 )
return - errno ;
/* Try to read an error code from the child */
n = read ( errno_pipe [ 0 ] , & r , sizeof ( r ) ) ;
if ( n < 0 )
return - errno ;
if ( n = = sizeof ( r ) ) { /* an error code was sent to us */
if ( r < 0 )
return r ;
return - EIO ;
}
if ( n ! = 0 ) /* on success we should have read 0 bytes */
return - EIO ;
r = wait_for_terminate ( pid , & si ) ;
if ( r < 0 )
return r ;
pid = 0 ;
/* If something strange happened with the child, let's consider this fatal, too */
if ( si . si_code ! = CLD_EXITED | | si . si_status ! = 0 )
return - EIO ;
return 0 ;
}
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
static int setup_exec_directory (
2016-08-25 10:12:57 +02:00
const ExecContext * context ,
const ExecParameters * params ,
uid_t uid ,
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
gid_t gid ,
ExecDirectoryType type ,
int * exit_status ) {
2016-08-25 10:12:57 +02:00
2017-09-28 16:58:43 +02:00
static const int exit_status_table [ _EXEC_DIRECTORY_TYPE_MAX ] = {
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
[ EXEC_DIRECTORY_RUNTIME ] = EXIT_RUNTIME_DIRECTORY ,
[ EXEC_DIRECTORY_STATE ] = EXIT_STATE_DIRECTORY ,
[ EXEC_DIRECTORY_CACHE ] = EXIT_CACHE_DIRECTORY ,
[ EXEC_DIRECTORY_LOGS ] = EXIT_LOGS_DIRECTORY ,
[ EXEC_DIRECTORY_CONFIGURATION ] = EXIT_CONFIGURATION_DIRECTORY ,
} ;
2016-08-25 10:12:57 +02:00
char * * rt ;
int r ;
assert ( context ) ;
assert ( params ) ;
2017-09-28 16:58:43 +02:00
assert ( type > = 0 & & type < _EXEC_DIRECTORY_TYPE_MAX ) ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
assert ( exit_status ) ;
2016-08-25 10:12:57 +02:00
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
if ( ! params - > prefix [ type ] )
return 0 ;
2017-08-01 10:35:10 +02:00
if ( params - > flags & EXEC_CHOWN_DIRECTORIES ) {
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
if ( ! uid_is_valid ( uid ) )
uid = 0 ;
if ( ! gid_is_valid ( gid ) )
gid = 0 ;
}
STRV_FOREACH ( rt , context - > directories [ type ] . paths ) {
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
_cleanup_free_ char * p = NULL , * pp = NULL ;
const char * effective ;
2016-08-25 10:12:57 +02:00
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
p = strjoin ( params - > prefix [ type ] , " / " , * rt ) ;
if ( ! p ) {
r = - ENOMEM ;
goto fail ;
}
2016-08-25 10:12:57 +02:00
2017-07-17 09:30:53 +02:00
r = mkdir_parents_label ( p , 0755 ) ;
if ( r < 0 )
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
goto fail ;
2017-07-17 09:30:53 +02:00
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
if ( context - > dynamic_user & & type ! = EXEC_DIRECTORY_CONFIGURATION ) {
_cleanup_free_ char * private_root = NULL , * relative = NULL , * parent = NULL ;
/* So, here's one extra complication when dealing with DynamicUser=1 units. In that case we
* want to avoid leaving a directory around fully accessible that is owned by a dynamic user
* whose UID is later on reused . To lock this down we use the same trick used by container
* managers to prohibit host users to get access to files of the same UID in containers : we
* place everything inside a directory that has an access mode of 0700 and is owned root : root ,
* so that it acts as security boundary for unprivileged host code . We then use fs namespacing
* to make this directory permeable for the service itself .
*
* Specifically : for a service which wants a special directory " foo/ " we first create a
* directory " private/ " with access mode 0700 owned by root : root . Then we place " foo " inside of
* that directory ( i . e . " private/foo/ " ) , and make " foo " a symlink to " private/foo " . This way ,
* privileged host users can access " foo/ " as usual , but unprivileged host users can ' t look
* into it . Inside of the namespaceof the container " private/ " is replaced by a more liberally
* accessible tmpfs , into which the host ' s " private/foo/ " is mounted under the same name , thus
* disabling the access boundary for the service and making sure it only gets access to the
* dirs it needs but no others . Tricky ? Yes , absolutely , but it works !
*
* Note that we don ' t do this for EXEC_DIRECTORY_CONFIGURATION as that ' s assumed not to be
* owned by the service itself . */
private_root = strjoin ( params - > prefix [ type ] , " /private " ) ;
if ( ! private_root ) {
r = - ENOMEM ;
goto fail ;
}
/* First set up private root if it doesn't exist yet, with access mode 0700 and owned by root:root */
2017-10-06 09:03:33 +02:00
r = mkdir_safe_label ( private_root , 0700 , 0 , 0 , false ) ;
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
if ( r < 0 )
goto fail ;
pp = strjoin ( private_root , " / " , * rt ) ;
if ( ! pp ) {
r = - ENOMEM ;
goto fail ;
}
/* Create all directories between the configured directory and this private root, and mark them 0755 */
r = mkdir_parents_label ( pp , 0755 ) ;
if ( r < 0 )
goto fail ;
/* Finally, create the actual directory for the service */
r = mkdir_label ( pp , context - > directories [ type ] . mode ) ;
if ( r < 0 & & r ! = - EEXIST )
goto fail ;
parent = dirname_malloc ( p ) ;
if ( ! parent ) {
r = - ENOMEM ;
goto fail ;
}
r = path_make_relative ( parent , pp , & relative ) ;
if ( r < 0 )
goto fail ;
/* And link it up from the original place */
r = symlink_idempotent ( relative , p ) ;
if ( r < 0 )
goto fail ;
effective = pp ;
} else {
r = mkdir_label ( p , context - > directories [ type ] . mode ) ;
if ( r < 0 & & r ! = - EEXIST )
goto fail ;
effective = p ;
}
2017-09-28 19:13:44 +02:00
/* First lock down the access mode */
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
if ( chmod ( effective , context - > directories [ type ] . mode ) < 0 ) {
2017-09-28 19:13:44 +02:00
r = - errno ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
goto fail ;
2017-09-28 19:13:44 +02:00
}
2016-08-25 10:12:57 +02:00
2017-08-01 10:36:33 +02:00
/* Don't change the owner of the configuration directory, as in the common case it is not written to by
* a service , and shall not be writable . */
if ( type = = EXEC_DIRECTORY_CONFIGURATION )
continue ;
2017-09-28 19:13:44 +02:00
/* Then, change the ownership of the whole tree, if necessary */
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
r = path_chown_recursive ( effective , uid , gid ) ;
2016-08-25 10:12:57 +02:00
if ( r < 0 )
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
goto fail ;
2016-08-25 10:12:57 +02:00
}
return 0 ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
fail :
* exit_status = exit_status_table [ type ] ;
return r ;
2016-08-25 10:12:57 +02:00
}
2016-08-26 17:40:42 +02:00
static int setup_smack (
const ExecContext * context ,
const ExecCommand * command ) {
int r ;
assert ( context ) ;
assert ( command ) ;
if ( context - > smack_process_label ) {
r = mac_smack_apply_pid ( 0 , context - > smack_process_label ) ;
if ( r < 0 )
return r ;
}
# ifdef SMACK_DEFAULT_PROCESS_LABEL
else {
_cleanup_free_ char * exec_label = NULL ;
r = mac_smack_read ( command - > path , SMACK_ATTR_EXEC , & exec_label ) ;
2017-10-04 16:01:32 +02:00
if ( r < 0 & & ! IN_SET ( r , - ENODATA , - EOPNOTSUPP ) )
2016-08-26 17:40:42 +02:00
return r ;
r = mac_smack_apply_pid ( 0 , exec_label ? : SMACK_DEFAULT_PROCESS_LABEL ) ;
if ( r < 0 )
return r ;
}
# endif
return 0 ;
}
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
static int compile_bind_mounts (
const ExecContext * context ,
const ExecParameters * params ,
BindMount * * ret_bind_mounts ,
unsigned * ret_n_bind_mounts ,
char * * * ret_empty_directories ) {
_cleanup_strv_free_ char * * empty_directories = NULL ;
BindMount * bind_mounts ;
unsigned n , h = 0 , i ;
ExecDirectoryType t ;
int r ;
assert ( context ) ;
assert ( params ) ;
assert ( ret_bind_mounts ) ;
assert ( ret_n_bind_mounts ) ;
assert ( ret_empty_directories ) ;
n = context - > n_bind_mounts ;
for ( t = 0 ; t < _EXEC_DIRECTORY_TYPE_MAX ; t + + ) {
if ( ! params - > prefix [ t ] )
continue ;
n + = strv_length ( context - > directories [ t ] . paths ) ;
}
if ( n < = 0 ) {
* ret_bind_mounts = NULL ;
* ret_n_bind_mounts = 0 ;
* ret_empty_directories = NULL ;
return 0 ;
}
bind_mounts = new ( BindMount , n ) ;
if ( ! bind_mounts )
return - ENOMEM ;
2017-10-11 05:27:13 +02:00
for ( i = 0 ; i < context - > n_bind_mounts ; i + + ) {
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
BindMount * item = context - > bind_mounts + i ;
char * s , * d ;
s = strdup ( item - > source ) ;
if ( ! s ) {
r = - ENOMEM ;
goto finish ;
}
d = strdup ( item - > destination ) ;
if ( ! d ) {
free ( s ) ;
r = - ENOMEM ;
goto finish ;
}
bind_mounts [ h + + ] = ( BindMount ) {
. source = s ,
. destination = d ,
. read_only = item - > read_only ,
. recursive = item - > recursive ,
. ignore_enoent = item - > ignore_enoent ,
} ;
}
for ( t = 0 ; t < _EXEC_DIRECTORY_TYPE_MAX ; t + + ) {
char * * suffix ;
if ( ! params - > prefix [ t ] )
continue ;
if ( strv_isempty ( context - > directories [ t ] . paths ) )
continue ;
if ( context - > dynamic_user & & t ! = EXEC_DIRECTORY_CONFIGURATION ) {
char * private_root ;
/* So this is for a dynamic user, and we need to make sure the process can access its own
* directory . For that we overmount the usually inaccessible " private " subdirectory with a
* tmpfs that makes it accessible and is empty except for the submounts we do this for . */
private_root = strjoin ( params - > prefix [ t ] , " /private " ) ;
if ( ! private_root ) {
r = - ENOMEM ;
goto finish ;
}
r = strv_consume ( & empty_directories , private_root ) ;
if ( r < 0 ) {
r = - ENOMEM ;
goto finish ;
}
}
STRV_FOREACH ( suffix , context - > directories [ t ] . paths ) {
char * s , * d ;
if ( context - > dynamic_user & & t ! = EXEC_DIRECTORY_CONFIGURATION )
s = strjoin ( params - > prefix [ t ] , " /private/ " , * suffix ) ;
else
s = strjoin ( params - > prefix [ t ] , " / " , * suffix ) ;
if ( ! s ) {
r = - ENOMEM ;
goto finish ;
}
d = strdup ( s ) ;
if ( ! d ) {
free ( s ) ;
r = - ENOMEM ;
goto finish ;
}
bind_mounts [ h + + ] = ( BindMount ) {
. source = s ,
. destination = d ,
. read_only = false ,
. recursive = true ,
. ignore_enoent = false ,
} ;
}
}
assert ( h = = n ) ;
* ret_bind_mounts = bind_mounts ;
* ret_n_bind_mounts = n ;
* ret_empty_directories = empty_directories ;
empty_directories = NULL ;
return ( int ) n ;
finish :
bind_mount_free_many ( bind_mounts , h ) ;
return r ;
}
core: skip ReadOnlyPaths= and other permission-related mounts on PermissionsStartOnly= (#5309)
ReadOnlyPaths=, ProtectHome=, InaccessiblePaths= and ProtectSystem= are
about restricting access and little more, hence they should be disabled
if PermissionsStartOnly= is used or ExecStart= lines are prefixed with a
"+". Do that.
(Note that we will still create namespaces and stuff, since that's about
a lot more than just permissions. We'll simply disable the effect of
the four options mentioned above, but nothing else mount related.)
This also adds a test for this, to ensure this works as intended.
No documentation updates, as the documentation are already vague enough
to support the new behaviour ("If true, the permission-related execution
options…"). We could clarify this further, but I think we might want to
extend the switches' behaviour a bit more in future, hence leave it at
this for now.
Fixes: #5308
2017-02-12 06:44:46 +01:00
static int apply_mount_namespace (
Unit * u ,
ExecCommand * command ,
const ExecContext * context ,
const ExecParameters * params ,
ExecRuntime * runtime ) {
2017-10-13 14:13:25 +02:00
_cleanup_strv_free_ char * * empty_directories = NULL ;
2016-10-27 09:20:18 +02:00
char * tmp = NULL , * var = NULL ;
2016-12-23 14:26:05 +01:00
const char * root_dir = NULL , * root_image = NULL ;
2017-10-10 09:49:20 +02:00
NamespaceInfo ns_info = {
2016-11-06 23:31:55 +01:00
. ignore_protect_paths = false ,
2016-10-27 09:20:18 +02:00
. private_dev = context - > private_devices ,
. protect_control_groups = context - > protect_control_groups ,
. protect_kernel_tunables = context - > protect_kernel_tunables ,
. protect_kernel_modules = context - > protect_kernel_modules ,
2016-12-22 23:34:35 +01:00
. mount_apivfs = context - > mount_apivfs ,
2016-10-27 09:20:18 +02:00
} ;
2017-08-09 16:09:04 +02:00
bool needs_sandboxing ;
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
BindMount * bind_mounts = NULL ;
unsigned n_bind_mounts = 0 ;
core: skip ReadOnlyPaths= and other permission-related mounts on PermissionsStartOnly= (#5309)
ReadOnlyPaths=, ProtectHome=, InaccessiblePaths= and ProtectSystem= are
about restricting access and little more, hence they should be disabled
if PermissionsStartOnly= is used or ExecStart= lines are prefixed with a
"+". Do that.
(Note that we will still create namespaces and stuff, since that's about
a lot more than just permissions. We'll simply disable the effect of
the four options mentioned above, but nothing else mount related.)
This also adds a test for this, to ensure this works as intended.
No documentation updates, as the documentation are already vague enough
to support the new behaviour ("If true, the permission-related execution
options…"). We could clarify this further, but I think we might want to
extend the switches' behaviour a bit more in future, hence leave it at
this for now.
Fixes: #5308
2017-02-12 06:44:46 +01:00
int r ;
2016-10-27 09:20:18 +02:00
2016-10-27 09:28:54 +02:00
assert ( context ) ;
2016-10-27 09:20:18 +02:00
/* The runtime struct only contains the parent of the private /tmp,
* which is non - accessible to world users . Inside of it there ' s a / tmp
* that is sticky , and that ' s the one we want to use here . */
if ( context - > private_tmp & & runtime ) {
if ( runtime - > tmp_dir )
tmp = strjoina ( runtime - > tmp_dir , " /tmp " ) ;
if ( runtime - > var_tmp_dir )
var = strjoina ( runtime - > var_tmp_dir , " /tmp " ) ;
}
2016-12-23 14:26:05 +01:00
if ( params - > flags & EXEC_APPLY_CHROOT ) {
root_image = context - > root_image ;
if ( ! root_image )
root_dir = context - > root_directory ;
}
2016-10-27 09:20:18 +02:00
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
r = compile_bind_mounts ( context , params , & bind_mounts , & n_bind_mounts , & empty_directories ) ;
if ( r < 0 )
return r ;
2016-11-06 23:31:55 +01:00
/*
* If DynamicUser = no and RootDirectory = is set then lets pass a relaxed
* sandbox info , otherwise enforce it , don ' t ignore protected paths and
* fail if we are enable to apply the sandbox inside the mount namespace .
*/
if ( ! context - > dynamic_user & & root_dir )
ns_info . ignore_protect_paths = true ;
2017-08-09 16:09:04 +02:00
needs_sandboxing = ( params - > flags & EXEC_APPLY_SANDBOXING ) & & ! ( command - > flags & EXEC_COMMAND_FULLY_PRIVILEGED ) ;
core: skip ReadOnlyPaths= and other permission-related mounts on PermissionsStartOnly= (#5309)
ReadOnlyPaths=, ProtectHome=, InaccessiblePaths= and ProtectSystem= are
about restricting access and little more, hence they should be disabled
if PermissionsStartOnly= is used or ExecStart= lines are prefixed with a
"+". Do that.
(Note that we will still create namespaces and stuff, since that's about
a lot more than just permissions. We'll simply disable the effect of
the four options mentioned above, but nothing else mount related.)
This also adds a test for this, to ensure this works as intended.
No documentation updates, as the documentation are already vague enough
to support the new behaviour ("If true, the permission-related execution
options…"). We could clarify this further, but I think we might want to
extend the switches' behaviour a bit more in future, hence leave it at
this for now.
Fixes: #5308
2017-02-12 06:44:46 +01:00
2016-12-23 14:26:05 +01:00
r = setup_namespace ( root_dir , root_image ,
2017-10-13 14:13:25 +02:00
& ns_info , context - > read_write_paths ,
2017-08-09 16:09:04 +02:00
needs_sandboxing ? context - > read_only_paths : NULL ,
needs_sandboxing ? context - > inaccessible_paths : NULL ,
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
empty_directories ,
bind_mounts ,
n_bind_mounts ,
2016-10-27 09:20:18 +02:00
tmp ,
var ,
2017-08-09 16:09:04 +02:00
needs_sandboxing ? context - > protect_home : PROTECT_HOME_NO ,
needs_sandboxing ? context - > protect_system : PROTECT_SYSTEM_NO ,
2016-12-23 14:26:05 +01:00
context - > mount_flags ,
DISSECT_IMAGE_DISCARD_ON_LOOP ) ;
2016-10-27 09:20:18 +02:00
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
bind_mount_free_many ( bind_mounts , n_bind_mounts ) ;
2016-10-27 09:20:18 +02:00
/* If we couldn't set up the namespace this is probably due to a
* missing capability . In this case , silently proceeed . */
if ( IN_SET ( r , - EPERM , - EACCES ) ) {
log_unit_debug_errno ( u , r , " Failed to set up namespace, assuming containerized execution, ignoring: %m " ) ;
2017-09-26 17:41:53 +02:00
return 0 ;
2016-10-27 09:20:18 +02:00
}
return r ;
}
2016-12-23 14:26:05 +01:00
static int apply_working_directory (
const ExecContext * context ,
const ExecParameters * params ,
const char * home ,
2017-02-09 13:17:00 +01:00
const bool needs_mount_ns ,
int * exit_status ) {
2016-12-23 14:26:05 +01:00
2017-02-09 11:58:39 +01:00
const char * d , * wd ;
2016-10-27 09:28:54 +02:00
assert ( context ) ;
2017-02-09 13:17:00 +01:00
assert ( exit_status ) ;
2016-10-27 09:28:54 +02:00
2017-02-09 11:58:39 +01:00
if ( context - > working_directory_home ) {
2017-02-09 13:17:00 +01:00
if ( ! home ) {
* exit_status = EXIT_CHDIR ;
2017-02-09 11:58:39 +01:00
return - ENXIO ;
2017-02-09 13:17:00 +01:00
}
2017-02-09 11:58:39 +01:00
2016-10-27 09:28:54 +02:00
wd = home ;
2017-02-09 11:58:39 +01:00
} else if ( context - > working_directory )
2016-10-27 09:28:54 +02:00
wd = context - > working_directory ;
else
wd = " / " ;
2016-10-27 09:21:44 +02:00
if ( params - > flags & EXEC_APPLY_CHROOT ) {
if ( ! needs_mount_ns & & context - > root_directory )
2017-02-09 13:17:00 +01:00
if ( chroot ( context - > root_directory ) < 0 ) {
* exit_status = EXIT_CHROOT ;
2016-10-27 09:21:44 +02:00
return - errno ;
2017-02-09 13:17:00 +01:00
}
2016-10-27 09:21:44 +02:00
2016-10-27 09:28:54 +02:00
d = wd ;
} else
2017-02-09 13:16:51 +01:00
d = prefix_roota ( context - > root_directory , wd ) ;
2016-10-27 09:21:44 +02:00
2017-02-09 13:17:00 +01:00
if ( chdir ( d ) < 0 & & ! context - > working_directory_missing_ok ) {
* exit_status = EXIT_CHDIR ;
2016-10-27 09:28:54 +02:00
return - errno ;
2017-02-09 13:17:00 +01:00
}
2016-10-27 09:21:44 +02:00
return 0 ;
}
2017-09-14 21:19:05 +02:00
static int setup_keyring (
Unit * u ,
const ExecContext * context ,
const ExecParameters * p ,
uid_t uid , gid_t gid ) {
2016-12-02 01:54:41 +01:00
key_serial_t keyring ;
2017-09-14 21:19:05 +02:00
int r ;
2016-12-02 01:54:41 +01:00
assert ( u ) ;
2017-09-14 21:19:05 +02:00
assert ( context ) ;
2016-12-02 01:54:41 +01:00
assert ( p ) ;
/* Let's set up a new per-service "session" kernel keyring for each system service. This has the benefit that
* each service runs with its own keyring shared among all processes of the service , but with no hook - up beyond
* that scope , and in particular no link to the per - UID keyring . If we don ' t do this the keyring will be
* automatically created on - demand and then linked to the per - UID keyring , by the kernel . The kernel ' s built - in
* on - demand behaviour is very appropriate for login users , but probably not so much for system services , where
* UIDs are not necessarily specific to a service but reused ( at least in the case of UID 0 ) . */
if ( ! ( p - > flags & EXEC_NEW_KEYRING ) )
return 0 ;
2017-09-14 21:19:05 +02:00
if ( context - > keyring_mode = = EXEC_KEYRING_INHERIT )
return 0 ;
2016-12-02 01:54:41 +01:00
keyring = keyctl ( KEYCTL_JOIN_SESSION_KEYRING , 0 , 0 , 0 , 0 ) ;
if ( keyring = = - 1 ) {
if ( errno = = ENOSYS )
2017-09-26 17:42:57 +02:00
log_unit_debug_errno ( u , errno , " Kernel keyring not supported, ignoring. " ) ;
2016-12-02 01:54:41 +01:00
else if ( IN_SET ( errno , EACCES , EPERM ) )
2017-09-26 17:42:57 +02:00
log_unit_debug_errno ( u , errno , " Kernel keyring access prohibited, ignoring. " ) ;
2016-12-02 01:54:41 +01:00
else if ( errno = = EDQUOT )
2017-09-26 17:42:57 +02:00
log_unit_debug_errno ( u , errno , " Out of kernel keyrings to allocate, ignoring. " ) ;
2016-12-02 01:54:41 +01:00
else
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , errno , " Setting up kernel keyring failed: %m " ) ;
2016-12-02 01:54:41 +01:00
return 0 ;
}
core: store the invocation ID in the per-service keyring
Let's store the invocation ID in the per-service keyring as a root-owned key,
with strict access rights. This has the advantage over the environment-based ID
passing that it also works from SUID binaries (as they key cannot be overidden
by unprivileged code starting them), in contrast to the secure_getenv() based
mode.
The invocation ID is now passed in three different ways to a service:
- As environment variable $INVOCATION_ID. This is easy to use, but may be
overriden by unprivileged code (which might be a bad or a good thing), which
means it's incompatible with SUID code (see above).
- As extended attribute on the service cgroup. This cannot be overriden by
unprivileged code, and may be queried safely from "outside" of a service.
However, it is incompatible with containers right now, as unprivileged
containers generally cannot set xattrs on cgroupfs.
- As "invocation_id" key in the kernel keyring. This has the benefit that the
key cannot be changed by unprivileged service code, and thus is safe to
access from SUID code (see above). But do note that service code can replace
the session keyring with a fresh one that lacks the key. However in that case
the key will not be owned by root, which is easily detectable. The keyring is
also incompatible with containers right now, as it is not properly namespace
aware (but this is being worked on), and thus most container managers mask
the keyring-related system calls.
Ideally we'd only have one way to pass the invocation ID, but the different
ways all have limitations. The invocation ID hookup in journald is currently
only available on the host but not in containers, due to the mentioned
limitations.
How to verify the new invocation ID in the keyring:
# systemd-run -t /bin/sh
Running as unit: run-rd917366c04f847b480d486017f7239d6.service
Press ^] three times within 1s to disconnect TTY.
# keyctl show
Session Keyring
680208392 --alswrv 0 0 keyring: _ses
250926536 ----s-rv 0 0 \_ user: invocation_id
# keyctl request user invocation_id
250926536
# keyctl read 250926536
16 bytes of data in key:
9c96317c ac64495a a42b9cd7 4f3ff96b
# echo $INVOCATION_ID
9c96317cac64495aa42b9cd74f3ff96b
# ^D
This creates a new transient service runnint a shell. Then verifies the
contents of the keyring, requests the invocation ID key, and reads its payload.
For comparison the invocation ID as passed via the environment variable is also
displayed.
2016-12-02 15:05:55 +01:00
/* Populate they keyring with the invocation ID by default. */
if ( ! sd_id128_is_null ( u - > invocation_id ) ) {
key_serial_t key ;
key = add_key ( " user " , " invocation_id " , & u - > invocation_id , sizeof ( u - > invocation_id ) , KEY_SPEC_SESSION_KEYRING ) ;
if ( key = = - 1 )
2017-09-26 17:42:57 +02:00
log_unit_debug_errno ( u , errno , " Failed to add invocation ID to keyring, ignoring: %m " ) ;
core: store the invocation ID in the per-service keyring
Let's store the invocation ID in the per-service keyring as a root-owned key,
with strict access rights. This has the advantage over the environment-based ID
passing that it also works from SUID binaries (as they key cannot be overidden
by unprivileged code starting them), in contrast to the secure_getenv() based
mode.
The invocation ID is now passed in three different ways to a service:
- As environment variable $INVOCATION_ID. This is easy to use, but may be
overriden by unprivileged code (which might be a bad or a good thing), which
means it's incompatible with SUID code (see above).
- As extended attribute on the service cgroup. This cannot be overriden by
unprivileged code, and may be queried safely from "outside" of a service.
However, it is incompatible with containers right now, as unprivileged
containers generally cannot set xattrs on cgroupfs.
- As "invocation_id" key in the kernel keyring. This has the benefit that the
key cannot be changed by unprivileged service code, and thus is safe to
access from SUID code (see above). But do note that service code can replace
the session keyring with a fresh one that lacks the key. However in that case
the key will not be owned by root, which is easily detectable. The keyring is
also incompatible with containers right now, as it is not properly namespace
aware (but this is being worked on), and thus most container managers mask
the keyring-related system calls.
Ideally we'd only have one way to pass the invocation ID, but the different
ways all have limitations. The invocation ID hookup in journald is currently
only available on the host but not in containers, due to the mentioned
limitations.
How to verify the new invocation ID in the keyring:
# systemd-run -t /bin/sh
Running as unit: run-rd917366c04f847b480d486017f7239d6.service
Press ^] three times within 1s to disconnect TTY.
# keyctl show
Session Keyring
680208392 --alswrv 0 0 keyring: _ses
250926536 ----s-rv 0 0 \_ user: invocation_id
# keyctl request user invocation_id
250926536
# keyctl read 250926536
16 bytes of data in key:
9c96317c ac64495a a42b9cd7 4f3ff96b
# echo $INVOCATION_ID
9c96317cac64495aa42b9cd74f3ff96b
# ^D
This creates a new transient service runnint a shell. Then verifies the
contents of the keyring, requests the invocation ID key, and reads its payload.
For comparison the invocation ID as passed via the environment variable is also
displayed.
2016-12-02 15:05:55 +01:00
else {
if ( keyctl ( KEYCTL_SETPERM , key ,
KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH , 0 , 0 ) < 0 )
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , errno , " Failed to restrict invocation ID permission: %m " ) ;
core: store the invocation ID in the per-service keyring
Let's store the invocation ID in the per-service keyring as a root-owned key,
with strict access rights. This has the advantage over the environment-based ID
passing that it also works from SUID binaries (as they key cannot be overidden
by unprivileged code starting them), in contrast to the secure_getenv() based
mode.
The invocation ID is now passed in three different ways to a service:
- As environment variable $INVOCATION_ID. This is easy to use, but may be
overriden by unprivileged code (which might be a bad or a good thing), which
means it's incompatible with SUID code (see above).
- As extended attribute on the service cgroup. This cannot be overriden by
unprivileged code, and may be queried safely from "outside" of a service.
However, it is incompatible with containers right now, as unprivileged
containers generally cannot set xattrs on cgroupfs.
- As "invocation_id" key in the kernel keyring. This has the benefit that the
key cannot be changed by unprivileged service code, and thus is safe to
access from SUID code (see above). But do note that service code can replace
the session keyring with a fresh one that lacks the key. However in that case
the key will not be owned by root, which is easily detectable. The keyring is
also incompatible with containers right now, as it is not properly namespace
aware (but this is being worked on), and thus most container managers mask
the keyring-related system calls.
Ideally we'd only have one way to pass the invocation ID, but the different
ways all have limitations. The invocation ID hookup in journald is currently
only available on the host but not in containers, due to the mentioned
limitations.
How to verify the new invocation ID in the keyring:
# systemd-run -t /bin/sh
Running as unit: run-rd917366c04f847b480d486017f7239d6.service
Press ^] three times within 1s to disconnect TTY.
# keyctl show
Session Keyring
680208392 --alswrv 0 0 keyring: _ses
250926536 ----s-rv 0 0 \_ user: invocation_id
# keyctl request user invocation_id
250926536
# keyctl read 250926536
16 bytes of data in key:
9c96317c ac64495a a42b9cd7 4f3ff96b
# echo $INVOCATION_ID
9c96317cac64495aa42b9cd74f3ff96b
# ^D
This creates a new transient service runnint a shell. Then verifies the
contents of the keyring, requests the invocation ID key, and reads its payload.
For comparison the invocation ID as passed via the environment variable is also
displayed.
2016-12-02 15:05:55 +01:00
}
}
2016-12-02 01:54:41 +01:00
/* And now, make the keyring owned by the service's user */
if ( uid_is_valid ( uid ) | | gid_is_valid ( gid ) )
if ( keyctl ( KEYCTL_CHOWN , keyring , uid , gid , 0 ) < 0 )
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , errno , " Failed to change ownership of session keyring: %m " ) ;
2016-12-02 01:54:41 +01:00
2017-09-14 21:19:05 +02:00
/* When requested link the user keyring into the session keyring. */
if ( context - > keyring_mode = = EXEC_KEYRING_SHARED ) {
uid_t saved_uid ;
gid_t saved_gid ;
/* Acquiring a reference to the user keyring is nasty. We briefly change identity in order to get things
* set up properly by the kernel . If we don ' t do that then we can ' t create it atomically , and that
* sucks for parallel execution . This mimics what pam_keyinit does , too . */
saved_uid = getuid ( ) ;
saved_gid = getgid ( ) ;
if ( gid_is_valid ( gid ) & & gid ! = saved_gid ) {
if ( setregid ( gid , - 1 ) < 0 )
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , errno , " Failed to change GID for user keyring: %m " ) ;
2017-09-14 21:19:05 +02:00
}
if ( uid_is_valid ( uid ) & & uid ! = saved_uid ) {
if ( setreuid ( uid , - 1 ) < 0 ) {
( void ) setregid ( saved_gid , - 1 ) ;
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , errno , " Failed to change UID for user keyring: %m " ) ;
2017-09-14 21:19:05 +02:00
}
}
if ( keyctl ( KEYCTL_LINK ,
KEY_SPEC_USER_KEYRING ,
KEY_SPEC_SESSION_KEYRING , 0 , 0 ) < 0 ) {
r = - errno ;
( void ) setreuid ( saved_uid , - 1 ) ;
( void ) setregid ( saved_gid , - 1 ) ;
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , r , " Failed to link user keyring into session keyring: %m " ) ;
2017-09-14 21:19:05 +02:00
}
if ( uid_is_valid ( uid ) & & uid ! = saved_uid ) {
if ( setreuid ( saved_uid , - 1 ) < 0 ) {
( void ) setregid ( saved_gid , - 1 ) ;
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , errno , " Failed to change UID back for user keyring: %m " ) ;
2017-09-14 21:19:05 +02:00
}
}
if ( gid_is_valid ( gid ) & & gid ! = saved_gid ) {
if ( setregid ( saved_gid , - 1 ) < 0 )
2017-09-26 17:42:57 +02:00
return log_unit_error_errno ( u , errno , " Failed to change GID back for user keyring: %m " ) ;
2017-09-14 21:19:05 +02:00
}
2017-09-16 08:45:02 +02:00
}
2017-09-14 21:19:05 +02:00
2016-12-02 01:54:41 +01:00
return 0 ;
}
2016-07-14 12:37:28 +02:00
static void append_socket_pair ( int * array , unsigned * n , int pair [ 2 ] ) {
assert ( array ) ;
assert ( n ) ;
if ( ! pair )
return ;
if ( pair [ 0 ] > = 0 )
array [ ( * n ) + + ] = pair [ 0 ] ;
if ( pair [ 1 ] > = 0 )
array [ ( * n ) + + ] = pair [ 1 ] ;
}
2015-10-07 23:07:39 +02:00
static int close_remaining_fds (
const ExecParameters * params ,
ExecRuntime * runtime ,
2016-07-14 12:37:28 +02:00
DynamicCreds * dcreds ,
2016-08-01 19:24:40 +02:00
int user_lookup_fd ,
2015-10-07 23:07:39 +02:00
int socket_fd ,
int * fds , unsigned n_fds ) {
unsigned n_dont_close = 0 ;
2016-08-01 19:24:40 +02:00
int dont_close [ n_fds + 12 ] ;
2015-10-07 23:07:39 +02:00
assert ( params ) ;
if ( params - > stdin_fd > = 0 )
dont_close [ n_dont_close + + ] = params - > stdin_fd ;
if ( params - > stdout_fd > = 0 )
dont_close [ n_dont_close + + ] = params - > stdout_fd ;
if ( params - > stderr_fd > = 0 )
dont_close [ n_dont_close + + ] = params - > stderr_fd ;
if ( socket_fd > = 0 )
dont_close [ n_dont_close + + ] = socket_fd ;
if ( n_fds > 0 ) {
memcpy ( dont_close + n_dont_close , fds , sizeof ( int ) * n_fds ) ;
n_dont_close + = n_fds ;
}
2016-07-14 12:37:28 +02:00
if ( runtime )
append_socket_pair ( dont_close , & n_dont_close , runtime - > netns_storage_socket ) ;
if ( dcreds ) {
if ( dcreds - > user )
append_socket_pair ( dont_close , & n_dont_close , dcreds - > user - > storage_socket ) ;
if ( dcreds - > group )
append_socket_pair ( dont_close , & n_dont_close , dcreds - > group - > storage_socket ) ;
2015-10-07 23:07:39 +02:00
}
2016-08-01 19:24:40 +02:00
if ( user_lookup_fd > = 0 )
dont_close [ n_dont_close + + ] = user_lookup_fd ;
2015-10-07 23:07:39 +02:00
return close_all_fds ( dont_close , n_dont_close ) ;
}
2016-08-01 19:24:40 +02:00
static int send_user_lookup (
Unit * unit ,
int user_lookup_fd ,
uid_t uid ,
gid_t gid ) {
assert ( unit ) ;
/* Send the resolved UID/GID to PID 1 after we learnt it. We send a single datagram, containing the UID/GID
* data as well as the unit name . Note that we suppress sending this if no user / group to resolve was
* specified . */
if ( user_lookup_fd < 0 )
return 0 ;
if ( ! uid_is_valid ( uid ) & & ! gid_is_valid ( gid ) )
return 0 ;
if ( writev ( user_lookup_fd ,
( struct iovec [ ] ) {
2017-09-21 13:52:34 +02:00
IOVEC_INIT ( & uid , sizeof ( uid ) ) ,
IOVEC_INIT ( & gid , sizeof ( gid ) ) ,
IOVEC_INIT_STRING ( unit - > id ) } , 3 ) < 0 )
2016-08-01 19:24:40 +02:00
return - errno ;
return 0 ;
}
2017-02-09 11:58:39 +01:00
static int acquire_home ( const ExecContext * c , uid_t uid , const char * * home , char * * buf ) {
int r ;
assert ( c ) ;
assert ( home ) ;
assert ( buf ) ;
/* If WorkingDirectory=~ is set, try to acquire a usable home directory. */
if ( * home )
return 0 ;
if ( ! c - > working_directory_home )
return 0 ;
if ( uid = = 0 ) {
/* Hardcode /root as home directory for UID 0 */
* home = " /root " ;
return 1 ;
}
r = get_home_dir ( buf ) ;
if ( r < 0 )
return r ;
* home = * buf ;
return 1 ;
}
2017-09-28 20:28:09 +02:00
static int compile_suggested_paths ( const ExecContext * c , const ExecParameters * p , char * * * ret ) {
_cleanup_strv_free_ char * * list = NULL ;
ExecDirectoryType t ;
int r ;
assert ( c ) ;
assert ( p ) ;
assert ( ret ) ;
assert ( c - > dynamic_user ) ;
/* Compile a list of paths that it might make sense to read the owning UID from to use as initial candidate for
* dynamic UID allocation , in order to save us from doing costly recursive chown ( ) s of the special
* directories . */
for ( t = 0 ; t < _EXEC_DIRECTORY_TYPE_MAX ; t + + ) {
char * * i ;
if ( t = = EXEC_DIRECTORY_CONFIGURATION )
continue ;
if ( ! p - > prefix [ t ] )
continue ;
STRV_FOREACH ( i , c - > directories [ t ] . paths ) {
char * e ;
e = strjoin ( p - > prefix [ t ] , " /private/ " , * i ) ;
if ( ! e )
return - ENOMEM ;
r = strv_consume ( & list , e ) ;
if ( r < 0 )
return r ;
}
}
* ret = list ;
list = NULL ;
return 0 ;
}
2015-01-09 00:13:33 +01:00
static int exec_child (
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
Unit * unit ,
2015-01-09 00:13:33 +01:00
ExecCommand * command ,
const ExecContext * context ,
const ExecParameters * params ,
ExecRuntime * runtime ,
2016-07-14 12:37:28 +02:00
DynamicCreds * dcreds ,
2015-01-09 00:13:33 +01:00
char * * argv ,
int socket_fd ,
2016-10-18 02:05:49 +02:00
int named_iofds [ 3 ] ,
2017-06-08 15:41:26 +02:00
int * fds ,
unsigned n_storage_fds ,
2017-05-12 11:32:53 +02:00
unsigned n_socket_fds ,
2015-01-09 00:13:33 +01:00
char * * files_env ,
2016-08-01 19:24:40 +02:00
int user_lookup_fd ,
2017-09-26 17:47:27 +02:00
int * exit_status ) {
2014-08-23 16:02:21 +02:00
2016-06-13 12:50:12 +02:00
_cleanup_strv_free_ char * * our_env = NULL , * * pass_env = NULL , * * accum_env = NULL , * * final_argv = NULL ;
2017-02-09 11:58:39 +01:00
_cleanup_free_ char * mac_selinux_context_net = NULL , * home_buffer = NULL ;
2016-10-23 23:24:14 +02:00
_cleanup_free_ gid_t * supplementary_gids = NULL ;
const char * username = NULL , * groupname = NULL ;
2016-10-27 09:28:54 +02:00
const char * home = NULL , * shell = NULL ;
2016-06-14 16:50:45 +02:00
dev_t journal_stream_dev = 0 ;
ino_t journal_stream_ino = 0 ;
2017-08-09 16:09:04 +02:00
bool needs_sandboxing , /* Do we need to set up full sandboxing? (i.e. all namespacing, all MAC stuff, caps, yadda yadda */
needs_setuid , /* Do we need to do the actual setresuid()/setresgid() calls? */
needs_mount_namespace , /* Do we need to set up a mount namespace for this kernel? */
needs_ambient_hack ; /* Do we need to apply the ambient capabilities hack? */
2017-10-03 10:41:51 +02:00
# if HAVE_SELINUX
2017-08-08 19:49:04 +02:00
bool use_selinux = false ;
2017-08-02 07:38:08 +02:00
# endif
2017-10-03 12:22:40 +02:00
# if ENABLE_SMACK
2017-08-08 19:49:04 +02:00
bool use_smack = false ;
2017-08-02 07:38:08 +02:00
# endif
2017-10-03 10:41:51 +02:00
# if HAVE_APPARMOR
2017-08-08 19:49:04 +02:00
bool use_apparmor = false ;
2017-08-02 07:38:08 +02:00
# endif
2014-11-28 20:51:01 +01:00
uid_t uid = UID_INVALID ;
gid_t gid = GID_INVALID ;
2016-10-23 23:24:14 +02:00
int i , r , ngids = 0 ;
2017-06-08 15:41:26 +02:00
unsigned n_fds ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
ExecDirectoryType dt ;
2017-08-09 16:09:04 +02:00
int secure_bits ;
2010-01-26 04:18:44 +01:00
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
assert ( unit ) ;
2010-01-23 01:52:57 +01:00
assert ( command ) ;
assert ( context ) ;
2014-08-23 16:02:21 +02:00
assert ( params ) ;
2015-01-09 00:13:33 +01:00
assert ( exit_status ) ;
2014-08-23 16:02:21 +02:00
rename_process_from_path ( command - > path ) ;
/* We reset exactly these signals, since they are the
* only ones we set to SIG_IGN in the main daemon . All
* others we leave untouched because we set them to
* SIG_DFL or a valid handler initially , both of which
* will be demoted to SIG_DFL . */
2015-05-31 23:55:55 +02:00
( void ) default_signals ( SIGNALS_CRASH_HANDLER ,
SIGNALS_IGNORE , - 1 ) ;
2014-08-23 16:02:21 +02:00
if ( context - > ignore_sigpipe )
2015-05-31 23:55:55 +02:00
( void ) ignore_signals ( SIGPIPE , - 1 ) ;
2014-08-23 16:02:21 +02:00
2015-01-09 00:13:33 +01:00
r = reset_signal_mask ( ) ;
if ( r < 0 ) {
* exit_status = EXIT_SIGNAL_MASK ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set process signal mask: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2010-01-26 04:18:44 +01:00
2014-08-23 16:02:21 +02:00
if ( params - > idle_pipe )
do_idle_pipe_dance ( params - > idle_pipe ) ;
2010-04-15 06:19:54 +02:00
2017-09-26 17:45:32 +02:00
/* Close fds we don't need very early to make sure we don't block init reexecution because it cannot bind its
* sockets . Among the fds we close are the logging fds , and we want to keep them closed , so that we don ' t have
* any fds open we don ' t really want open during the transition . In order to make logging work , we switch the
* log subsystem into open_when_needed mode , so that it reopens the logs on every single log call . */
2015-01-09 00:13:33 +01:00
2014-08-23 16:02:21 +02:00
log_forget_fds ( ) ;
2017-09-26 17:45:32 +02:00
log_set_open_when_needed ( true ) ;
2010-04-15 06:19:54 +02:00
2017-09-26 17:52:25 +02:00
/* In case anything used libc syslog(), close this here, too */
closelog ( ) ;
2017-06-08 15:41:26 +02:00
n_fds = n_storage_fds + n_socket_fds ;
2016-08-01 19:24:40 +02:00
r = close_remaining_fds ( params , runtime , dcreds , user_lookup_fd , socket_fd , fds , n_fds ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
* exit_status = EXIT_FDS ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to close unwanted file descriptors: %m " ) ;
2011-03-04 03:44:43 +01:00
}
2014-08-23 16:02:21 +02:00
if ( ! context - > same_pgrp )
if ( setsid ( ) < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_SETSID ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to create new process session: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2010-04-15 03:11:11 +02:00
2016-01-28 16:25:39 +01:00
exec_context_tty_reset ( context , params ) ;
2014-08-23 16:02:21 +02:00
2016-11-14 17:37:40 +01:00
if ( unit_shall_confirm_spawn ( unit ) ) {
2016-11-02 10:38:22 +01:00
const char * vc = params - > confirm_spawn ;
2016-11-02 13:51:02 +01:00
_cleanup_free_ char * cmdline = NULL ;
cmdline = exec_command_line ( argv ) ;
if ( ! cmdline ) {
2017-09-15 16:42:09 +02:00
* exit_status = EXIT_MEMORY ;
2017-09-26 17:47:27 +02:00
return log_oom ( ) ;
2016-11-02 13:51:02 +01:00
}
2014-08-23 16:02:21 +02:00
2016-11-12 14:55:12 +01:00
r = ask_for_confirmation ( vc , unit , cmdline ) ;
2016-11-02 13:51:02 +01:00
if ( r ! = CONFIRM_EXECUTE ) {
if ( r = = CONFIRM_PRETEND_SUCCESS ) {
* exit_status = EXIT_SUCCESS ;
return 0 ;
}
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_CONFIRM ;
2017-09-26 17:47:27 +02:00
log_unit_error ( unit , " Execution cancelled by the user " ) ;
2014-08-23 16:02:21 +02:00
return - ECANCELED ;
}
}
2010-04-10 17:46:01 +02:00
2016-07-14 12:37:28 +02:00
if ( context - > dynamic_user & & dcreds ) {
2017-09-28 20:28:09 +02:00
_cleanup_strv_free_ char * * suggested_paths = NULL ;
2016-07-14 12:37:28 +02:00
2016-07-14 19:19:49 +02:00
/* Make sure we bypass our own NSS module for any NSS checks */
if ( putenv ( ( char * ) " SYSTEMD_NSS_DYNAMIC_BYPASS=1 " ) ! = 0 ) {
* exit_status = EXIT_USER ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to update environment: %m " ) ;
2016-07-14 19:19:49 +02:00
}
2017-09-28 20:28:09 +02:00
r = compile_suggested_paths ( context , params , & suggested_paths ) ;
if ( r < 0 ) {
* exit_status = EXIT_MEMORY ;
return log_oom ( ) ;
}
r = dynamic_creds_realize ( dcreds , suggested_paths , & uid , & gid ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
* exit_status = EXIT_USER ;
2017-10-18 01:57:54 +02:00
if ( r = = - EILSEQ ) {
log_unit_error ( unit , " Failed to update dynamic user credentials: User or group with specified name already exists. " ) ;
return - EOPNOTSUPP ;
}
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to update dynamic user credentials: %m " ) ;
2015-01-01 04:40:41 +01:00
}
2017-01-18 04:38:55 +01:00
if ( ! uid_is_valid ( uid ) ) {
2016-07-14 12:37:28 +02:00
* exit_status = EXIT_USER ;
2017-09-26 17:47:27 +02:00
log_unit_error ( unit , " UID validation failed for \" " UID_FMT " \" " , uid ) ;
2017-01-18 04:38:55 +01:00
return - ESRCH ;
}
if ( ! gid_is_valid ( gid ) ) {
* exit_status = EXIT_USER ;
2017-09-26 17:47:27 +02:00
log_unit_error ( unit , " GID validation failed for \" " GID_FMT " \" " , gid ) ;
2016-07-14 12:37:28 +02:00
return - ESRCH ;
}
2015-09-21 15:45:51 +02:00
2016-07-14 12:37:28 +02:00
if ( dcreds - > user )
username = dcreds - > user - > name ;
} else {
2016-10-23 23:24:14 +02:00
r = get_fixed_user ( context , & username , & uid , & gid , & home , & shell ) ;
if ( r < 0 ) {
* exit_status = EXIT_USER ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to determine user credentials: %m " ) ;
2015-09-21 15:45:51 +02:00
}
2016-10-23 23:24:14 +02:00
r = get_fixed_group ( context , & groupname , & gid ) ;
if ( r < 0 ) {
* exit_status = EXIT_GROUP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to determine group credentials: %m " ) ;
2016-10-23 23:24:14 +02:00
}
2016-11-02 22:42:40 +01:00
}
2016-07-14 12:37:28 +02:00
2016-11-02 22:42:40 +01:00
/* Initialize user supplementary groups and get SupplementaryGroups= ones */
r = get_supplementary_groups ( context , username , groupname , gid ,
& supplementary_gids , & ngids ) ;
if ( r < 0 ) {
* exit_status = EXIT_GROUP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to determine supplementary groups: %m " ) ;
2016-07-14 12:37:28 +02:00
}
2015-09-21 15:45:51 +02:00
2016-08-01 19:24:40 +02:00
r = send_user_lookup ( unit , user_lookup_fd , uid , gid ) ;
if ( r < 0 ) {
* exit_status = EXIT_USER ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to send user credentials to PID1: %m " ) ;
2016-08-01 19:24:40 +02:00
}
user_lookup_fd = safe_close ( user_lookup_fd ) ;
2017-02-09 11:58:39 +01:00
r = acquire_home ( context , uid , & home , & home_buffer ) ;
if ( r < 0 ) {
* exit_status = EXIT_CHDIR ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to determine $HOME for user: %m " ) ;
2017-02-09 11:58:39 +01:00
}
2014-08-23 16:02:21 +02:00
/* If a socket is connected to STDIN/STDOUT/STDERR, we
* must sure to drop O_NONBLOCK */
if ( socket_fd > = 0 )
2015-10-07 23:07:39 +02:00
( void ) fd_nonblock ( socket_fd , false ) ;
2010-01-27 04:31:52 +01:00
2016-10-18 02:05:49 +02:00
r = setup_input ( context , params , socket_fd , named_iofds ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
* exit_status = EXIT_STDIN ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up standard input: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2010-01-26 04:18:44 +01:00
2016-10-18 02:05:49 +02:00
r = setup_output ( unit , context , params , STDOUT_FILENO , socket_fd , named_iofds , basename ( command - > path ) , uid , gid , & journal_stream_dev , & journal_stream_ino ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
* exit_status = EXIT_STDOUT ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up standard output: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2016-10-18 02:05:49 +02:00
r = setup_output ( unit , context , params , STDERR_FILENO , socket_fd , named_iofds , basename ( command - > path ) , uid , gid , & journal_stream_dev , & journal_stream_ino ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
* exit_status = EXIT_STDERR ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up standard error output: %m " ) ;
2014-08-23 16:02:21 +02:00
}
if ( params - > cgroup_path ) {
2015-01-09 00:13:33 +01:00
r = cg_attach_everywhere ( params - > cgroup_supported , params - > cgroup_path , 0 , NULL , NULL ) ;
if ( r < 0 ) {
* exit_status = EXIT_CGROUP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to attach to cgroup %s: %m " , params - > cgroup_path ) ;
2010-01-27 06:17:51 +01:00
}
2014-08-23 16:02:21 +02:00
}
2010-01-27 06:17:51 +01:00
2014-08-23 16:02:21 +02:00
if ( context - > oom_score_adjust_set ) {
2015-01-08 23:12:16 +01:00
char t [ DECIMAL_STR_MAX ( context - > oom_score_adjust ) ] ;
2012-04-24 14:28:00 +02:00
2015-01-08 23:12:16 +01:00
/* When we can't make this change due to EPERM, then
* let ' s silently skip over it . User namespaces
* prohibit write access to this file , and we
* shouldn ' t trip up over that . */
2013-11-27 20:23:18 +01:00
2015-01-08 23:12:16 +01:00
sprintf ( t , " %i " , context - > oom_score_adjust ) ;
2015-07-07 01:27:20 +02:00
r = write_string_file ( " /proc/self/oom_score_adj " , t , 0 ) ;
2017-09-26 17:47:27 +02:00
if ( IN_SET ( r , - EPERM , - EACCES ) )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_unit_debug_errno ( unit , r , " Failed to adjust OOM setting, assuming containerized execution, ignoring: %m " ) ;
2017-09-26 17:47:27 +02:00
else if ( r < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_OOM_ADJUST ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to adjust OOM setting: %m " ) ;
2013-11-27 20:23:18 +01:00
}
2014-08-23 16:02:21 +02:00
}
if ( context - > nice_set )
if ( setpriority ( PRIO_PROCESS , 0 , context - > nice ) < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_NICE ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to set up process scheduling priority (nice level) : % m " ) ;
2013-11-27 20:23:18 +01:00
}
2014-08-23 16:02:21 +02:00
if ( context - > cpu_sched_set ) {
struct sched_param param = {
. sched_priority = context - > cpu_sched_priority ,
} ;
2015-01-09 00:13:33 +01:00
r = sched_setscheduler ( 0 ,
context - > cpu_sched_policy |
( context - > cpu_sched_reset_on_fork ?
SCHED_RESET_ON_FORK : 0 ) ,
& param ) ;
if ( r < 0 ) {
* exit_status = EXIT_SETSCHEDULER ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to set up CPU scheduling: %m " ) ;
2010-07-12 20:34:53 +02:00
}
2014-08-23 16:02:21 +02:00
}
2010-07-12 20:34:53 +02:00
2014-08-23 16:02:21 +02:00
if ( context - > cpuset )
if ( sched_setaffinity ( 0 , CPU_ALLOC_SIZE ( context - > cpuset_ncpus ) , context - > cpuset ) < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_CPUAFFINITY ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to set up CPU affinity: %m " ) ;
2010-01-26 04:18:44 +01:00
}
2014-08-23 16:02:21 +02:00
if ( context - > ioprio_set )
if ( ioprio_set ( IOPRIO_WHO_PROCESS , 0 , context - > ioprio ) < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_IOPRIO ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to set up IO scheduling priority: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2010-08-30 23:31:27 +02:00
2014-08-23 16:02:21 +02:00
if ( context - > timer_slack_nsec ! = NSEC_INFINITY )
if ( prctl ( PR_SET_TIMERSLACK , context - > timer_slack_nsec ) < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_TIMERSLACK ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to set up timer slack: %m " ) ;
2011-11-17 00:21:16 +01:00
}
2010-01-29 20:46:22 +01:00
2017-09-08 16:16:29 +02:00
if ( context - > personality ! = PERSONALITY_INVALID ) {
r = safe_personality ( context - > personality ) ;
if ( r < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_PERSONALITY ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up execution domain (personality) : % m " ) ;
2011-11-17 00:21:16 +01:00
}
2017-09-08 16:16:29 +02:00
}
2010-01-30 01:55:42 +01:00
2014-08-23 16:02:21 +02:00
if ( context - > utmp_id )
2017-07-20 16:19:18 +02:00
utmp_put_init_process ( context - > utmp_id , getpid_cached ( ) , getsid ( 0 ) ,
2017-02-03 17:32:42 +01:00
context - > tty_path ,
2015-08-23 13:14:04 +02:00
context - > utmp_mode = = EXEC_UTMP_INIT ? INIT_PROCESS :
context - > utmp_mode = = EXEC_UTMP_LOGIN ? LOGIN_PROCESS :
USER_PROCESS ,
2017-02-03 17:32:42 +01:00
username ) ;
2014-08-23 16:02:21 +02:00
2016-10-11 20:07:22 +02:00
if ( context - > user ) {
2015-01-09 00:13:33 +01:00
r = chown_terminal ( STDIN_FILENO , uid ) ;
if ( r < 0 ) {
* exit_status = EXIT_STDIN ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to change ownership of terminal: %m " ) ;
2010-01-28 02:06:20 +01:00
}
2014-08-23 16:02:21 +02:00
}
2010-03-31 16:29:55 +02:00
2014-11-05 17:57:23 +01:00
/* If delegation is enabled we'll pass ownership of the cgroup
* ( but only in systemd ' s own controller hierarchy ! ) to the
* user of the new process . */
2017-08-01 10:51:18 +02:00
if ( params - > cgroup_path & & context - > user & & ( params - > flags & EXEC_CGROUP_DELEGATE ) ) {
2015-01-09 00:13:33 +01:00
r = cg_set_task_access ( SYSTEMD_CGROUP_CONTROLLER , params - > cgroup_path , 0644 , uid , gid ) ;
if ( r < 0 ) {
* exit_status = EXIT_CGROUP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to adjust control group access: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2010-01-26 04:18:44 +01:00
2015-01-09 00:13:33 +01:00
r = cg_set_group_access ( SYSTEMD_CGROUP_CONTROLLER , params - > cgroup_path , 0755 , uid , gid ) ;
if ( r < 0 ) {
* exit_status = EXIT_CGROUP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to adjust control group access: %m " ) ;
2010-01-26 04:18:44 +01:00
}
2014-08-23 16:02:21 +02:00
}
2010-01-26 04:18:44 +01:00
2017-09-28 16:58:43 +02:00
for ( dt = 0 ; dt < _EXEC_DIRECTORY_TYPE_MAX ; dt + + ) {
2017-08-01 10:35:10 +02:00
r = setup_exec_directory ( context , params , uid , gid , dt , exit_status ) ;
2017-09-26 17:47:27 +02:00
if ( r < 0 )
return log_unit_error_errno ( unit , r , " Failed to set up special execution directory in %s: %m " , params - > prefix [ dt ] ) ;
2014-08-23 16:02:21 +02:00
}
2010-01-30 01:55:42 +01:00
2016-06-14 16:50:45 +02:00
r = build_environment (
2016-08-02 12:28:51 +02:00
unit ,
2016-06-14 16:50:45 +02:00
context ,
params ,
n_fds ,
home ,
username ,
shell ,
journal_stream_dev ,
journal_stream_ino ,
& our_env ) ;
2016-06-13 12:50:12 +02:00
if ( r < 0 ) {
* exit_status = EXIT_MEMORY ;
2017-09-26 17:47:27 +02:00
return log_oom ( ) ;
2016-06-13 12:50:12 +02:00
}
r = build_pass_environment ( context , & pass_env ) ;
if ( r < 0 ) {
* exit_status = EXIT_MEMORY ;
2017-09-26 17:47:27 +02:00
return log_oom ( ) ;
2016-06-13 12:50:12 +02:00
}
accum_env = strv_env_merge ( 5 ,
params - > environment ,
our_env ,
pass_env ,
context - > environment ,
files_env ,
NULL ) ;
if ( ! accum_env ) {
* exit_status = EXIT_MEMORY ;
2017-09-26 17:47:27 +02:00
return log_oom ( ) ;
2016-06-13 12:50:12 +02:00
}
2016-07-07 12:36:33 +02:00
accum_env = strv_env_clean ( accum_env ) ;
2016-06-13 12:50:12 +02:00
2016-08-25 17:29:12 +02:00
( void ) umask ( context - > umask ) ;
2015-09-23 13:53:09 +02:00
2017-09-14 21:19:05 +02:00
r = setup_keyring ( unit , context , params , uid , gid ) ;
2016-12-02 01:54:41 +01:00
if ( r < 0 ) {
* exit_status = EXIT_KEYRING ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up kernel keyring: %m " ) ;
2016-12-02 01:54:41 +01:00
}
2017-08-09 16:09:04 +02:00
/* We need sandboxing if the caller asked us to apply it and the command isn't explicitly excepted from it */
core: rename EXEC_APPLY_PERMISSIONS → EXEC_APPLY_SANDBOXING
"Permissions" was a bit of a misnomer, as it suggests that UNIX file
permission bits are adjusted, which aren't really changed here. Instead,
this is about UNIX credentials such as users or groups, as well as
namespacing, hence let's use a more generic term here, without any
misleading reference to UNIX file permissions: "sandboxing", which shall
refer to all kinds of sandboxing technologies, including UID/GID
dropping, selinux relabelling, namespacing, seccomp, and so on.
2017-08-01 11:30:44 +02:00
needs_sandboxing = ( params - > flags & EXEC_APPLY_SANDBOXING ) & & ! ( command - > flags & EXEC_COMMAND_FULLY_PRIVILEGED ) ;
2017-08-01 09:15:18 +02:00
2017-08-09 16:09:04 +02:00
/* We need the ambient capability hack, if the caller asked us to apply it and the command is marked for it, and the kernel doesn't actually support ambient caps */
needs_ambient_hack = ( params - > flags & EXEC_APPLY_SANDBOXING ) & & ( command - > flags & EXEC_COMMAND_AMBIENT_MAGIC ) & & ! ambient_capabilities_supported ( ) ;
2017-08-01 09:15:18 +02:00
2017-08-09 16:09:04 +02:00
/* We need setresuid() if the caller asked us to apply sandboxing and the command isn't explicitly excepted from either whole sandboxing or just setresuid() itself, and the ambient hack is not desired */
if ( needs_ambient_hack )
needs_setuid = false ;
else
needs_setuid = ( params - > flags & EXEC_APPLY_SANDBOXING ) & & ! ( command - > flags & ( EXEC_COMMAND_FULLY_PRIVILEGED | EXEC_COMMAND_NO_SETUID ) ) ;
if ( needs_sandboxing ) {
2017-08-01 09:15:18 +02:00
/* MAC enablement checks need to be done before a new mount ns is created, as they rely on /sys being
* present . The actual MAC context application will happen later , as late as possible , to avoid
* impacting our own code paths . */
2017-10-03 10:41:51 +02:00
# if HAVE_SELINUX
2017-08-08 19:49:04 +02:00
use_selinux = mac_selinux_use ( ) ;
2017-08-01 09:15:18 +02:00
# endif
2017-10-03 12:22:40 +02:00
# if ENABLE_SMACK
2017-08-08 19:49:04 +02:00
use_smack = mac_smack_use ( ) ;
2017-08-01 09:15:18 +02:00
# endif
2017-10-03 10:41:51 +02:00
# if HAVE_APPARMOR
2017-08-08 19:49:04 +02:00
use_apparmor = mac_apparmor_use ( ) ;
2017-08-01 09:15:18 +02:00
# endif
2017-08-09 16:09:04 +02:00
}
2017-08-01 09:15:18 +02:00
2017-08-09 16:09:04 +02:00
if ( needs_setuid ) {
if ( context - > pam_name & & username ) {
r = setup_pam ( context - > pam_name , username , uid , gid , context - > tty_path , & accum_env , fds , n_fds ) ;
if ( r < 0 ) {
* exit_status = EXIT_PAM ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up PAM session: %m " ) ;
2017-08-09 16:09:04 +02:00
}
}
2015-09-23 13:53:09 +02:00
}
2014-02-19 02:15:24 +01:00
2014-08-23 16:02:21 +02:00
if ( context - > private_network & & runtime & & runtime - > netns_storage_socket [ 0 ] > = 0 ) {
2017-10-10 09:46:13 +02:00
if ( ns_type_supported ( NAMESPACE_NET ) ) {
r = setup_netns ( runtime - > netns_storage_socket ) ;
if ( r < 0 ) {
* exit_status = EXIT_NETWORK ;
return log_unit_error_errno ( unit , r , " Failed to set up network namespacing: %m " ) ;
}
} else
log_unit_warning ( unit , " PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring. " ) ;
2014-08-23 16:02:21 +02:00
}
2010-10-08 16:06:23 +02:00
2015-05-18 12:20:28 +02:00
needs_mount_namespace = exec_needs_mount_namespace ( context , params , runtime ) ;
if ( needs_mount_namespace ) {
core: skip ReadOnlyPaths= and other permission-related mounts on PermissionsStartOnly= (#5309)
ReadOnlyPaths=, ProtectHome=, InaccessiblePaths= and ProtectSystem= are
about restricting access and little more, hence they should be disabled
if PermissionsStartOnly= is used or ExecStart= lines are prefixed with a
"+". Do that.
(Note that we will still create namespaces and stuff, since that's about
a lot more than just permissions. We'll simply disable the effect of
the four options mentioned above, but nothing else mount related.)
This also adds a test for this, to ensure this works as intended.
No documentation updates, as the documentation are already vague enough
to support the new behaviour ("If true, the permission-related execution
options…"). We could clarify this further, but I think we might want to
extend the switches' behaviour a bit more in future, hence leave it at
this for now.
Fixes: #5308
2017-02-12 06:44:46 +01:00
r = apply_mount_namespace ( unit , command , context , params , runtime ) ;
2016-08-25 10:42:38 +02:00
if ( r < 0 ) {
* exit_status = EXIT_NAMESPACE ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up mount namespacing: %m " ) ;
2016-08-25 10:42:38 +02:00
}
2014-08-23 16:02:21 +02:00
}
2010-02-14 22:43:08 +01:00
2016-10-25 16:24:35 +02:00
/* Apply just after mount namespace setup */
2017-02-09 13:17:00 +01:00
r = apply_working_directory ( context , params , home , needs_mount_namespace , exit_status ) ;
2017-09-26 17:47:27 +02:00
if ( r < 0 )
return log_unit_error_errno ( unit , r , " Changing to the requested working directory failed: %m " ) ;
2016-10-25 16:24:35 +02:00
2016-11-02 17:51:35 +01:00
/* Drop groups as early as possbile */
2017-08-09 16:09:04 +02:00
if ( needs_setuid ) {
2017-10-12 08:10:25 +02:00
r = enforce_groups ( gid , supplementary_gids , ngids ) ;
2016-08-25 17:29:12 +02:00
if ( r < 0 ) {
* exit_status = EXIT_GROUP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Changing group credentials failed: %m " ) ;
2016-08-25 17:29:12 +02:00
}
2017-08-09 16:09:04 +02:00
}
2016-08-25 17:29:12 +02:00
2017-08-09 16:09:04 +02:00
if ( needs_sandboxing ) {
2017-10-03 10:41:51 +02:00
# if HAVE_SELINUX
2017-08-08 19:49:04 +02:00
if ( use_selinux & & params - > selinux_context_net & & socket_fd > = 0 ) {
2017-08-01 11:44:37 +02:00
r = mac_selinux_get_child_mls_label ( socket_fd , command - > path , context - > selinux_context , & mac_selinux_context_net ) ;
if ( r < 0 ) {
* exit_status = EXIT_SELINUX_CONTEXT ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to determine SELinux context: %m " ) ;
2017-08-01 11:44:37 +02:00
}
2014-11-12 13:53:27 +01:00
}
# endif
2017-08-01 11:44:37 +02:00
if ( context - > private_users ) {
r = setup_private_users ( uid , gid ) ;
if ( r < 0 ) {
* exit_status = EXIT_USER ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up user namespacing: %m " ) ;
2017-08-01 11:44:37 +02:00
}
2016-08-03 18:44:51 +02:00
}
}
2017-08-09 16:09:04 +02:00
/* We repeat the fd closing here, to make sure that nothing is leaked from the PAM modules. Note that we are
* more aggressive this time since socket_fd and the netns fds we don ' t need anymore . The custom endpoint fd
* was needed to upload the policy and can now be closed as well . */
2015-01-09 00:13:33 +01:00
r = close_all_fds ( fds , n_fds ) ;
if ( r > = 0 )
r = shift_fds ( fds , n_fds ) ;
if ( r > = 0 )
2017-06-08 15:41:26 +02:00
r = flags_fds ( fds , n_storage_fds , n_socket_fds , context - > non_blocking ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
* exit_status = EXIT_FDS ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to adjust passed file descriptors: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2014-03-03 17:14:07 +01:00
2017-08-09 16:09:04 +02:00
secure_bits = context - > secure_bits ;
2014-03-03 17:14:07 +01:00
2017-08-09 16:09:04 +02:00
if ( needs_sandboxing ) {
uint64_t bset ;
2015-12-31 13:54:44 +01:00
2014-08-23 16:02:21 +02:00
for ( i = 0 ; i < _RLIMIT_MAX ; i + + ) {
2016-06-23 01:31:24 +02:00
2014-08-23 16:02:21 +02:00
if ( ! context - > rlimit [ i ] )
continue ;
2016-06-23 01:31:24 +02:00
r = setrlimit_closest ( i , context - > rlimit [ i ] ) ;
if ( r < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_LIMITS ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to adjust resource limit %s: %m " , rlimit_to_string ( i ) ) ;
2014-03-03 17:14:07 +01:00
}
}
2016-06-23 01:45:45 +02:00
/* Set the RTPRIO resource limit to 0, but only if nothing else was explicitly requested. */
if ( context - > restrict_realtime & & ! context - > rlimit [ RLIMIT_RTPRIO ] ) {
if ( setrlimit ( RLIMIT_RTPRIO , & RLIMIT_MAKE_CONST ( 0 ) ) < 0 ) {
* exit_status = EXIT_LIMITS ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to adjust RLIMIT_RTPRIO resource limit: %m " ) ;
2016-06-23 01:45:45 +02:00
}
}
2017-08-09 16:09:04 +02:00
bset = context - > capability_bounding_set ;
/* If the ambient caps hack is enabled (which means the kernel can't do them, and the user asked for
* our magic fallback ) , then let ' s add some extra caps , so that the service can drop privs of its own ,
* instead of us doing that */
if ( needs_ambient_hack )
bset | = ( UINT64_C ( 1 ) < < CAP_SETPCAP ) |
( UINT64_C ( 1 ) < < CAP_SETUID ) |
( UINT64_C ( 1 ) < < CAP_SETGID ) ;
if ( ! cap_test_all ( bset ) ) {
r = capability_bounding_set_drop ( bset , false ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
* exit_status = EXIT_CAPABILITIES ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to drop capabilities: %m " ) ;
2011-06-30 02:15:01 +02:00
}
2011-11-17 00:21:16 +01:00
}
2011-06-30 02:15:01 +02:00
2015-12-31 13:54:44 +01:00
/* This is done before enforce_user, but ambient set
* does not survive over setresuid ( ) if keep_caps is not set . */
2017-08-09 16:09:04 +02:00
if ( ! needs_ambient_hack & &
context - > capability_ambient_set ! = 0 ) {
2015-12-31 13:54:44 +01:00
r = capability_ambient_set_apply ( context - > capability_ambient_set , true ) ;
if ( r < 0 ) {
* exit_status = EXIT_CAPABILITIES ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply ambient capabilities (before UID change) : % m " ) ;
2015-12-31 13:54:44 +01:00
}
}
2017-08-09 16:09:04 +02:00
}
2015-12-31 13:54:44 +01:00
2017-08-09 16:09:04 +02:00
if ( needs_setuid ) {
2014-08-23 16:02:21 +02:00
if ( context - > user ) {
2015-01-09 00:13:33 +01:00
r = enforce_user ( context , uid ) ;
if ( r < 0 ) {
* exit_status = EXIT_USER ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to change UID to " UID_FMT " : %m " , uid ) ;
2010-06-16 21:54:17 +02:00
}
2017-08-09 16:09:04 +02:00
if ( ! needs_ambient_hack & &
context - > capability_ambient_set ! = 0 ) {
2015-12-31 13:54:44 +01:00
/* Fix the ambient capabilities after user change. */
r = capability_ambient_set_apply ( context - > capability_ambient_set , false ) ;
if ( r < 0 ) {
* exit_status = EXIT_CAPABILITIES ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply ambient capabilities (after UID change) : % m " ) ;
2015-12-31 13:54:44 +01:00
}
/* If we were asked to change user and ambient capabilities
* were requested , we had to add keep - caps to the securebits
* so that we would maintain the inherited capability set
* through the setresuid ( ) . Make sure that the bit is added
* also to the context secure_bits so that we don ' t try to
* drop the bit away next . */
2016-02-25 00:27:56 +01:00
secure_bits | = 1 < < SECURE_KEEP_CAPS ;
2015-12-31 13:54:44 +01:00
}
2010-06-16 21:54:17 +02:00
}
2017-08-09 16:09:04 +02:00
}
2014-08-23 16:02:21 +02:00
2017-08-09 16:09:04 +02:00
if ( needs_sandboxing ) {
2016-10-25 15:52:54 +02:00
/* Apply the MAC contexts late, but before seccomp syscall filtering, as those should really be last to
* influence our own codepaths as little as possible . Moreover , applying MAC contexts usually requires
* syscalls that are subject to seccomp filtering , hence should probably be applied before the syscalls
* are restricted . */
2017-10-03 10:41:51 +02:00
# if HAVE_SELINUX
2017-08-08 19:49:04 +02:00
if ( use_selinux ) {
2016-10-25 15:52:54 +02:00
char * exec_context = mac_selinux_context_net ? : context - > selinux_context ;
if ( exec_context ) {
r = setexeccon ( exec_context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SELINUX_CONTEXT ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to change SELinux context to %s: %m " , exec_context ) ;
2016-10-25 15:52:54 +02:00
}
}
}
# endif
2017-10-03 12:22:40 +02:00
# if ENABLE_SMACK
2017-08-08 19:49:04 +02:00
if ( use_smack ) {
2017-08-01 09:15:18 +02:00
r = setup_smack ( context , command ) ;
if ( r < 0 ) {
* exit_status = EXIT_SMACK_PROCESS_LABEL ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set SMACK process label: %m " ) ;
2017-08-01 09:15:18 +02:00
}
2016-10-25 15:52:54 +02:00
}
2017-08-01 09:15:18 +02:00
# endif
2016-10-25 15:52:54 +02:00
2017-10-03 10:41:51 +02:00
# if HAVE_APPARMOR
2017-08-08 19:49:04 +02:00
if ( use_apparmor & & context - > apparmor_profile ) {
2016-10-25 15:52:54 +02:00
r = aa_change_onexec ( context - > apparmor_profile ) ;
if ( r < 0 & & ! context - > apparmor_profile_ignore ) {
* exit_status = EXIT_APPARMOR_PROFILE ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to prepare AppArmor profile change to %s: %m " , context - > apparmor_profile ) ;
2016-10-25 15:52:54 +02:00
}
}
# endif
2017-08-09 16:09:04 +02:00
/* PR_GET_SECUREBITS is not privileged, while PR_SET_SECUREBITS is. So to suppress potential EPERMs
* we ' ll try not to call PR_SET_SECUREBITS unless necessary . */
2015-12-31 13:54:44 +01:00
if ( prctl ( PR_GET_SECUREBITS ) ! = secure_bits )
if ( prctl ( PR_SET_SECUREBITS , secure_bits ) < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_SECUREBITS ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to set process secure bits: %m " ) ;
2011-08-02 05:24:58 +02:00
}
2010-06-16 21:54:17 +02:00
2016-08-22 18:43:59 +02:00
if ( context_has_no_new_privileges ( context ) )
2014-08-23 16:02:21 +02:00
if ( prctl ( PR_SET_NO_NEW_PRIVS , 1 , 0 , 0 , 0 ) < 0 ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_NO_NEW_PRIVILEGES ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to disable new privileges: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
r = apply_address_families ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_ADDRESS_FAMILIES ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to restrict address families: %m " ) ;
2011-11-17 00:21:16 +01:00
}
2010-06-16 16:39:28 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
r = apply_memory_deny_write_execute ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to disable writing to executable memory: %m " ) ;
2016-06-03 17:58:18 +02:00
}
2016-06-23 01:45:45 +02:00
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
r = apply_restrict_realtime ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply realtime restrictions: %m " ) ;
2016-06-23 01:45:45 +02:00
}
2016-11-02 03:25:19 +01:00
r = apply_restrict_namespaces ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply namespace restrictions: %m " ) ;
2016-11-02 03:25:19 +01:00
}
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
r = apply_protect_sysctl ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply sysctl restrictions: %m " ) ;
2016-10-12 13:31:21 +02:00
}
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
r = apply_protect_kernel_modules ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply module loading restrictions: %m " ) ;
2016-08-22 18:43:59 +02:00
}
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
r = apply_private_devices ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to set up private devices: %m " ) ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
}
r = apply_syscall_archs ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply syscall architecture restrictions: %m " ) ;
2016-08-26 16:39:04 +02:00
}
2017-07-04 14:48:18 +02:00
r = apply_lock_personality ( unit , context ) ;
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to lock personalities: %m " ) ;
2017-07-04 14:48:18 +02:00
}
2016-10-25 15:52:54 +02:00
/* This really should remain the last step before the execve(), to make sure our own code is unaffected
* by the filter as little as possible . */
2017-08-09 16:09:04 +02:00
r = apply_syscall_filter ( unit , context , needs_ambient_hack ) ;
seccomp: rework seccomp code, to improve compat with some archs
This substantially reworks the seccomp code, to ensure better
compatibility with some architectures, including i386.
So far we relied on libseccomp's internal handling of the multiple
syscall ABIs supported on Linux. This is problematic however, as it does
not define clear semantics if an ABI is not able to support specific
seccomp rules we install.
This rework hence changes a couple of things:
- We no longer use seccomp_rule_add(), but only
seccomp_rule_add_exact(), and fail the installation of a filter if the
architecture doesn't support it.
- We no longer rely on adding multiple syscall architectures to a single filter,
but instead install a separate filter for each syscall architecture
supported. This way, we can install a strict filter for x86-64, while
permitting a less strict filter for i386.
- All high-level filter additions are now moved from execute.c to
seccomp-util.c, so that we can test them independently of the service
execution logic.
- Tests have been added for all types of our seccomp filters.
- SystemCallFilters= and SystemCallArchitectures= are now implemented in
independent filters and installation logic, as they semantically are
very much independent of each other.
Fixes: #4575
2016-12-27 15:28:25 +01:00
if ( r < 0 ) {
* exit_status = EXIT_SECCOMP ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , r , " Failed to apply system call filters: %m " ) ;
2014-08-23 16:02:21 +02:00
}
# endif
}
2010-01-26 04:18:44 +01:00
2017-09-10 12:16:44 +02:00
if ( ! strv_isempty ( context - > unset_environment ) ) {
char * * ee = NULL ;
ee = strv_env_delete ( accum_env , 1 , context - > unset_environment ) ;
if ( ! ee ) {
* exit_status = EXIT_MEMORY ;
2017-09-26 17:47:27 +02:00
return log_oom ( ) ;
2017-09-10 12:16:44 +02:00
}
strv_free ( accum_env ) ;
accum_env = ee ;
}
2016-06-13 12:50:12 +02:00
final_argv = replace_env_argv ( argv , accum_env ) ;
2014-08-23 16:02:21 +02:00
if ( ! final_argv ) {
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_MEMORY ;
2017-09-26 17:47:27 +02:00
return log_oom ( ) ;
2014-08-23 16:02:21 +02:00
}
2010-01-26 04:18:44 +01:00
2015-01-06 06:29:40 +01:00
if ( _unlikely_ ( log_get_max_level ( ) > = LOG_DEBUG ) ) {
2014-08-23 16:02:21 +02:00
_cleanup_free_ char * line ;
2010-02-14 22:43:08 +01:00
2014-08-23 16:02:21 +02:00
line = exec_command_line ( final_argv ) ;
if ( line ) {
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_struct ( LOG_DEBUG ,
" EXECUTABLE=%s " , command - > path ,
LOG_UNIT_MESSAGE ( unit , " Executing: %s " , line ) ,
2017-04-20 20:15:28 +02:00
LOG_UNIT_ID ( unit ) ,
2017-09-20 18:27:53 +02:00
LOG_UNIT_INVOCATION_ID ( unit ) ,
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
NULL ) ;
2014-08-23 16:02:21 +02:00
}
}
2015-04-28 12:20:29 +02:00
2016-06-13 12:50:12 +02:00
execve ( command - > path , final_argv , accum_env ) ;
2017-09-26 17:47:27 +02:00
if ( errno = = ENOENT & & ( command - > flags & EXEC_COMMAND_IGNORE_FAILURE ) ) {
log_struct_errno ( LOG_INFO , errno ,
" MESSAGE_ID= " SD_MESSAGE_SPAWN_FAILED_STR ,
LOG_UNIT_ID ( unit ) ,
LOG_UNIT_INVOCATION_ID ( unit ) ,
LOG_UNIT_MESSAGE ( unit , " Executable %s missing, skipping: %m " ,
command - > path ) ,
" EXECUTABLE=%s " , command - > path ,
NULL ) ;
return 0 ;
}
2015-01-09 00:13:33 +01:00
* exit_status = EXIT_EXEC ;
2017-09-26 17:47:27 +02:00
return log_unit_error_errno ( unit , errno , " Failed to execute command: %m " ) ;
2014-08-23 16:02:21 +02:00
}
2010-02-14 22:43:08 +01:00
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
int exec_spawn ( Unit * unit ,
ExecCommand * command ,
2014-08-23 16:02:21 +02:00
const ExecContext * context ,
const ExecParameters * params ,
ExecRuntime * runtime ,
2016-07-14 12:37:28 +02:00
DynamicCreds * dcreds ,
2014-08-23 16:02:21 +02:00
pid_t * ret ) {
2012-07-17 04:17:53 +02:00
2014-08-23 16:02:21 +02:00
_cleanup_strv_free_ char * * files_env = NULL ;
2017-05-12 11:32:53 +02:00
int * fds = NULL ;
2017-06-08 15:41:26 +02:00
unsigned n_storage_fds = 0 , n_socket_fds = 0 ;
2015-01-09 00:13:33 +01:00
_cleanup_free_ char * line = NULL ;
int socket_fd , r ;
2016-10-18 02:05:49 +02:00
int named_iofds [ 3 ] = { - 1 , - 1 , - 1 } ;
2015-01-09 00:13:33 +01:00
char * * argv ;
2014-08-23 16:02:21 +02:00
pid_t pid ;
2012-07-17 04:17:53 +02:00
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
assert ( unit ) ;
2014-08-23 16:02:21 +02:00
assert ( command ) ;
assert ( context ) ;
assert ( ret ) ;
assert ( params ) ;
2017-06-08 15:41:26 +02:00
assert ( params - > fds | | ( params - > n_storage_fds + params - > n_socket_fds < = 0 ) ) ;
2014-02-25 20:37:03 +01:00
2014-08-23 16:02:21 +02:00
if ( context - > std_input = = EXEC_INPUT_SOCKET | |
context - > std_output = = EXEC_OUTPUT_SOCKET | |
context - > std_error = = EXEC_OUTPUT_SOCKET ) {
2014-02-12 18:28:21 +01:00
2017-06-08 15:41:26 +02:00
if ( params - > n_socket_fds > 1 ) {
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_unit_error ( unit , " Got more than one socket. " ) ;
2014-08-23 16:02:21 +02:00
return - EINVAL ;
2015-01-09 00:13:33 +01:00
}
2014-02-20 16:19:44 +01:00
2017-06-08 15:41:26 +02:00
if ( params - > n_socket_fds = = 0 ) {
2017-05-09 01:09:22 +02:00
log_unit_error ( unit , " Got no socket. " ) ;
return - EINVAL ;
}
2014-08-23 16:02:21 +02:00
socket_fd = params - > fds [ 0 ] ;
} else {
socket_fd = - 1 ;
fds = params - > fds ;
2017-06-08 15:41:26 +02:00
n_storage_fds = params - > n_storage_fds ;
2017-05-12 11:32:53 +02:00
n_socket_fds = params - > n_socket_fds ;
2014-08-23 16:02:21 +02:00
}
2010-01-30 01:55:42 +01:00
2016-10-18 02:05:49 +02:00
r = exec_context_named_iofds ( unit , context , params , named_iofds ) ;
if ( r < 0 )
return log_unit_error_errno ( unit , r , " Failed to load a named file descriptor: %m " ) ;
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
r = exec_context_load_environment ( unit , context , & files_env ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
return log_unit_error_errno ( unit , r , " Failed to load environment files: %m " ) ;
2010-01-26 04:18:44 +01:00
2014-08-23 16:02:21 +02:00
argv = params - > argv ? : command - > argv ;
line = exec_command_line ( argv ) ;
if ( ! line )
return log_oom ( ) ;
2010-07-08 04:09:59 +02:00
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_struct ( LOG_DEBUG ,
LOG_UNIT_MESSAGE ( unit , " About to execute: %s " , line ) ,
" EXECUTABLE=%s " , command - > path ,
2017-04-20 20:15:28 +02:00
LOG_UNIT_ID ( unit ) ,
2017-09-20 18:27:53 +02:00
LOG_UNIT_INVOCATION_ID ( unit ) ,
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
NULL ) ;
2017-09-26 17:47:27 +02:00
2014-08-23 16:02:21 +02:00
pid = fork ( ) ;
if ( pid < 0 )
2015-11-26 04:46:40 +01:00
return log_unit_error_errno ( unit , errno , " Failed to fork: %m " ) ;
2014-08-23 16:02:21 +02:00
if ( pid = = 0 ) {
2017-09-26 17:47:27 +02:00
int exit_status = EXIT_SUCCESS ;
2015-01-09 00:13:33 +01:00
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
r = exec_child ( unit ,
command ,
2015-01-09 00:13:33 +01:00
context ,
params ,
runtime ,
2016-07-14 12:37:28 +02:00
dcreds ,
2015-01-09 00:13:33 +01:00
argv ,
socket_fd ,
2016-10-18 02:05:49 +02:00
named_iofds ,
2017-06-08 15:41:26 +02:00
fds ,
n_storage_fds ,
2017-05-12 11:32:53 +02:00
n_socket_fds ,
2015-01-09 00:13:33 +01:00
files_env ,
2016-08-01 19:24:40 +02:00
unit - > manager - > user_lookup_fds [ 1 ] ,
2017-09-26 17:47:27 +02:00
& exit_status ) ;
2015-01-09 00:13:33 +01:00
if ( r < 0 ) {
2017-09-26 17:47:27 +02:00
log_struct_errno ( LOG_ERR , r ,
" MESSAGE_ID= " SD_MESSAGE_SPAWN_FAILED_STR ,
LOG_UNIT_ID ( unit ) ,
LOG_UNIT_INVOCATION_ID ( unit ) ,
LOG_UNIT_MESSAGE ( unit , " Failed at step %s spawning %s: %m " ,
exit_status_to_string ( exit_status , EXIT_STATUS_SYSTEMD ) ,
command - > path ) ,
" EXECUTABLE=%s " , command - > path ,
NULL ) ;
2011-11-17 00:21:16 +01:00
}
2015-01-09 00:13:33 +01:00
_exit ( exit_status ) ;
2010-01-26 04:18:44 +01:00
}
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_unit_debug ( unit , " Forked %s as " PID_FMT , command - > path , pid ) ;
2012-10-11 00:11:24 +02:00
2010-04-13 02:06:27 +02:00
/* We add the new process to the cgroup both in the child (so
* that we can be sure that no user code is ever executed
* outside of the cgroup ) and in the parent ( so that we can be
* sure that when we kill the cgroup the process will be
* killed too ) . */
2014-08-23 16:02:21 +02:00
if ( params - > cgroup_path )
2015-04-28 12:20:29 +02:00
( void ) cg_attach ( SYSTEMD_CGROUP_CONTROLLER , params - > cgroup_path , pid ) ;
2010-01-27 05:30:58 +01:00
2010-07-04 18:49:58 +02:00
exec_status_start ( & command - > exec_status , pid ) ;
2010-04-10 05:03:14 +02:00
2010-01-26 04:18:44 +01:00
* ret = pid ;
2010-01-23 01:52:57 +01:00
return 0 ;
}
2010-01-26 04:18:44 +01:00
void exec_context_init ( ExecContext * c ) {
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
ExecDirectoryType i ;
2010-01-26 04:18:44 +01:00
assert ( c ) ;
2011-08-01 20:52:18 +02:00
c - > umask = 0022 ;
2010-01-29 20:46:22 +01:00
c - > ioprio = IOPRIO_PRIO_VALUE ( IOPRIO_CLASS_BE , 0 ) ;
2010-01-30 01:55:42 +01:00
c - > cpu_sched_policy = SCHED_OTHER ;
2010-01-28 02:06:20 +01:00
c - > syslog_priority = LOG_DAEMON | LOG_INFO ;
2010-07-05 01:08:13 +02:00
c - > syslog_level_prefix = true ;
2012-02-09 03:18:04 +01:00
c - > ignore_sigpipe = true ;
2014-07-29 12:23:31 +02:00
c - > timer_slack_nsec = NSEC_INFINITY ;
2015-05-21 19:48:49 +02:00
c - > personality = PERSONALITY_INVALID ;
2017-09-28 16:58:43 +02:00
for ( i = 0 ; i < _EXEC_DIRECTORY_TYPE_MAX ; i + + )
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
c - > directories [ i ] . mode = 0755 ;
2016-01-07 23:00:04 +01:00
c - > capability_bounding_set = CAP_ALL ;
2016-11-02 03:25:19 +01:00
c - > restrict_namespaces = NAMESPACE_FLAGS_ALL ;
2010-01-26 04:18:44 +01:00
}
2013-11-27 20:23:18 +01:00
void exec_context_done ( ExecContext * c ) {
2010-01-23 01:52:57 +01:00
unsigned l ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
ExecDirectoryType i ;
2010-01-23 01:52:57 +01:00
assert ( c ) ;
2015-09-09 23:05:10 +02:00
c - > environment = strv_free ( c - > environment ) ;
c - > environment_files = strv_free ( c - > environment_files ) ;
2015-09-07 08:06:53 +02:00
c - > pass_environment = strv_free ( c - > pass_environment ) ;
2017-09-10 12:16:44 +02:00
c - > unset_environment = strv_free ( c - > unset_environment ) ;
2011-03-04 03:44:43 +01:00
2015-09-09 14:23:02 +02:00
for ( l = 0 ; l < ELEMENTSOF ( c - > rlimit ) ; l + + )
2015-09-08 18:43:11 +02:00
c - > rlimit [ l ] = mfree ( c - > rlimit [ l ] ) ;
2010-01-26 04:18:44 +01:00
2016-10-18 02:05:49 +02:00
for ( l = 0 ; l < 3 ; l + + )
c - > stdio_fdname [ l ] = mfree ( c - > stdio_fdname [ l ] ) ;
2015-09-08 18:43:11 +02:00
c - > working_directory = mfree ( c - > working_directory ) ;
c - > root_directory = mfree ( c - > root_directory ) ;
2016-12-23 14:26:05 +01:00
c - > root_image = mfree ( c - > root_image ) ;
2015-09-08 18:43:11 +02:00
c - > tty_path = mfree ( c - > tty_path ) ;
c - > syslog_identifier = mfree ( c - > syslog_identifier ) ;
c - > user = mfree ( c - > user ) ;
c - > group = mfree ( c - > group ) ;
2010-01-26 04:18:44 +01:00
2015-09-09 23:05:10 +02:00
c - > supplementary_groups = strv_free ( c - > supplementary_groups ) ;
2010-01-30 01:55:42 +01:00
2015-09-08 18:43:11 +02:00
c - > pam_name = mfree ( c - > pam_name ) ;
2010-06-16 21:54:17 +02:00
2016-07-07 11:17:00 +02:00
c - > read_only_paths = strv_free ( c - > read_only_paths ) ;
c - > read_write_paths = strv_free ( c - > read_write_paths ) ;
c - > inaccessible_paths = strv_free ( c - > inaccessible_paths ) ;
2010-07-04 16:44:58 +02:00
2016-11-23 22:21:40 +01:00
bind_mount_free_many ( c - > bind_mounts , c - > n_bind_mounts ) ;
2010-07-04 16:44:58 +02:00
if ( c - > cpuset )
CPU_FREE ( c - > cpuset ) ;
2011-01-06 23:52:17 +01:00
2015-09-08 18:43:11 +02:00
c - > utmp_id = mfree ( c - > utmp_id ) ;
c - > selinux_context = mfree ( c - > selinux_context ) ;
c - > apparmor_profile = mfree ( c - > apparmor_profile ) ;
2017-07-13 06:06:34 +02:00
c - > smack_process_label = mfree ( c - > smack_process_label ) ;
2014-02-20 16:19:44 +01:00
2015-09-09 23:12:07 +02:00
c - > syscall_filter = set_free ( c - > syscall_filter ) ;
c - > syscall_archs = set_free ( c - > syscall_archs ) ;
c - > address_families = set_free ( c - > address_families ) ;
2014-03-03 17:14:07 +01:00
2017-09-28 16:58:43 +02:00
for ( i = 0 ; i < _EXEC_DIRECTORY_TYPE_MAX ; i + + )
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
c - > directories [ i ] . paths = strv_free ( c - > directories [ i ] . paths ) ;
2014-03-03 17:14:07 +01:00
}
int exec_context_destroy_runtime_directory ( ExecContext * c , const char * runtime_prefix ) {
char * * i ;
assert ( c ) ;
if ( ! runtime_prefix )
return 0 ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
STRV_FOREACH ( i , c - > directories [ EXEC_DIRECTORY_RUNTIME ] . paths ) {
2014-03-03 17:14:07 +01:00
_cleanup_free_ char * p ;
2016-10-23 17:43:27 +02:00
p = strjoin ( runtime_prefix , " / " , * i ) ;
2014-03-03 17:14:07 +01:00
if ( ! p )
return - ENOMEM ;
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
/* We execute this synchronously, since we need to be sure this is gone when we start the service
2014-03-03 17:14:07 +01:00
* next . */
2015-04-04 11:52:57 +02:00
( void ) rm_rf ( p , REMOVE_ROOT ) ;
execute: make StateDirectory= and friends compatible with DynamicUser=1 and RootDirectory=/RootImage=
Let's clean up the interaction of StateDirectory= (and friends) to
DynamicUser=1: instead of creating these directories directly below
/var/lib, place them in /var/lib/private instead if DynamicUser=1 is
set, making that directory 0700 and owned by root:root. This way, if a
dynamic UID is later reused, access to the old run's state directory is
prohibited for that user. Then, use file system namespacing inside the
service to make /var/lib/private a readable tmpfs, hiding all state
directories that are not listed in StateDirectory=, and making access to
the actual state directory possible. Mount all directories listed in
StateDirectory= to the same places inside the service (which means
they'll now be mounted into the tmpfs instance). Finally, add a symlink
from the state directory name in /var/lib/ to the one in
/var/lib/private, so that both the host and the service can access the
path under the same location.
Here's an example: let's say a service runs with StateDirectory=foo.
When DynamicUser=0 is set, it will get the following setup, and no
difference between what the unit and what the host sees:
/var/lib/foo (created as directory)
Now, if DynamicUser=1 is set, we'll instead get this on the host:
/var/lib/private (created as directory with mode 0700, root:root)
/var/lib/private/foo (created as directory)
/var/lib/foo → private/foo (created as symlink)
And from inside the unit:
/var/lib/private (a tmpfs mount with mode 0755, root:root)
/var/lib/private/foo (bind mounted from the host)
/var/lib/foo → private/foo (the same symlink as above)
This takes inspiration from how container trees are protected below
/var/lib/machines: they generally reuse UIDs/GIDs of the host, but
because /var/lib/machines itself is set to 0700 host users cannot access
files in the container tree even if the UIDs/GIDs are reused. However,
for this commit we add one further trick: inside and outside of the unit
/var/lib/private is a different thing: outside it is a plain,
inaccessible directory, and inside it is a world-readable tmpfs mount
with only the whitelisted subdirs below it, bind mounte din. This
means, from the outside the dir acts as an access barrier, but from the
inside it does not. And the symlink created in /var/lib/foo itself
points across the barrier in both cases, so that root and the unit's
user always have access to these dirs without knowing the details of
this mounting magic.
This logic resolves a major shortcoming of DynamicUser=1 units:
previously they couldn't safely store persistant data. With this change
they can have their own private state, log and data directories, which
they can write to, but which are protected from UID recycling.
With this change, if RootDirectory= or RootImage= are used it is ensured
that the specified state/log/cache directories are always mounted in
from the host. This change of semantics I think is much preferable since
this means the root directory/image logic can be used easily for
read-only resource bundling (as all writable data resides outside of the
image). Note that this is a change of behaviour, but given that we
haven't released any systemd version with StateDirectory= and friends
implemented this should be a safe change to make (in particular as
previously it wasn't clear what would actually happen when used in
combination). Moreover, by making this change we can later add a "+"
modifier to these setings too working similar to the same modifier in
ReadOnlyPaths= and friends, making specified paths relative to the
container itself.
2017-09-28 18:55:45 +02:00
/* Also destroy any matching subdirectory below /private/. This is done to support DynamicUser=1
* setups . Note that we don ' t conditionalize here on that though , as the namespace is same way , and it
* makes us a bit more robust towards changing unit settings . Or to say this differently : in the worst
* case this is a NOP . */
free ( p ) ;
p = strjoin ( runtime_prefix , " /private/ " , * i ) ;
if ( ! p )
return - ENOMEM ;
( void ) rm_rf ( p , REMOVE_ROOT ) ;
2014-03-03 17:14:07 +01:00
}
return 0 ;
2010-01-23 01:52:57 +01:00
}
2010-04-10 17:47:07 +02:00
void exec_command_done ( ExecCommand * c ) {
assert ( c ) ;
2015-09-08 18:43:11 +02:00
c - > path = mfree ( c - > path ) ;
2010-04-10 17:47:07 +02:00
2015-09-09 23:05:10 +02:00
c - > argv = strv_free ( c - > argv ) ;
2010-04-10 17:47:07 +02:00
}
void exec_command_done_array ( ExecCommand * c , unsigned n ) {
unsigned i ;
for ( i = 0 ; i < n ; i + + )
exec_command_done ( c + i ) ;
}
2014-12-18 18:29:24 +01:00
ExecCommand * exec_command_free_list ( ExecCommand * c ) {
2010-01-23 01:52:57 +01:00
ExecCommand * i ;
while ( ( i = c ) ) {
2013-10-14 06:10:14 +02:00
LIST_REMOVE ( command , c , i ) ;
2010-04-10 17:47:07 +02:00
exec_command_done ( i ) ;
2010-01-23 01:52:57 +01:00
free ( i ) ;
}
2014-12-18 18:29:24 +01:00
return NULL ;
2010-01-23 01:52:57 +01:00
}
2010-01-26 04:18:44 +01:00
void exec_command_free_array ( ExecCommand * * c , unsigned n ) {
unsigned i ;
2014-12-18 18:29:24 +01:00
for ( i = 0 ; i < n ; i + + )
c [ i ] = exec_command_free_list ( c [ i ] ) ;
2010-01-26 04:18:44 +01:00
}
2014-12-23 19:04:56 +01:00
typedef struct InvalidEnvInfo {
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
Unit * unit ;
2014-12-23 19:04:56 +01:00
const char * path ;
} InvalidEnvInfo ;
static void invalid_env ( const char * p , void * userdata ) {
InvalidEnvInfo * info = userdata ;
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_unit_error ( info - > unit , " Ignoring invalid environment assignment '%s': %s " , p , info - > path ) ;
2014-12-23 19:04:56 +01:00
}
2016-10-18 02:05:49 +02:00
const char * exec_context_fdname ( const ExecContext * c , int fd_index ) {
assert ( c ) ;
switch ( fd_index ) {
case STDIN_FILENO :
if ( c - > std_input ! = EXEC_INPUT_NAMED_FD )
return NULL ;
return c - > stdio_fdname [ STDIN_FILENO ] ? : " stdin " ;
case STDOUT_FILENO :
if ( c - > std_output ! = EXEC_OUTPUT_NAMED_FD )
return NULL ;
return c - > stdio_fdname [ STDOUT_FILENO ] ? : " stdout " ;
case STDERR_FILENO :
if ( c - > std_error ! = EXEC_OUTPUT_NAMED_FD )
return NULL ;
return c - > stdio_fdname [ STDERR_FILENO ] ? : " stderr " ;
default :
return NULL ;
}
}
int exec_context_named_iofds ( Unit * unit , const ExecContext * c , const ExecParameters * p , int named_iofds [ 3 ] ) {
unsigned i , targets ;
2017-01-31 17:23:10 +01:00
const char * stdio_fdname [ 3 ] ;
2017-06-08 15:41:26 +02:00
unsigned n_fds ;
2016-10-18 02:05:49 +02:00
assert ( c ) ;
assert ( p ) ;
targets = ( c - > std_input = = EXEC_INPUT_NAMED_FD ) +
( c - > std_output = = EXEC_OUTPUT_NAMED_FD ) +
( c - > std_error = = EXEC_OUTPUT_NAMED_FD ) ;
for ( i = 0 ; i < 3 ; i + + )
stdio_fdname [ i ] = exec_context_fdname ( c , i ) ;
2017-06-08 15:41:26 +02:00
n_fds = p - > n_storage_fds + p - > n_socket_fds ;
for ( i = 0 ; i < n_fds & & targets > 0 ; i + + )
2017-01-31 17:23:10 +01:00
if ( named_iofds [ STDIN_FILENO ] < 0 & &
c - > std_input = = EXEC_INPUT_NAMED_FD & &
stdio_fdname [ STDIN_FILENO ] & &
streq ( p - > fd_names [ i ] , stdio_fdname [ STDIN_FILENO ] ) ) {
2016-10-18 02:05:49 +02:00
named_iofds [ STDIN_FILENO ] = p - > fds [ i ] ;
targets - - ;
2017-01-31 17:23:10 +01:00
} else if ( named_iofds [ STDOUT_FILENO ] < 0 & &
c - > std_output = = EXEC_OUTPUT_NAMED_FD & &
stdio_fdname [ STDOUT_FILENO ] & &
streq ( p - > fd_names [ i ] , stdio_fdname [ STDOUT_FILENO ] ) ) {
2016-10-18 02:05:49 +02:00
named_iofds [ STDOUT_FILENO ] = p - > fds [ i ] ;
targets - - ;
2017-01-31 17:23:10 +01:00
} else if ( named_iofds [ STDERR_FILENO ] < 0 & &
c - > std_error = = EXEC_OUTPUT_NAMED_FD & &
stdio_fdname [ STDERR_FILENO ] & &
streq ( p - > fd_names [ i ] , stdio_fdname [ STDERR_FILENO ] ) ) {
2016-10-18 02:05:49 +02:00
named_iofds [ STDERR_FILENO ] = p - > fds [ i ] ;
targets - - ;
}
2017-01-31 17:23:10 +01:00
return targets = = 0 ? 0 : - ENOENT ;
2016-10-18 02:05:49 +02:00
}
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
int exec_context_load_environment ( Unit * unit , const ExecContext * c , char * * * l ) {
2011-03-04 03:44:43 +01:00
char * * i , * * r = NULL ;
assert ( c ) ;
assert ( l ) ;
STRV_FOREACH ( i , c - > environment_files ) {
char * fn ;
2017-05-11 08:15:28 +02:00
int k ;
unsigned n ;
2011-03-04 03:44:43 +01:00
bool ignore = false ;
char * * p ;
2013-04-18 09:11:22 +02:00
_cleanup_globfree_ glob_t pglob = { } ;
2011-03-04 03:44:43 +01:00
fn = * i ;
if ( fn [ 0 ] = = ' - ' ) {
ignore = true ;
2016-02-23 05:32:04 +01:00
fn + + ;
2011-03-04 03:44:43 +01:00
}
if ( ! path_is_absolute ( fn ) ) {
if ( ignore )
continue ;
strv_free ( r ) ;
return - EINVAL ;
}
2013-01-02 12:41:52 +01:00
/* Filename supports globbing, take all matching files */
2017-04-26 04:54:50 +02:00
k = safe_glob ( fn , 0 , & pglob ) ;
if ( k < 0 ) {
2013-01-02 12:41:52 +01:00
if ( ignore )
continue ;
2011-03-04 03:44:43 +01:00
2013-01-02 12:41:52 +01:00
strv_free ( r ) ;
2017-04-26 04:54:50 +02:00
return k ;
2013-01-02 12:41:52 +01:00
}
2011-03-04 03:44:43 +01:00
2017-04-26 04:54:50 +02:00
/* When we don't match anything, -ENOENT should be returned */
assert ( pglob . gl_pathc > 0 ) ;
for ( n = 0 ; n < pglob . gl_pathc ; n + + ) {
2014-07-03 17:50:55 +02:00
k = load_env_file ( NULL , pglob . gl_pathv [ n ] , NULL , & p ) ;
2013-01-02 12:41:52 +01:00
if ( k < 0 ) {
if ( ignore )
continue ;
2011-03-04 03:44:43 +01:00
2013-01-02 12:41:52 +01:00
strv_free ( r ) ;
return k ;
2013-07-02 13:24:48 +02:00
}
2013-04-17 15:25:02 +02:00
/* Log invalid environment variables with filename */
2014-12-23 19:04:56 +01:00
if ( p ) {
InvalidEnvInfo info = {
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
. unit = unit ,
2014-12-23 19:04:56 +01:00
. path = pglob . gl_pathv [ n ]
} ;
p = strv_env_clean_with_callback ( p , invalid_env , & info ) ;
}
2011-03-04 03:44:43 +01:00
2013-01-02 12:41:52 +01:00
if ( r = = NULL )
r = p ;
else {
char * * m ;
2011-03-04 03:44:43 +01:00
2013-01-02 12:41:52 +01:00
m = strv_env_merge ( 2 , r , p ) ;
strv_free ( r ) ;
strv_free ( p ) ;
2013-03-25 00:09:19 +01:00
if ( ! m )
2013-01-02 12:41:52 +01:00
return - ENOMEM ;
r = m ;
}
2011-03-04 03:44:43 +01:00
}
}
* l = r ;
return 0 ;
}
2013-02-28 01:36:55 +01:00
static bool tty_may_match_dev_console ( const char * tty ) {
2014-06-24 19:00:32 +02:00
_cleanup_free_ char * active = NULL ;
2015-08-06 00:31:09 +02:00
char * console ;
2013-02-28 01:36:55 +01:00
2016-01-28 16:25:39 +01:00
if ( ! tty )
return true ;
2017-08-09 19:01:18 +02:00
tty = skip_dev_prefix ( tty ) ;
2013-02-28 01:36:55 +01:00
/* trivial identity? */
if ( streq ( tty , " console " ) )
return true ;
console = resolve_dev_console ( & active ) ;
/* if we could not resolve, assume it may */
if ( ! console )
return true ;
/* "tty0" means the active VC, so it may be the same sometimes */
2014-06-24 19:00:32 +02:00
return streq ( console , tty ) | | ( streq ( console , " tty0 " ) & & tty_is_vc ( tty ) ) ;
2013-02-28 01:36:55 +01:00
}
bool exec_context_may_touch_console ( ExecContext * ec ) {
2016-01-28 16:25:39 +01:00
return ( ec - > tty_reset | |
ec - > tty_vhangup | |
ec - > tty_vt_disallocate | |
2013-02-28 01:36:55 +01:00
is_terminal_input ( ec - > std_input ) | |
is_terminal_output ( ec - > std_output ) | |
is_terminal_output ( ec - > std_error ) ) & &
2016-01-28 16:25:39 +01:00
tty_may_match_dev_console ( exec_context_tty_path ( ec ) ) ;
2013-02-28 01:36:55 +01:00
}
2010-04-21 22:15:06 +02:00
static void strv_fprintf ( FILE * f , char * * l ) {
char * * g ;
assert ( f ) ;
STRV_FOREACH ( g , l )
fprintf ( f , " %s " , * g ) ;
}
2010-01-23 01:52:57 +01:00
void exec_context_dump ( ExecContext * c , FILE * f , const char * prefix ) {
2015-10-15 21:15:11 +02:00
char * * e , * * d ;
2010-01-30 01:55:42 +01:00
unsigned i ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
ExecDirectoryType dt ;
2016-11-02 03:25:19 +01:00
int r ;
2010-01-29 20:46:22 +01:00
2010-01-23 01:52:57 +01:00
assert ( c ) ;
assert ( f ) ;
2013-06-27 04:14:27 +02:00
prefix = strempty ( prefix ) ;
2010-01-23 01:52:57 +01:00
fprintf ( f ,
2010-01-30 01:55:42 +01:00
" %sUMask: %04o \n "
" %sWorkingDirectory: %s \n "
2010-02-12 02:00:18 +01:00
" %sRootDirectory: %s \n "
2010-04-21 22:15:06 +02:00
" %sNonBlocking: %s \n "
2011-06-30 00:11:25 +02:00
" %sPrivateTmp: %s \n "
2014-01-20 19:54:51 +01:00
" %sPrivateDevices: %s \n "
2016-08-22 18:43:59 +02:00
" %sProtectKernelTunables: %s \n "
2016-10-09 12:31:51 +02:00
" %sProtectKernelModules: %s \n "
2016-08-22 18:43:59 +02:00
" %sProtectControlGroups: %s \n "
2016-08-03 18:44:51 +02:00
" %sPrivateNetwork: %s \n "
" %sPrivateUsers: %s \n "
2014-06-04 18:07:55 +02:00
" %sProtectHome: %s \n "
" %sProtectSystem: %s \n "
2016-12-22 23:34:35 +01:00
" %sMountAPIVFS: %s \n "
2016-06-03 17:58:18 +02:00
" %sIgnoreSIGPIPE: %s \n "
2016-06-23 01:45:45 +02:00
" %sMemoryDenyWriteExecute: %s \n "
2017-09-14 21:19:05 +02:00
" %sRestrictRealtime: %s \n "
" %sKeyringMode: %s \n " ,
2010-01-23 01:52:57 +01:00
prefix , c - > umask ,
2010-01-29 20:46:22 +01:00
prefix , c - > working_directory ? c - > working_directory : " / " ,
2010-02-12 02:00:18 +01:00
prefix , c - > root_directory ? c - > root_directory : " / " ,
2010-04-21 22:15:06 +02:00
prefix , yes_no ( c - > non_blocking ) ,
2011-06-30 00:11:25 +02:00
prefix , yes_no ( c - > private_tmp ) ,
2014-01-20 19:54:51 +01:00
prefix , yes_no ( c - > private_devices ) ,
2016-08-22 18:43:59 +02:00
prefix , yes_no ( c - > protect_kernel_tunables ) ,
2016-10-09 12:31:51 +02:00
prefix , yes_no ( c - > protect_kernel_modules ) ,
2016-08-22 18:43:59 +02:00
prefix , yes_no ( c - > protect_control_groups ) ,
2016-08-03 18:44:51 +02:00
prefix , yes_no ( c - > private_network ) ,
prefix , yes_no ( c - > private_users ) ,
2014-06-04 18:07:55 +02:00
prefix , protect_home_to_string ( c - > protect_home ) ,
prefix , protect_system_to_string ( c - > protect_system ) ,
2016-12-22 23:34:35 +01:00
prefix , yes_no ( c - > mount_apivfs ) ,
2016-06-03 17:58:18 +02:00
prefix , yes_no ( c - > ignore_sigpipe ) ,
2016-06-23 01:45:45 +02:00
prefix , yes_no ( c - > memory_deny_write_execute ) ,
2017-09-14 21:19:05 +02:00
prefix , yes_no ( c - > restrict_realtime ) ,
prefix , exec_keyring_mode_to_string ( c - > keyring_mode ) ) ;
2010-01-28 02:53:56 +01:00
2016-12-23 14:26:05 +01:00
if ( c - > root_image )
fprintf ( f , " %sRootImage: %s \n " , prefix , c - > root_image ) ;
2011-03-04 03:44:43 +01:00
STRV_FOREACH ( e , c - > environment )
fprintf ( f , " %sEnvironment: %s \n " , prefix , * e ) ;
STRV_FOREACH ( e , c - > environment_files )
fprintf ( f , " %sEnvironmentFile: %s \n " , prefix , * e ) ;
2010-01-30 01:55:42 +01:00
2015-09-07 08:06:53 +02:00
STRV_FOREACH ( e , c - > pass_environment )
fprintf ( f , " %sPassEnvironment: %s \n " , prefix , * e ) ;
2017-09-10 12:16:44 +02:00
STRV_FOREACH ( e , c - > unset_environment )
fprintf ( f , " %sUnsetEnvironment: %s \n " , prefix , * e ) ;
2017-07-17 09:22:25 +02:00
fprintf ( f , " %sRuntimeDirectoryPreserve: %s \n " , prefix , exec_preserve_mode_to_string ( c - > runtime_directory_preserve_mode ) ) ;
2017-09-28 16:58:43 +02:00
for ( dt = 0 ; dt < _EXEC_DIRECTORY_TYPE_MAX ; dt + + ) {
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
fprintf ( f , " %s%sMode: %04o \n " , prefix , exec_directory_type_to_string ( dt ) , c - > directories [ dt ] . mode ) ;
STRV_FOREACH ( d , c - > directories [ dt ] . paths )
fprintf ( f , " %s%s: %s \n " , prefix , exec_directory_type_to_string ( dt ) , * d ) ;
}
2015-10-15 21:15:11 +02:00
2010-01-28 02:53:56 +01:00
if ( c - > nice_set )
fprintf ( f ,
" %sNice: %i \n " ,
prefix , c - > nice ) ;
2010-08-31 01:33:39 +02:00
if ( c - > oom_score_adjust_set )
2010-01-28 02:53:56 +01:00
fprintf ( f ,
2010-08-31 01:33:39 +02:00
" %sOOMScoreAdjust: %i \n " ,
prefix , c - > oom_score_adjust ) ;
2010-01-29 20:46:22 +01:00
2010-01-30 01:55:42 +01:00
for ( i = 0 ; i < RLIM_NLIMITS ; i + + )
2015-11-28 18:15:03 +01:00
if ( c - > rlimit [ i ] ) {
fprintf ( f , " %s%s: " RLIM_FMT " \n " ,
prefix , rlimit_to_string ( i ) , c - > rlimit [ i ] - > rlim_max ) ;
fprintf ( f , " %s%sSoft: " RLIM_FMT " \n " ,
prefix , rlimit_to_string ( i ) , c - > rlimit [ i ] - > rlim_cur ) ;
}
2010-01-30 01:55:42 +01:00
2012-10-30 14:29:38 +01:00
if ( c - > ioprio_set ) {
2014-02-19 17:49:00 +01:00
_cleanup_free_ char * class_str = NULL ;
2012-10-30 14:29:38 +01:00
2017-08-06 16:34:55 +02:00
r = ioprio_class_to_string_alloc ( IOPRIO_PRIO_CLASS ( c - > ioprio ) , & class_str ) ;
if ( r > = 0 )
fprintf ( f , " %sIOSchedulingClass: %s \n " , prefix , class_str ) ;
fprintf ( f , " %sIOPriority: %lu \n " , prefix , IOPRIO_PRIO_DATA ( c - > ioprio ) ) ;
2012-10-30 14:29:38 +01:00
}
2010-01-30 01:55:42 +01:00
2012-10-30 14:29:38 +01:00
if ( c - > cpu_sched_set ) {
2014-02-19 17:49:00 +01:00
_cleanup_free_ char * policy_str = NULL ;
2012-10-30 14:29:38 +01:00
2017-08-06 16:34:55 +02:00
r = sched_policy_to_string_alloc ( c - > cpu_sched_policy , & policy_str ) ;
if ( r > = 0 )
fprintf ( f , " %sCPUSchedulingPolicy: %s \n " , prefix , policy_str ) ;
2010-01-30 01:55:42 +01:00
fprintf ( f ,
2010-02-02 12:50:04 +01:00
" %sCPUSchedulingPriority: %i \n "
" %sCPUSchedulingResetOnFork: %s \n " ,
prefix , c - > cpu_sched_priority ,
prefix , yes_no ( c - > cpu_sched_reset_on_fork ) ) ;
2013-01-09 21:03:11 +01:00
}
2010-01-30 01:55:42 +01:00
2010-07-04 16:44:58 +02:00
if ( c - > cpuset ) {
2010-01-30 01:55:42 +01:00
fprintf ( f , " %sCPUAffinity: " , prefix ) ;
2010-07-04 16:44:58 +02:00
for ( i = 0 ; i < c - > cpuset_ncpus ; i + + )
if ( CPU_ISSET_S ( i , CPU_ALLOC_SIZE ( c - > cpuset_ncpus ) , c - > cpuset ) )
2013-12-25 19:00:12 +01:00
fprintf ( f , " %u " , i ) ;
2010-01-30 01:55:42 +01:00
fputs ( " \n " , f ) ;
}
2014-07-29 12:23:31 +02:00
if ( c - > timer_slack_nsec ! = NSEC_INFINITY )
2013-12-30 23:22:26 +01:00
fprintf ( f , " %sTimerSlackNSec: " NSEC_FMT " \n " , prefix , c - > timer_slack_nsec ) ;
2010-01-30 01:55:42 +01:00
fprintf ( f ,
2010-04-13 02:06:27 +02:00
" %sStandardInput: %s \n "
" %sStandardOutput: %s \n "
" %sStandardError: %s \n " ,
prefix , exec_input_to_string ( c - > std_input ) ,
prefix , exec_output_to_string ( c - > std_output ) ,
prefix , exec_output_to_string ( c - > std_error ) ) ;
if ( c - > tty_path )
fprintf ( f ,
2011-05-18 01:07:31 +02:00
" %sTTYPath: %s \n "
" %sTTYReset: %s \n "
" %sTTYVHangup: %s \n "
" %sTTYVTDisallocate: %s \n " ,
prefix , c - > tty_path ,
prefix , yes_no ( c - > tty_reset ) ,
prefix , yes_no ( c - > tty_vhangup ) ,
prefix , yes_no ( c - > tty_vt_disallocate ) ) ;
2010-01-30 01:55:42 +01:00
2017-08-01 11:52:36 +02:00
if ( IN_SET ( c - > std_output ,
EXEC_OUTPUT_SYSLOG ,
EXEC_OUTPUT_KMSG ,
EXEC_OUTPUT_JOURNAL ,
EXEC_OUTPUT_SYSLOG_AND_CONSOLE ,
EXEC_OUTPUT_KMSG_AND_CONSOLE ,
EXEC_OUTPUT_JOURNAL_AND_CONSOLE ) | |
IN_SET ( c - > std_error ,
EXEC_OUTPUT_SYSLOG ,
EXEC_OUTPUT_KMSG ,
EXEC_OUTPUT_JOURNAL ,
EXEC_OUTPUT_SYSLOG_AND_CONSOLE ,
EXEC_OUTPUT_KMSG_AND_CONSOLE ,
EXEC_OUTPUT_JOURNAL_AND_CONSOLE ) ) {
2012-10-30 14:29:38 +01:00
2014-01-01 04:35:54 +01:00
_cleanup_free_ char * fac_str = NULL , * lvl_str = NULL ;
2012-10-30 14:29:38 +01:00
2017-08-06 16:34:55 +02:00
r = log_facility_unshifted_to_string_alloc ( c - > syslog_priority > > 3 , & fac_str ) ;
if ( r > = 0 )
fprintf ( f , " %sSyslogFacility: %s \n " , prefix , fac_str ) ;
2012-10-30 14:29:38 +01:00
2017-08-06 16:34:55 +02:00
r = log_level_to_string_alloc ( LOG_PRI ( c - > syslog_priority ) , & lvl_str ) ;
if ( r > = 0 )
fprintf ( f , " %sSyslogLevel: %s \n " , prefix , lvl_str ) ;
2012-10-30 14:29:38 +01:00
}
2010-01-30 01:55:42 +01:00
2017-08-07 16:40:25 +02:00
if ( c - > secure_bits ) {
_cleanup_free_ char * str = NULL ;
r = secure_bits_to_string_alloc ( c - > secure_bits , & str ) ;
if ( r > = 0 )
fprintf ( f , " %sSecure Bits: %s \n " , prefix , str ) ;
}
2010-01-30 01:55:42 +01:00
2016-01-07 23:00:04 +01:00
if ( c - > capability_bounding_set ! = CAP_ALL ) {
2017-08-07 16:25:11 +02:00
_cleanup_free_ char * str = NULL ;
2010-01-30 01:55:42 +01:00
2017-08-07 16:25:11 +02:00
r = capability_set_to_string_alloc ( c - > capability_bounding_set , & str ) ;
if ( r > = 0 )
fprintf ( f , " %sCapabilityBoundingSet: %s \n " , prefix , str ) ;
2015-12-31 13:54:44 +01:00
}
if ( c - > capability_ambient_set ! = 0 ) {
2017-08-07 16:25:11 +02:00
_cleanup_free_ char * str = NULL ;
2015-12-31 13:54:44 +01:00
2017-08-07 16:25:11 +02:00
r = capability_set_to_string_alloc ( c - > capability_ambient_set , & str ) ;
if ( r > = 0 )
fprintf ( f , " %sAmbientCapabilities: %s \n " , prefix , str ) ;
2010-01-30 01:55:42 +01:00
}
if ( c - > user )
2010-06-18 23:25:19 +02:00
fprintf ( f , " %sUser: %s \n " , prefix , c - > user ) ;
2010-01-30 01:55:42 +01:00
if ( c - > group )
2010-06-18 23:25:19 +02:00
fprintf ( f , " %sGroup: %s \n " , prefix , c - > group ) ;
2010-01-30 01:55:42 +01:00
2016-07-14 12:37:28 +02:00
fprintf ( f , " %sDynamicUser: %s \n " , prefix , yes_no ( c - > dynamic_user ) ) ;
2017-10-04 11:33:30 +02:00
if ( ! strv_isempty ( c - > supplementary_groups ) ) {
2010-01-30 01:55:42 +01:00
fprintf ( f , " %sSupplementaryGroups: " , prefix ) ;
2010-04-21 22:15:06 +02:00
strv_fprintf ( f , c - > supplementary_groups ) ;
fputs ( " \n " , f ) ;
}
2010-01-30 01:55:42 +01:00
2010-06-16 21:54:17 +02:00
if ( c - > pam_name )
2010-06-18 23:25:19 +02:00
fprintf ( f , " %sPAMName: %s \n " , prefix , c - > pam_name ) ;
2010-06-16 21:54:17 +02:00
2016-07-07 11:17:00 +02:00
if ( strv_length ( c - > read_write_paths ) > 0 ) {
fprintf ( f , " %sReadWritePaths: " , prefix ) ;
strv_fprintf ( f , c - > read_write_paths ) ;
2010-04-21 22:15:06 +02:00
fputs ( " \n " , f ) ;
}
2016-07-07 11:17:00 +02:00
if ( strv_length ( c - > read_only_paths ) > 0 ) {
fprintf ( f , " %sReadOnlyPaths: " , prefix ) ;
strv_fprintf ( f , c - > read_only_paths ) ;
2010-04-21 22:15:06 +02:00
fputs ( " \n " , f ) ;
}
2010-01-30 01:55:42 +01:00
2016-07-07 11:17:00 +02:00
if ( strv_length ( c - > inaccessible_paths ) > 0 ) {
fprintf ( f , " %sInaccessiblePaths: " , prefix ) ;
strv_fprintf ( f , c - > inaccessible_paths ) ;
2010-01-30 01:55:42 +01:00
fputs ( " \n " , f ) ;
}
2010-07-10 04:49:37 +02:00
2016-11-23 22:21:40 +01:00
if ( c - > n_bind_mounts > 0 )
for ( i = 0 ; i < c - > n_bind_mounts ; i + + ) {
fprintf ( f , " %s%s: %s:%s:%s \n " , prefix ,
c - > bind_mounts [ i ] . read_only ? " BindReadOnlyPaths " : " BindPaths " ,
c - > bind_mounts [ i ] . source ,
c - > bind_mounts [ i ] . destination ,
c - > bind_mounts [ i ] . recursive ? " rbind " : " norbind " ) ;
}
2010-10-08 16:06:23 +02:00
if ( c - > utmp_id )
fprintf ( f ,
" %sUtmpIdentifier: %s \n " ,
prefix , c - > utmp_id ) ;
2014-02-06 10:05:16 +01:00
if ( c - > selinux_context )
fprintf ( f ,
2014-02-17 16:52:52 +01:00
" %sSELinuxContext: %s%s \n " ,
prefix , c - > selinux_context_ignore ? " - " : " " , c - > selinux_context ) ;
2014-02-12 18:28:21 +01:00
2017-07-13 06:10:41 +02:00
if ( c - > apparmor_profile )
fprintf ( f ,
" %sAppArmorProfile: %s%s \n " ,
prefix , c - > apparmor_profile_ignore ? " - " : " " , c - > apparmor_profile ) ;
if ( c - > smack_process_label )
fprintf ( f ,
" %sSmackProcessLabel: %s%s \n " ,
prefix , c - > smack_process_label_ignore ? " - " : " " , c - > smack_process_label ) ;
2015-05-21 19:48:49 +02:00
if ( c - > personality ! = PERSONALITY_INVALID )
2014-02-19 02:15:24 +01:00
fprintf ( f ,
" %sPersonality: %s \n " ,
prefix , strna ( personality_to_string ( c - > personality ) ) ) ;
2017-07-04 14:48:18 +02:00
fprintf ( f ,
" %sLockPersonality: %s \n " ,
prefix , yes_no ( c - > lock_personality ) ) ;
2014-02-12 18:28:21 +01:00
if ( c - > syscall_filter ) {
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
2014-02-12 18:28:21 +01:00
Iterator j ;
void * id ;
bool first = true ;
2014-02-12 18:44:40 +01:00
# endif
2014-02-12 18:28:21 +01:00
fprintf ( f ,
2014-02-13 00:24:00 +01:00
" %sSystemCallFilter: " ,
2014-02-12 18:28:21 +01:00
prefix ) ;
if ( ! c - > syscall_whitelist )
fputc ( ' ~ ' , f ) ;
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
2014-02-12 18:28:21 +01:00
SET_FOREACH ( id , c - > syscall_filter , j ) {
_cleanup_free_ char * name = NULL ;
if ( first )
first = false ;
else
fputc ( ' ' , f ) ;
2014-02-13 00:24:00 +01:00
name = seccomp_syscall_resolve_num_arch ( SCMP_ARCH_NATIVE , PTR_TO_INT ( id ) - 1 ) ;
2014-02-12 18:28:21 +01:00
fputs ( strna ( name ) , f ) ;
}
2014-02-12 18:44:40 +01:00
# endif
2014-02-12 18:28:21 +01:00
fputc ( ' \n ' , f ) ;
}
2014-02-13 00:24:00 +01:00
if ( c - > syscall_archs ) {
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
2014-02-13 00:24:00 +01:00
Iterator j ;
void * id ;
# endif
fprintf ( f ,
" %sSystemCallArchitectures: " ,
prefix ) ;
2017-10-03 10:41:51 +02:00
# if HAVE_SECCOMP
2014-02-13 00:24:00 +01:00
SET_FOREACH ( id , c - > syscall_archs , j )
fprintf ( f , " %s " , strna ( seccomp_arch_to_string ( PTR_TO_UINT32 ( id ) - 1 ) ) ) ;
# endif
fputc ( ' \n ' , f ) ;
}
2016-11-02 03:25:19 +01:00
if ( exec_context_restrict_namespaces_set ( c ) ) {
_cleanup_free_ char * s = NULL ;
r = namespace_flag_to_string_many ( c - > restrict_namespaces , & s ) ;
if ( r > = 0 )
fprintf ( f , " %sRestrictNamespaces: %s \n " ,
prefix , s ) ;
}
2016-01-11 20:31:14 +01:00
if ( c - > syscall_errno > 0 )
2014-02-12 18:28:21 +01:00
fprintf ( f ,
" %sSystemCallErrorNumber: %s \n " ,
prefix , strna ( errno_to_name ( c - > syscall_errno ) ) ) ;
2014-02-20 16:19:44 +01:00
if ( c - > apparmor_profile )
fprintf ( f ,
" %sAppArmorProfile: %s%s \n " ,
prefix , c - > apparmor_profile_ignore ? " - " : " " , c - > apparmor_profile ) ;
2010-01-23 01:52:57 +01:00
}
2014-11-05 17:57:23 +01:00
bool exec_context_maintains_privileges ( ExecContext * c ) {
assert ( c ) ;
2016-07-10 14:48:23 +02:00
/* Returns true if the process forked off would run under
2014-11-05 17:57:23 +01:00
* an unchanged UID or as root . */
if ( ! c - > user )
return true ;
if ( streq ( c - > user , " root " ) | | streq ( c - > user , " 0 " ) )
return true ;
return false ;
}
2017-06-26 17:40:08 +02:00
int exec_context_get_effective_ioprio ( ExecContext * c ) {
int p ;
assert ( c ) ;
if ( c - > ioprio_set )
return c - > ioprio ;
p = ioprio_get ( IOPRIO_WHO_PROCESS , 0 ) ;
if ( p < 0 )
return IOPRIO_PRIO_VALUE ( IOPRIO_CLASS_BE , 4 ) ;
return p ;
}
2010-07-04 18:49:58 +02:00
void exec_status_start ( ExecStatus * s , pid_t pid ) {
2010-01-26 04:18:44 +01:00
assert ( s ) ;
2010-01-23 01:52:57 +01:00
2010-07-04 18:49:58 +02:00
zero ( * s ) ;
s - > pid = pid ;
dual_timestamp_get ( & s - > start_timestamp ) ;
}
2011-05-18 01:07:31 +02:00
void exec_status_exit ( ExecStatus * s , ExecContext * context , pid_t pid , int code , int status ) {
2010-07-04 18:49:58 +02:00
assert ( s ) ;
2011-12-17 01:33:40 +01:00
if ( s - > pid & & s - > pid ! = pid )
2010-07-04 18:49:58 +02:00
zero ( * s ) ;
2010-01-26 04:18:44 +01:00
s - > pid = pid ;
2010-07-01 00:26:44 +02:00
dual_timestamp_get ( & s - > exit_timestamp ) ;
2010-04-10 05:03:14 +02:00
2010-01-26 04:18:44 +01:00
s - > code = code ;
s - > status = status ;
2010-10-08 16:06:23 +02:00
2011-05-18 01:07:31 +02:00
if ( context ) {
if ( context - > utmp_id )
utmp_put_dead_process ( context - > utmp_id , pid , code , status ) ;
2016-01-28 16:25:39 +01:00
exec_context_tty_reset ( context , NULL ) ;
2011-05-18 01:07:31 +02:00
}
2010-04-10 05:03:14 +02:00
}
void exec_status_dump ( ExecStatus * s , FILE * f , const char * prefix ) {
char buf [ FORMAT_TIMESTAMP_MAX ] ;
assert ( s ) ;
assert ( f ) ;
if ( s - > pid < = 0 )
return ;
2014-08-21 16:15:49 +02:00
prefix = strempty ( prefix ) ;
2010-04-10 05:03:14 +02:00
fprintf ( f ,
2013-12-30 23:22:26 +01:00
" %sPID: " PID_FMT " \n " ,
prefix , s - > pid ) ;
2010-04-10 05:03:14 +02:00
2016-07-27 11:50:37 +02:00
if ( dual_timestamp_is_set ( & s - > start_timestamp ) )
2010-04-10 05:03:14 +02:00
fprintf ( f ,
" %sStart Timestamp: %s \n " ,
2010-07-01 00:26:44 +02:00
prefix , format_timestamp ( buf , sizeof ( buf ) , s - > start_timestamp . realtime ) ) ;
2010-04-10 05:03:14 +02:00
2016-07-27 11:50:37 +02:00
if ( dual_timestamp_is_set ( & s - > exit_timestamp ) )
2010-04-10 05:03:14 +02:00
fprintf ( f ,
" %sExit Timestamp: %s \n "
" %sExit Code: %s \n "
" %sExit Status: %i \n " ,
2010-07-01 00:26:44 +02:00
prefix , format_timestamp ( buf , sizeof ( buf ) , s - > exit_timestamp . realtime ) ,
2010-04-10 05:03:14 +02:00
prefix , sigchld_code_to_string ( s - > code ) ,
prefix , s - > status ) ;
2010-01-23 01:52:57 +01:00
}
2010-01-26 07:02:51 +01:00
2010-04-15 03:11:11 +02:00
char * exec_command_line ( char * * argv ) {
2010-01-26 07:02:51 +01:00
size_t k ;
char * n , * p , * * a ;
bool first = true ;
2010-04-15 03:11:11 +02:00
assert ( argv ) ;
2010-01-26 07:02:51 +01:00
2010-01-27 02:15:54 +01:00
k = 1 ;
2010-04-15 03:11:11 +02:00
STRV_FOREACH ( a , argv )
2010-01-26 07:02:51 +01:00
k + = strlen ( * a ) + 3 ;
2016-10-25 15:52:54 +02:00
n = new ( char , k ) ;
if ( ! n )
2010-01-26 07:02:51 +01:00
return NULL ;
p = n ;
2010-04-15 03:11:11 +02:00
STRV_FOREACH ( a , argv ) {
2010-01-26 07:02:51 +01:00
if ( ! first )
* ( p + + ) = ' ' ;
else
first = false ;
if ( strpbrk ( * a , WHITESPACE ) ) {
* ( p + + ) = ' \' ' ;
p = stpcpy ( p , * a ) ;
* ( p + + ) = ' \' ' ;
} else
p = stpcpy ( p , * a ) ;
}
2010-01-27 02:15:54 +01:00
* p = 0 ;
2010-01-26 07:02:51 +01:00
/* FIXME: this doesn't really handle arguments that have
* spaces and ticks in them */
return n ;
}
void exec_command_dump ( ExecCommand * c , FILE * f , const char * prefix ) {
2014-06-24 19:00:32 +02:00
_cleanup_free_ char * cmd = NULL ;
2014-08-21 16:15:49 +02:00
const char * prefix2 ;
2010-01-26 07:02:51 +01:00
assert ( c ) ;
assert ( f ) ;
2014-08-21 16:15:49 +02:00
prefix = strempty ( prefix ) ;
2015-02-03 02:05:59 +01:00
prefix2 = strjoina ( prefix , " \t " ) ;
2010-01-26 07:02:51 +01:00
2010-04-15 03:11:11 +02:00
cmd = exec_command_line ( c - > argv ) ;
2010-01-26 07:02:51 +01:00
fprintf ( f ,
" %sCommand Line: %s \n " ,
prefix , cmd ? cmd : strerror ( ENOMEM ) ) ;
2010-04-10 05:03:14 +02:00
exec_status_dump ( & c - > exec_status , f , prefix2 ) ;
2010-01-26 07:02:51 +01:00
}
void exec_command_dump_list ( ExecCommand * c , FILE * f , const char * prefix ) {
assert ( f ) ;
2014-08-21 16:15:49 +02:00
prefix = strempty ( prefix ) ;
2010-01-26 07:02:51 +01:00
LIST_FOREACH ( command , c , c )
exec_command_dump ( c , f , prefix ) ;
}
2010-01-30 01:55:42 +01:00
2010-02-14 01:05:55 +01:00
void exec_command_append_list ( ExecCommand * * l , ExecCommand * e ) {
ExecCommand * end ;
assert ( l ) ;
assert ( e ) ;
if ( * l ) {
2011-02-21 15:32:17 +01:00
/* It's kind of important, that we keep the order here */
2013-10-14 06:10:14 +02:00
LIST_FIND_TAIL ( command , * l , end ) ;
LIST_INSERT_AFTER ( command , * l , end , e ) ;
2010-02-14 01:05:55 +01:00
} else
* l = e ;
}
2010-04-10 17:46:41 +02:00
int exec_command_set ( ExecCommand * c , const char * path , . . . ) {
va_list ap ;
char * * l , * p ;
assert ( c ) ;
assert ( path ) ;
va_start ( ap , path ) ;
l = strv_new_ap ( path , ap ) ;
va_end ( ap ) ;
if ( ! l )
return - ENOMEM ;
2013-10-29 19:53:43 +01:00
p = strdup ( path ) ;
if ( ! p ) {
2010-04-10 17:46:41 +02:00
strv_free ( l ) ;
return - ENOMEM ;
}
free ( c - > path ) ;
c - > path = p ;
strv_free ( c - > argv ) ;
c - > argv = l ;
return 0 ;
}
2014-09-24 14:29:05 +02:00
int exec_command_append ( ExecCommand * c , const char * path , . . . ) {
2014-09-30 11:34:01 +02:00
_cleanup_strv_free_ char * * l = NULL ;
2014-09-24 14:29:05 +02:00
va_list ap ;
int r ;
assert ( c ) ;
assert ( path ) ;
va_start ( ap , path ) ;
l = strv_new_ap ( path , ap ) ;
va_end ( ap ) ;
if ( ! l )
return - ENOMEM ;
2015-10-07 11:26:10 +02:00
r = strv_extend_strv ( & c - > argv , l , false ) ;
2014-09-30 11:34:01 +02:00
if ( r < 0 )
2014-09-24 14:29:05 +02:00
return r ;
return 0 ;
}
2013-11-27 20:23:18 +01:00
static int exec_runtime_allocate ( ExecRuntime * * rt ) {
if ( * rt )
return 0 ;
* rt = new0 ( ExecRuntime , 1 ) ;
2013-12-30 00:18:39 +01:00
if ( ! * rt )
2013-11-27 20:23:18 +01:00
return - ENOMEM ;
( * rt ) - > n_ref = 1 ;
( * rt ) - > netns_storage_socket [ 0 ] = ( * rt ) - > netns_storage_socket [ 1 ] = - 1 ;
return 0 ;
}
int exec_runtime_make ( ExecRuntime * * rt , ExecContext * c , const char * id ) {
int r ;
assert ( rt ) ;
assert ( c ) ;
assert ( id ) ;
if ( * rt )
return 1 ;
if ( ! c - > private_network & & ! c - > private_tmp )
return 0 ;
r = exec_runtime_allocate ( rt ) ;
if ( r < 0 )
return r ;
if ( c - > private_network & & ( * rt ) - > netns_storage_socket [ 0 ] < 0 ) {
2016-07-14 13:12:01 +02:00
if ( socketpair ( AF_UNIX , SOCK_DGRAM | SOCK_CLOEXEC , 0 , ( * rt ) - > netns_storage_socket ) < 0 )
2013-11-27 20:23:18 +01:00
return - errno ;
}
if ( c - > private_tmp & & ! ( * rt ) - > tmp_dir ) {
r = setup_tmp_dirs ( id , & ( * rt ) - > tmp_dir , & ( * rt ) - > var_tmp_dir ) ;
if ( r < 0 )
return r ;
}
return 1 ;
}
ExecRuntime * exec_runtime_ref ( ExecRuntime * r ) {
assert ( r ) ;
assert ( r - > n_ref > 0 ) ;
r - > n_ref + + ;
return r ;
}
ExecRuntime * exec_runtime_unref ( ExecRuntime * r ) {
if ( ! r )
return NULL ;
assert ( r - > n_ref > 0 ) ;
r - > n_ref - - ;
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
if ( r - > n_ref > 0 )
return NULL ;
free ( r - > tmp_dir ) ;
free ( r - > var_tmp_dir ) ;
safe_close_pair ( r - > netns_storage_socket ) ;
2016-10-17 00:28:30 +02:00
return mfree ( r ) ;
2013-11-27 20:23:18 +01:00
}
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
int exec_runtime_serialize ( Unit * u , ExecRuntime * rt , FILE * f , FDSet * fds ) {
2013-11-27 20:23:18 +01:00
assert ( u ) ;
assert ( f ) ;
assert ( fds ) ;
if ( ! rt )
return 0 ;
if ( rt - > tmp_dir )
unit_serialize_item ( u , f , " tmp-dir " , rt - > tmp_dir ) ;
if ( rt - > var_tmp_dir )
unit_serialize_item ( u , f , " var-tmp-dir " , rt - > var_tmp_dir ) ;
if ( rt - > netns_storage_socket [ 0 ] > = 0 ) {
int copy ;
copy = fdset_put_dup ( fds , rt - > netns_storage_socket [ 0 ] ) ;
if ( copy < 0 )
return copy ;
unit_serialize_item_format ( u , f , " netns-socket-0 " , " %i " , copy ) ;
}
if ( rt - > netns_storage_socket [ 1 ] > = 0 ) {
int copy ;
copy = fdset_put_dup ( fds , rt - > netns_storage_socket [ 1 ] ) ;
if ( copy < 0 )
return copy ;
unit_serialize_item_format ( u , f , " netns-socket-1 " , " %i " , copy ) ;
}
return 0 ;
}
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
int exec_runtime_deserialize_item ( Unit * u , ExecRuntime * * rt , const char * key , const char * value , FDSet * fds ) {
2013-11-27 20:23:18 +01:00
int r ;
assert ( rt ) ;
assert ( key ) ;
assert ( value ) ;
if ( streq ( key , " tmp-dir " ) ) {
char * copy ;
r = exec_runtime_allocate ( rt ) ;
if ( r < 0 )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
return log_oom ( ) ;
2013-11-27 20:23:18 +01:00
copy = strdup ( value ) ;
if ( ! copy )
return log_oom ( ) ;
free ( ( * rt ) - > tmp_dir ) ;
( * rt ) - > tmp_dir = copy ;
} else if ( streq ( key , " var-tmp-dir " ) ) {
char * copy ;
r = exec_runtime_allocate ( rt ) ;
if ( r < 0 )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
return log_oom ( ) ;
2013-11-27 20:23:18 +01:00
copy = strdup ( value ) ;
if ( ! copy )
return log_oom ( ) ;
free ( ( * rt ) - > var_tmp_dir ) ;
( * rt ) - > var_tmp_dir = copy ;
} else if ( streq ( key , " netns-socket-0 " ) ) {
int fd ;
r = exec_runtime_allocate ( rt ) ;
if ( r < 0 )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
return log_oom ( ) ;
2013-11-27 20:23:18 +01:00
if ( safe_atoi ( value , & fd ) < 0 | | ! fdset_contains ( fds , fd ) )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_unit_debug ( u , " Failed to parse netns socket value: %s " , value ) ;
2013-11-27 20:23:18 +01:00
else {
2014-03-18 19:22:43 +01:00
safe_close ( ( * rt ) - > netns_storage_socket [ 0 ] ) ;
2013-11-27 20:23:18 +01:00
( * rt ) - > netns_storage_socket [ 0 ] = fdset_remove ( fds , fd ) ;
}
} else if ( streq ( key , " netns-socket-1 " ) ) {
int fd ;
r = exec_runtime_allocate ( rt ) ;
if ( r < 0 )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
return log_oom ( ) ;
2013-11-27 20:23:18 +01:00
if ( safe_atoi ( value , & fd ) < 0 | | ! fdset_contains ( fds , fd ) )
core,network: major per-object logging rework
This changes log_unit_info() (and friends) to take a real Unit* object
insted of just a unit name as parameter. The call will now prefix all
logged messages with the unit name, thus allowing the unit name to be
dropped from the various passed romat strings, simplifying invocations
drastically, and unifying log output across messages. Also, UNIT= vs.
USER_UNIT= is now derived from the Manager object attached to the Unit
object, instead of getpid(). This has the benefit of correcting the
field for --test runs.
Also contains a couple of other logging improvements:
- Drops a couple of strerror() invocations in favour of using %m.
- Not only .mount units now warn if a symlinks exist for the mount
point already, .automount units do that too, now.
- A few invocations of log_struct() that didn't actually pass any
additional structured data have been replaced by simpler invocations
of log_unit_info() and friends.
- For structured data a new LOG_UNIT_MESSAGE() macro has been added,
that works like LOG_MESSAGE() but prefixes the message with the unit
name. Similar, there's now LOG_LINK_MESSAGE() and
LOG_NETDEV_MESSAGE().
- For structured data new LOG_UNIT_ID(), LOG_LINK_INTERFACE(),
LOG_NETDEV_INTERFACE() macros have been added that generate the
necessary per object fields. The old log_unit_struct() call has been
removed in favour of these new macros used in raw log_struct()
invocations. In addition to removing one more function call this
allows generated structured log messages that contain two object
fields, as necessary for example for network interfaces that are
joined into another network interface, and whose messages shall be
indexed by both.
- The LOG_ERRNO() macro has been removed, in favour of
log_struct_errno(). The latter has the benefit of ensuring that %m in
format strings is properly resolved to the specified error number.
- A number of logging messages have been converted to use
log_unit_info() instead of log_info()
- The client code in sysv-generator no longer #includes core code from
src/core/.
- log_unit_full_errno() has been removed, log_unit_full() instead takes
an errno now, too.
- log_unit_info(), log_link_info(), log_netdev_info() and friends, now
avoid double evaluation of their parameters
2015-05-11 20:38:21 +02:00
log_unit_debug ( u , " Failed to parse netns socket value: %s " , value ) ;
2013-11-27 20:23:18 +01:00
else {
2014-03-18 19:22:43 +01:00
safe_close ( ( * rt ) - > netns_storage_socket [ 1 ] ) ;
2013-11-27 20:23:18 +01:00
( * rt ) - > netns_storage_socket [ 1 ] = fdset_remove ( fds , fd ) ;
}
} else
return 0 ;
return 1 ;
}
static void * remove_tmpdir_thread ( void * p ) {
_cleanup_free_ char * path = p ;
2015-04-04 11:52:57 +02:00
( void ) rm_rf ( path , REMOVE_ROOT | REMOVE_PHYSICAL ) ;
2013-11-27 20:23:18 +01:00
return NULL ;
}
void exec_runtime_destroy ( ExecRuntime * rt ) {
2014-03-03 17:11:39 +01:00
int r ;
2013-11-27 20:23:18 +01:00
if ( ! rt )
return ;
/* If there are multiple users of this, let's leave the stuff around */
if ( rt - > n_ref > 1 )
return ;
if ( rt - > tmp_dir ) {
log_debug ( " Spawning thread to nuke %s " , rt - > tmp_dir ) ;
2014-03-03 17:11:39 +01:00
r = asynchronous_job ( remove_tmpdir_thread , rt - > tmp_dir ) ;
if ( r < 0 ) {
2014-11-28 13:19:16 +01:00
log_warning_errno ( r , " Failed to nuke %s: %m " , rt - > tmp_dir ) ;
2014-03-03 17:11:39 +01:00
free ( rt - > tmp_dir ) ;
}
2013-11-27 20:23:18 +01:00
rt - > tmp_dir = NULL ;
}
if ( rt - > var_tmp_dir ) {
log_debug ( " Spawning thread to nuke %s " , rt - > var_tmp_dir ) ;
2014-03-03 17:11:39 +01:00
r = asynchronous_job ( remove_tmpdir_thread , rt - > var_tmp_dir ) ;
if ( r < 0 ) {
2014-11-28 13:19:16 +01:00
log_warning_errno ( r , " Failed to nuke %s: %m " , rt - > var_tmp_dir ) ;
2014-03-03 17:11:39 +01:00
free ( rt - > var_tmp_dir ) ;
}
2013-11-27 20:23:18 +01:00
rt - > var_tmp_dir = NULL ;
}
2014-03-24 03:22:44 +01:00
safe_close_pair ( rt - > netns_storage_socket ) ;
2013-11-27 20:23:18 +01:00
}
2010-04-13 02:06:27 +02:00
static const char * const exec_input_table [ _EXEC_INPUT_MAX ] = {
[ EXEC_INPUT_NULL ] = " null " ,
[ EXEC_INPUT_TTY ] = " tty " ,
[ EXEC_INPUT_TTY_FORCE ] = " tty-force " ,
2010-04-15 06:19:54 +02:00
[ EXEC_INPUT_TTY_FAIL ] = " tty-fail " ,
2016-10-18 02:05:49 +02:00
[ EXEC_INPUT_SOCKET ] = " socket " ,
[ EXEC_INPUT_NAMED_FD ] = " fd " ,
2010-04-13 02:06:27 +02:00
} ;
2010-10-22 16:11:50 +02:00
DEFINE_STRING_TABLE_LOOKUP ( exec_input , ExecInput ) ;
2010-01-30 01:55:42 +01:00
static const char * const exec_output_table [ _EXEC_OUTPUT_MAX ] = {
2010-04-13 02:06:27 +02:00
[ EXEC_OUTPUT_INHERIT ] = " inherit " ,
2010-01-30 01:55:42 +01:00
[ EXEC_OUTPUT_NULL ] = " null " ,
2010-04-13 02:06:27 +02:00
[ EXEC_OUTPUT_TTY ] = " tty " ,
2010-01-30 01:55:42 +01:00
[ EXEC_OUTPUT_SYSLOG ] = " syslog " ,
2011-02-15 01:27:53 +01:00
[ EXEC_OUTPUT_SYSLOG_AND_CONSOLE ] = " syslog+console " ,
2010-05-19 21:49:03 +02:00
[ EXEC_OUTPUT_KMSG ] = " kmsg " ,
2011-02-15 01:27:53 +01:00
[ EXEC_OUTPUT_KMSG_AND_CONSOLE ] = " kmsg+console " ,
2012-01-05 23:54:45 +01:00
[ EXEC_OUTPUT_JOURNAL ] = " journal " ,
[ EXEC_OUTPUT_JOURNAL_AND_CONSOLE ] = " journal+console " ,
2016-10-18 02:05:49 +02:00
[ EXEC_OUTPUT_SOCKET ] = " socket " ,
[ EXEC_OUTPUT_NAMED_FD ] = " fd " ,
2010-01-30 01:55:42 +01:00
} ;
DEFINE_STRING_TABLE_LOOKUP ( exec_output , ExecOutput ) ;
2015-08-23 13:14:04 +02:00
static const char * const exec_utmp_mode_table [ _EXEC_UTMP_MODE_MAX ] = {
[ EXEC_UTMP_INIT ] = " init " ,
[ EXEC_UTMP_LOGIN ] = " login " ,
[ EXEC_UTMP_USER ] = " user " ,
} ;
DEFINE_STRING_TABLE_LOOKUP ( exec_utmp_mode , ExecUtmpMode ) ;
2017-07-17 09:22:25 +02:00
static const char * const exec_preserve_mode_table [ _EXEC_PRESERVE_MODE_MAX ] = {
[ EXEC_PRESERVE_NO ] = " no " ,
[ EXEC_PRESERVE_YES ] = " yes " ,
[ EXEC_PRESERVE_RESTART ] = " restart " ,
} ;
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN ( exec_preserve_mode , ExecPreserveMode , EXEC_PRESERVE_YES ) ;
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
2017-09-28 16:58:43 +02:00
static const char * const exec_directory_type_table [ _EXEC_DIRECTORY_TYPE_MAX ] = {
core: add {State,Cache,Log,Configuration}Directory= (#6384)
This introduces {State,Cache,Log,Configuration}Directory= those are
similar to RuntimeDirectory=. They create the directories under
/var/lib, /var/cache/, /var/log, or /etc, respectively, with the mode
specified in {State,Cache,Log,Configuration}DirectoryMode=.
This also fixes #6391.
2017-07-18 14:34:52 +02:00
[ EXEC_DIRECTORY_RUNTIME ] = " RuntimeDirectory " ,
[ EXEC_DIRECTORY_STATE ] = " StateDirectory " ,
[ EXEC_DIRECTORY_CACHE ] = " CacheDirectory " ,
[ EXEC_DIRECTORY_LOGS ] = " LogsDirectory " ,
[ EXEC_DIRECTORY_CONFIGURATION ] = " ConfigurationDirectory " ,
} ;
DEFINE_STRING_TABLE_LOOKUP ( exec_directory_type , ExecDirectoryType ) ;
2017-09-14 21:19:05 +02:00
static const char * const exec_keyring_mode_table [ _EXEC_KEYRING_MODE_MAX ] = {
[ EXEC_KEYRING_INHERIT ] = " inherit " ,
[ EXEC_KEYRING_PRIVATE ] = " private " ,
[ EXEC_KEYRING_SHARED ] = " shared " ,
} ;
DEFINE_STRING_TABLE_LOOKUP ( exec_keyring_mode , ExecKeyringMode ) ;