2010-08-14 19:59:25 +02:00
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2009-11-18 00:42:52 +01:00
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/>.
* * */
2009-11-18 00:42:52 +01:00
# include <assert.h>
# include <string.h>
# include <unistd.h>
# include <errno.h>
2009-11-19 00:46:47 +01:00
# include <stdlib.h>
2010-01-26 04:18:44 +01:00
# include <signal.h>
# include <stdio.h>
2010-01-30 01:52:32 +01:00
# include <syslog.h>
# include <sched.h>
# include <sys/resource.h>
2010-02-02 10:30:04 +01:00
# include <linux/sched.h>
2010-02-12 02:01:14 +01:00
# include <sys/types.h>
# include <sys/stat.h>
2010-04-06 21:53:02 +02:00
# include <fcntl.h>
2010-04-06 23:35:59 +02:00
# include <dirent.h>
2010-04-10 23:36:43 +02:00
# include <sys/ioctl.h>
# include <linux/vt.h>
# include <linux/tiocl.h>
2010-04-13 02:06:27 +02:00
# include <termios.h>
# include <stdarg.h>
# include <sys/inotify.h>
# include <sys/poll.h>
2010-05-09 18:13:02 +02:00
# include <ctype.h>
2010-06-16 21:54:17 +02:00
# include <sys/prctl.h>
2010-06-18 02:28:35 +02:00
# include <sys/utsname.h>
# include <pwd.h>
2010-07-01 00:29:17 +02:00
# include <netinet/ip.h>
2010-07-12 21:40:43 +02:00
# include <linux/kd.h>
2010-08-11 23:31:07 +02:00
# include <dlfcn.h>
2010-09-15 14:37:16 +02:00
# include <sys/wait.h>
2011-05-24 20:23:07 +02:00
# include <sys/time.h>
2011-07-07 02:07:39 +02:00
# include <glob.h>
2011-07-23 01:17:59 +02:00
# include <grp.h>
2011-10-07 21:06:39 +02:00
# include <sys/mman.h>
2012-07-10 18:46:26 +02:00
# include <sys/vfs.h>
# include <linux/magic.h>
2012-10-03 19:29:20 +02:00
# include <limits.h>
2012-11-02 17:27:15 +01:00
# include <langinfo.h>
# include <locale.h>
2014-02-18 23:35:19 +01:00
# include <sys/personality.h>
2013-03-29 01:17:24 +01:00
# include <libgen.h>
2013-12-07 03:29:55 +01:00
# undef basename
2009-11-18 00:42:52 +01:00
2013-12-22 19:59:12 +01:00
# ifdef HAVE_SYS_AUXV_H
# include <sys/auxv.h>
# endif
2009-11-18 00:42:52 +01:00
# include "macro.h"
# include "util.h"
2010-01-30 01:52:32 +01:00
# include "ioprio.h"
# include "missing.h"
2010-02-12 02:01:14 +01:00
# include "log.h"
2010-02-13 01:05:12 +01:00
# include "strv.h"
2010-08-11 22:58:34 +02:00
# include "label.h"
2012-05-07 21:36:12 +02:00
# include "path-util.h"
2010-08-19 03:18:49 +02:00
# include "exit-status.h"
2011-02-15 00:30:11 +01:00
# include "hashmap.h"
2013-02-11 03:46:08 +01:00
# include "env-util.h"
2013-02-14 12:26:13 +01:00
# include "fileio.h"
2013-09-18 18:12:04 +02:00
# include "device-nodes.h"
2013-09-21 03:37:33 +02:00
# include "utf8.h"
# include "gunicode.h"
2013-10-19 00:46:07 +02:00
# include "virt.h"
2013-11-08 19:32:45 +01:00
# include "def.h"
2014-03-05 02:29:58 +01:00
# include "missing.h"
Systemd is causing mislabeled devices to be created and then attempting to read them.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 07/28/2010 05:57 AM, Kay Sievers wrote:
> On Wed, Jul 28, 2010 at 11:43, Lennart Poettering
> <lennart@poettering.net> wrote:
>> On Mon, 26.07.10 16:42, Daniel J Walsh (dwalsh@redhat.com) wrote:
>>> tcontext=system_u:object_r:device_t:s0 tclass=chr_file
>>> type=1400 audit(1280174589.476:7): avc: denied { read } for pid=1
>>> comm="systemd" name="autofs" dev=devtmpfs ino=9482
>>> scontext=system_u:system_r:init_t:s0
>>> tcontext=system_u:object_r:device_t:s0 tclass=chr_file
>>> type=1400 audit(1280174589.476:8): avc: denied { read } for pid=1
>>> comm="systemd" name="autofs" dev=devtmpfs ino=9482
>>> scontext=system_u:system_r:init_t:s0
>>> tcontext=system_u:object_r:device_t:s0 tclass=chr_file
>>>
>>> Lennart, we talked about this earlier. I think this is caused by the
>>> modprobe calls to create /dev/autofs. Since udev is not created at the
>>> point that init loads the kernel modules, the devices get created with
>>> the wrong label. Once udev starts the labels get fixed.
>>>
>>> I can allow init_t to read device_t chr_files.
>>
>> Hmm, I think a cleaner fix would be to make systemd relabel this device
>> properly before accessing it? Given that this is only one device this
>> should not be a problem for us to maintain, I think? How would the
>> fixing of the label work? Would we have to spawn restorecon for this, or
>> can we actually do this in C without too much work?
>
> I guess we can just do what udev is doing, and call setfilecon(), with
> a context of an earlier matchpathcon().
>
> Kay
> _______________________________________________
> systemd-devel mailing list
> systemd-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
Here is the updated patch with a fix for the labeling of /dev/autofs
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
iEYEARECAAYFAkxQMyoACgkQrlYvE4MpobNviACfWgxsjW2xzz1qznFex8RVAQHf
gIEAmwRmRcLvGqYtwQaZ3WKIg8wmrwNk
=pC2e
2010-07-28 15:39:54 +02:00
2011-06-30 04:16:10 +02:00
int saved_argc = 0 ;
char * * saved_argv = NULL ;
2012-09-24 14:43:07 +02:00
2012-10-18 23:50:26 +02:00
static volatile unsigned cached_columns = 0 ;
2012-10-19 00:06:47 +02:00
static volatile unsigned cached_lines = 0 ;
2011-06-30 04:16:10 +02:00
2011-03-18 03:03:41 +01:00
size_t page_size ( void ) {
2013-12-16 01:24:14 +01:00
static thread_local size_t pgsz = 0 ;
2011-03-18 03:03:41 +01:00
long r ;
2011-10-07 21:06:39 +02:00
if ( _likely_ ( pgsz > 0 ) )
2011-03-18 03:03:41 +01:00
return pgsz ;
2012-09-14 10:06:42 +02:00
r = sysconf ( _SC_PAGESIZE ) ;
assert ( r > 0 ) ;
2011-03-18 03:03:41 +01:00
pgsz = ( size_t ) r ;
return pgsz ;
}
2010-04-10 04:38:14 +02:00
bool streq_ptr ( const char * a , const char * b ) {
/* Like streq(), but tries to make sense of NULL pointers */
if ( a & & b )
return streq ( a , b ) ;
if ( ! a & & ! b )
return true ;
return false ;
}
2012-10-19 04:52:51 +02:00
char * endswith ( const char * s , const char * postfix ) {
2009-11-18 00:42:52 +01:00
size_t sl , pl ;
assert ( s ) ;
assert ( postfix ) ;
sl = strlen ( s ) ;
pl = strlen ( postfix ) ;
2010-04-23 20:29:15 +02:00
if ( pl = = 0 )
2012-10-19 04:52:51 +02:00
return ( char * ) s + sl ;
2010-04-23 20:29:15 +02:00
2009-11-18 00:42:52 +01:00
if ( sl < pl )
2012-10-19 04:52:51 +02:00
return NULL ;
if ( memcmp ( s + sl - pl , postfix , pl ) ! = 0 )
return NULL ;
2009-11-18 00:42:52 +01:00
2012-10-19 04:52:51 +02:00
return ( char * ) s + sl - pl ;
2009-11-18 00:42:52 +01:00
}
2010-01-30 01:52:44 +01:00
bool first_word ( const char * s , const char * word ) {
size_t sl , wl ;
assert ( s ) ;
assert ( word ) ;
sl = strlen ( s ) ;
wl = strlen ( word ) ;
if ( sl < wl )
return false ;
2010-04-23 20:29:15 +02:00
if ( wl = = 0 )
return true ;
2010-01-30 01:52:44 +01:00
if ( memcmp ( s , word , wl ) ! = 0 )
return false ;
2010-04-23 20:29:15 +02:00
return s [ wl ] = = 0 | |
strchr ( WHITESPACE , s [ wl ] ) ;
2010-01-30 01:52:44 +01:00
}
2010-01-19 02:56:37 +01:00
int close_nointr ( int fd ) {
2013-01-25 17:21:20 +01:00
int r ;
2009-11-18 00:42:52 +01:00
2013-01-25 17:21:20 +01:00
assert ( fd > = 0 ) ;
r = close ( fd ) ;
/* Just ignore EINTR; a retry loop is the wrong
* thing to do on Linux .
*
* http : //lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
* https : //bugzilla.gnome.org/show_bug.cgi?id=682819
* http : //utcc.utoronto.ca/~cks/space/blog/unix/CloseEINTR
* https : //sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain
*/
if ( _unlikely_ ( r < 0 & & errno = = EINTR ) )
return 0 ;
else if ( r > = 0 )
return r ;
else
return - errno ;
2009-11-18 00:42:52 +01:00
}
2009-11-19 00:46:47 +01:00
2010-01-28 01:53:15 +01:00
void close_nointr_nofail ( int fd ) {
2013-04-02 16:31:55 +02:00
PROTECT_ERRNO ;
2010-01-28 01:53:15 +01:00
/* like close_nointr() but cannot fail, and guarantees errno
* is unchanged */
assert_se ( close_nointr ( fd ) = = 0 ) ;
}
2010-06-16 21:54:17 +02:00
void close_many ( const int fds [ ] , unsigned n_fd ) {
unsigned i ;
2013-03-24 22:02:05 +01:00
assert ( fds | | n_fd < = 0 ) ;
2010-06-16 21:54:17 +02:00
for ( i = 0 ; i < n_fd ; i + + )
close_nointr_nofail ( fds [ i ] ) ;
}
2013-04-08 22:43:52 +02:00
int unlink_noerrno ( const char * path ) {
PROTECT_ERRNO ;
int r ;
r = unlink ( path ) ;
if ( r < 0 )
return - errno ;
return 0 ;
}
2009-11-19 00:46:47 +01:00
int parse_boolean ( const char * v ) {
assert ( v ) ;
2013-02-12 21:47:37 +01:00
if ( streq ( v , " 1 " ) | | v [ 0 ] = = ' y ' | | v [ 0 ] = = ' Y ' | | v [ 0 ] = = ' t ' | | v [ 0 ] = = ' T ' | | strcaseeq ( v , " on " ) )
2009-11-19 00:46:47 +01:00
return 1 ;
2013-02-12 21:47:37 +01:00
else if ( streq ( v , " 0 " ) | | v [ 0 ] = = ' n ' | | v [ 0 ] = = ' N ' | | v [ 0 ] = = ' f ' | | v [ 0 ] = = ' F ' | | strcaseeq ( v , " off " ) )
2009-11-19 00:46:47 +01:00
return 0 ;
return - EINVAL ;
}
2010-06-17 22:50:35 +02:00
int parse_pid ( const char * s , pid_t * ret_pid ) {
2011-01-22 01:47:37 +01:00
unsigned long ul = 0 ;
2010-06-17 22:50:35 +02:00
pid_t pid ;
int r ;
assert ( s ) ;
assert ( ret_pid ) ;
2012-09-14 10:06:42 +02:00
r = safe_atolu ( s , & ul ) ;
if ( r < 0 )
2010-06-17 22:50:35 +02:00
return r ;
pid = ( pid_t ) ul ;
if ( ( unsigned long ) pid ! = ul )
return - ERANGE ;
if ( pid < = 0 )
return - ERANGE ;
* ret_pid = pid ;
return 0 ;
}
2011-07-22 21:01:15 +02:00
int parse_uid ( const char * s , uid_t * ret_uid ) {
unsigned long ul = 0 ;
uid_t uid ;
int r ;
assert ( s ) ;
assert ( ret_uid ) ;
2012-09-14 10:06:42 +02:00
r = safe_atolu ( s , & ul ) ;
if ( r < 0 )
2011-07-22 21:01:15 +02:00
return r ;
uid = ( uid_t ) ul ;
if ( ( unsigned long ) uid ! = ul )
return - ERANGE ;
* ret_uid = uid ;
return 0 ;
}
2009-11-19 00:46:47 +01:00
int safe_atou ( const char * s , unsigned * ret_u ) {
char * x = NULL ;
2010-01-26 04:18:44 +01:00
unsigned long l ;
2009-11-19 00:46:47 +01:00
assert ( s ) ;
assert ( ret_u ) ;
errno = 0 ;
l = strtoul ( s , & x , 0 ) ;
2012-10-30 10:29:40 +01:00
if ( ! x | | x = = s | | * x | | errno )
2013-03-22 04:24:30 +01:00
return errno > 0 ? - errno : - EINVAL ;
2009-11-19 00:46:47 +01:00
2010-01-26 04:18:44 +01:00
if ( ( unsigned long ) ( unsigned ) l ! = l )
2009-11-19 00:46:47 +01:00
return - ERANGE ;
* ret_u = ( unsigned ) l ;
return 0 ;
}
int safe_atoi ( const char * s , int * ret_i ) {
char * x = NULL ;
2010-01-26 04:18:44 +01:00
long l ;
2009-11-19 00:46:47 +01:00
assert ( s ) ;
assert ( ret_i ) ;
errno = 0 ;
l = strtol ( s , & x , 0 ) ;
2012-10-30 10:29:40 +01:00
if ( ! x | | x = = s | | * x | | errno )
2013-03-22 04:24:30 +01:00
return errno > 0 ? - errno : - EINVAL ;
2009-11-19 00:46:47 +01:00
2010-01-26 04:18:44 +01:00
if ( ( long ) ( int ) l ! = l )
2009-11-19 00:46:47 +01:00
return - ERANGE ;
2010-01-26 04:18:44 +01:00
* ret_i = ( int ) l ;
return 0 ;
}
int safe_atollu ( const char * s , long long unsigned * ret_llu ) {
char * x = NULL ;
unsigned long long l ;
assert ( s ) ;
assert ( ret_llu ) ;
errno = 0 ;
l = strtoull ( s , & x , 0 ) ;
2012-10-30 10:29:40 +01:00
if ( ! x | | x = = s | | * x | | errno )
2010-01-26 04:18:44 +01:00
return errno ? - errno : - EINVAL ;
* ret_llu = l ;
return 0 ;
}
int safe_atolli ( const char * s , long long int * ret_lli ) {
char * x = NULL ;
long long l ;
assert ( s ) ;
assert ( ret_lli ) ;
errno = 0 ;
l = strtoll ( s , & x , 0 ) ;
2012-10-30 10:29:40 +01:00
if ( ! x | | x = = s | | * x | | errno )
2010-01-26 04:18:44 +01:00
return errno ? - errno : - EINVAL ;
* ret_lli = l ;
2009-11-19 00:46:47 +01:00
return 0 ;
}
2009-11-19 02:50:21 +01:00
2013-02-14 21:32:49 +01:00
int safe_atod ( const char * s , double * ret_d ) {
char * x = NULL ;
2013-07-17 02:48:53 +02:00
double d = 0 ;
2013-02-14 21:32:49 +01:00
assert ( s ) ;
assert ( ret_d ) ;
2013-04-25 05:04:02 +02:00
RUN_WITH_LOCALE ( LC_NUMERIC_MASK , " C " ) {
errno = 0 ;
d = strtod ( s , & x ) ;
}
2013-02-14 21:32:49 +01:00
if ( ! x | | x = = s | | * x | | errno )
return errno ? - errno : - EINVAL ;
* ret_d = ( double ) d ;
return 0 ;
}
2014-01-04 02:35:25 +01:00
static size_t strcspn_escaped ( const char * s , const char * reject ) {
bool escaped = false ;
size_t n ;
for ( n = 0 ; s [ n ] ; n + + ) {
if ( escaped )
escaped = false ;
else if ( s [ n ] = = ' \\ ' )
escaped = true ;
else if ( strchr ( reject , s [ n ] ) )
return n ;
}
return n ;
}
2009-11-19 02:50:21 +01:00
/* Split a string into words. */
2014-01-04 02:35:25 +01:00
char * split ( const char * c , size_t * l , const char * separator , bool quoted , char * * state ) {
2009-11-19 02:50:21 +01:00
char * current ;
current = * state ? * state : ( char * ) c ;
if ( ! * current | | * c = = 0 )
return NULL ;
2010-02-13 01:05:12 +01:00
current + = strspn ( current , separator ) ;
2014-01-04 02:35:25 +01:00
if ( ! * current )
2013-11-27 22:37:52 +01:00
return NULL ;
2014-01-04 02:35:25 +01:00
if ( quoted & & strchr ( " \' \" " , * current ) ) {
char quotechar = * ( current + + ) ;
* l = strcspn_escaped ( current , ( char [ ] ) { quotechar , ' \0 ' } ) ;
* state = current + * l + 1 ;
} else if ( quoted ) {
* l = strcspn_escaped ( current , separator ) ;
* state = current + * l ;
2010-01-26 04:18:44 +01:00
} else {
2014-01-04 02:35:25 +01:00
* l = strcspn ( current , separator ) ;
* state = current + * l ;
2010-01-26 04:18:44 +01:00
}
2013-11-28 17:50:02 +01:00
return ( char * ) current ;
2010-01-26 04:18:44 +01:00
}
int get_parent_of_pid ( pid_t pid , pid_t * _ppid ) {
int r ;
2014-01-04 02:35:26 +01:00
_cleanup_free_ char * line = NULL ;
2010-06-19 04:35:52 +02:00
long unsigned ppid ;
2013-04-16 14:50:05 +02:00
const char * p ;
2010-01-26 04:18:44 +01:00
2013-04-16 14:50:05 +02:00
assert ( pid > = 0 ) ;
2010-01-26 04:18:44 +01:00
assert ( _ppid ) ;
2013-04-16 14:50:05 +02:00
if ( pid = = 0 ) {
* _ppid = getppid ( ) ;
return 0 ;
}
2010-01-26 04:18:44 +01:00
2013-04-16 14:50:05 +02:00
p = procfs_file_alloca ( pid , " stat " ) ;
2014-01-04 02:35:26 +01:00
r = read_one_line_file ( p , & line ) ;
if ( r < 0 )
2010-01-26 04:18:44 +01:00
return r ;
/* Let's skip the pid and comm fields. The latter is enclosed
* in ( ) but does not escape any ( ) in its value , so let ' s
* skip over it manually */
2012-09-13 22:30:26 +02:00
p = strrchr ( line , ' ) ' ) ;
if ( ! p )
2010-01-26 04:18:44 +01:00
return - EIO ;
p + + ;
if ( sscanf ( p , " "
" %*c " /* state */
2010-06-19 04:35:52 +02:00
" %lu " , /* ppid */
2010-01-26 04:18:44 +01:00
& ppid ) ! = 1 )
return - EIO ;
2010-06-19 04:35:52 +02:00
if ( ( long unsigned ) ( pid_t ) ppid ! = ppid )
2010-01-26 04:18:44 +01:00
return - ERANGE ;
* _ppid = ( pid_t ) ppid ;
return 0 ;
}
2011-04-16 02:02:54 +02:00
int get_starttime_of_pid ( pid_t pid , unsigned long long * st ) {
2014-01-04 02:35:26 +01:00
int r ;
_cleanup_free_ char * line = NULL ;
2013-04-16 14:50:05 +02:00
const char * p ;
2011-04-16 02:02:54 +02:00
2013-04-16 14:50:05 +02:00
assert ( pid > = 0 ) ;
2011-04-16 02:02:54 +02:00
assert ( st ) ;
2014-01-04 02:35:23 +01:00
p = procfs_file_alloca ( pid , " stat " ) ;
2014-01-04 02:35:26 +01:00
r = read_one_line_file ( p , & line ) ;
if ( r < 0 )
return r ;
2011-04-16 02:02:54 +02:00
/* Let's skip the pid and comm fields. The latter is enclosed
* in ( ) but does not escape any ( ) in its value , so let ' s
* skip over it manually */
2012-09-14 10:06:42 +02:00
p = strrchr ( line , ' ) ' ) ;
if ( ! p )
2011-04-16 02:02:54 +02:00
return - EIO ;
p + + ;
if ( sscanf ( p , " "
" %*c " /* state */
" %*d " /* ppid */
" %*d " /* pgrp */
" %*d " /* session */
" %*d " /* tty_nr */
" %*d " /* tpgid */
" %*u " /* flags */
" %*u " /* minflt */
" %*u " /* cminflt */
" %*u " /* majflt */
" %*u " /* cmajflt */
" %*u " /* utime */
" %*u " /* stime */
" %*d " /* cutime */
" %*d " /* cstime */
" %*d " /* priority */
" %*d " /* nice */
" %*d " /* num_threads */
" %*d " /* itrealvalue */
" %llu " /* starttime */ ,
st ) ! = 1 )
return - EIO ;
return 0 ;
}
2011-06-15 15:35:23 +02:00
int fchmod_umask ( int fd , mode_t m ) {
mode_t u ;
int r ;
u = umask ( 0777 ) ;
r = fchmod ( fd , m & ( ~ u ) ) < 0 ? - errno : 0 ;
umask ( u ) ;
return r ;
}
2010-04-08 03:22:25 +02:00
char * truncate_nl ( char * s ) {
assert ( s ) ;
s [ strcspn ( s , NEWLINE ) ] = 0 ;
return s ;
}
2014-02-15 05:04:50 +01:00
int get_process_state ( pid_t pid ) {
2014-02-14 19:38:50 +01:00
const char * p ;
char state ;
int r ;
_cleanup_free_ char * line = NULL ;
assert ( pid > = 0 ) ;
p = procfs_file_alloca ( pid , " stat " ) ;
r = read_one_line_file ( p , & line ) ;
if ( r < 0 )
return r ;
p = strrchr ( line , ' ) ' ) ;
if ( ! p )
return - EIO ;
p + + ;
if ( sscanf ( p , " %c " , & state ) ! = 1 )
return - EIO ;
return ( unsigned char ) state ;
}
2011-10-07 21:06:39 +02:00
int get_process_comm ( pid_t pid , char * * name ) {
2013-04-16 14:50:05 +02:00
const char * p ;
2013-11-28 17:50:02 +01:00
int r ;
2010-04-08 03:22:25 +02:00
assert ( name ) ;
2013-04-16 14:50:05 +02:00
assert ( pid > = 0 ) ;
2010-04-08 03:22:25 +02:00
2014-01-04 02:35:23 +01:00
p = procfs_file_alloca ( pid , " comm " ) ;
2010-04-08 03:22:25 +02:00
2013-11-28 17:50:02 +01:00
r = read_one_line_file ( p , name ) ;
if ( r = = - ENOENT )
return - ESRCH ;
return r ;
2010-04-08 03:22:25 +02:00
}
2011-10-07 21:06:39 +02:00
int get_process_cmdline ( pid_t pid , size_t max_length , bool comm_fallback , char * * line ) {
2013-04-16 14:50:05 +02:00
_cleanup_fclose_ FILE * f = NULL ;
2013-01-14 18:16:50 +01:00
char * r = NULL , * k ;
2013-04-16 14:50:05 +02:00
const char * p ;
2010-07-05 03:06:02 +02:00
int c ;
assert ( line ) ;
2013-04-16 14:50:05 +02:00
assert ( pid > = 0 ) ;
2010-07-05 03:06:02 +02:00
2014-01-04 02:35:23 +01:00
p = procfs_file_alloca ( pid , " cmdline " ) ;
2010-07-05 03:06:02 +02:00
2013-04-16 14:50:05 +02:00
f = fopen ( p , " re " ) ;
2010-07-05 03:06:02 +02:00
if ( ! f )
return - errno ;
2013-04-16 14:50:05 +02:00
2013-01-14 18:16:50 +01:00
if ( max_length = = 0 ) {
2013-04-16 14:50:05 +02:00
size_t len = 0 , allocated = 0 ;
2013-01-14 18:16:50 +01:00
while ( ( c = getc ( f ) ) ! = EOF ) {
2013-04-16 14:50:05 +02:00
if ( ! GREEDY_REALLOC ( r , allocated , len + 2 ) ) {
2013-01-14 18:16:50 +01:00
free ( r ) ;
return - ENOMEM ;
}
2013-04-16 14:50:05 +02:00
r [ len + + ] = isprint ( c ) ? c : ' ' ;
2013-01-14 18:16:50 +01:00
}
2013-04-16 14:50:05 +02:00
if ( len > 0 )
r [ len - 1 ] = 0 ;
2013-01-14 18:16:50 +01:00
} else {
bool space = false ;
size_t left ;
2013-04-16 14:50:05 +02:00
2013-01-14 18:16:50 +01:00
r = new ( char , max_length ) ;
2013-04-16 14:50:05 +02:00
if ( ! r )
2013-01-14 18:16:50 +01:00
return - ENOMEM ;
2010-07-05 03:06:02 +02:00
2013-01-14 18:16:50 +01:00
k = r ;
left = max_length ;
while ( ( c = getc ( f ) ) ! = EOF ) {
2010-07-05 03:06:02 +02:00
2013-01-14 18:16:50 +01:00
if ( isprint ( c ) ) {
if ( space ) {
if ( left < = 4 )
break ;
* ( k + + ) = ' ' ;
left - - ;
space = false ;
}
2010-07-05 03:06:02 +02:00
if ( left < = 4 )
break ;
2013-01-14 18:16:50 +01:00
* ( k + + ) = ( char ) c ;
2010-07-06 20:15:08 +02:00
left - - ;
2013-01-14 18:16:50 +01:00
} else
space = true ;
}
2010-07-05 03:06:02 +02:00
2013-01-14 18:16:50 +01:00
if ( left < = 4 ) {
size_t n = MIN ( left - 1 , 3U ) ;
memcpy ( k , " ... " , n ) ;
k [ n ] = 0 ;
} else
* k = 0 ;
2010-07-05 03:06:02 +02:00
}
2010-07-12 18:16:44 +02:00
/* Kernel threads have no argv[] */
2013-01-14 18:16:50 +01:00
if ( r = = NULL | | r [ 0 ] = = 0 ) {
2013-10-12 18:15:49 +02:00
_cleanup_free_ char * t = NULL ;
2010-07-12 18:16:44 +02:00
int h ;
free ( r ) ;
2011-10-07 21:06:39 +02:00
if ( ! comm_fallback )
return - ENOENT ;
h = get_process_comm ( pid , & t ) ;
if ( h < 0 )
2010-07-12 18:16:44 +02:00
return h ;
2012-07-13 13:41:01 +02:00
r = strjoin ( " [ " , t , " ] " , NULL ) ;
2011-10-07 21:06:39 +02:00
if ( ! r )
2010-07-12 18:16:44 +02:00
return - ENOMEM ;
}
2010-07-08 21:01:42 +02:00
2010-07-05 03:06:02 +02:00
* line = r ;
return 0 ;
}
2012-01-22 18:18:51 +01:00
int is_kernel_thread ( pid_t pid ) {
2013-04-16 14:50:05 +02:00
const char * p ;
2012-01-22 18:18:51 +01:00
size_t count ;
char c ;
bool eof ;
FILE * f ;
if ( pid = = 0 )
return 0 ;
2013-04-16 14:50:05 +02:00
assert ( pid > 0 ) ;
2012-01-22 18:18:51 +01:00
2013-04-16 14:50:05 +02:00
p = procfs_file_alloca ( pid , " cmdline " ) ;
f = fopen ( p , " re " ) ;
2012-01-22 18:18:51 +01:00
if ( ! f )
return - errno ;
count = fread ( & c , 1 , 1 , f ) ;
eof = feof ( f ) ;
fclose ( f ) ;
/* Kernel threads have an empty cmdline */
if ( count < = 0 )
return eof ? 1 : - errno ;
return 0 ;
}
2013-07-16 03:10:56 +02:00
int get_process_capeff ( pid_t pid , char * * capeff ) {
const char * p ;
assert ( capeff ) ;
assert ( pid > = 0 ) ;
2014-01-04 02:35:23 +01:00
p = procfs_file_alloca ( pid , " status " ) ;
2013-07-16 03:10:56 +02:00
2013-09-14 01:41:52 +02:00
return get_status_field ( p , " \n CapEff: " , capeff ) ;
2013-07-16 03:10:56 +02:00
}
2013-04-16 14:50:05 +02:00
2011-10-07 21:06:39 +02:00
int get_process_exe ( pid_t pid , char * * name ) {
2013-04-16 14:50:05 +02:00
const char * p ;
2013-06-29 19:11:44 +02:00
char * d ;
int r ;
2011-10-07 21:06:39 +02:00
2013-04-16 14:50:05 +02:00
assert ( pid > = 0 ) ;
2011-10-07 21:06:39 +02:00
assert ( name ) ;
2014-01-04 02:35:23 +01:00
p = procfs_file_alloca ( pid , " exe " ) ;
2011-10-07 21:06:39 +02:00
2013-06-29 19:11:44 +02:00
r = readlink_malloc ( p , name ) ;
if ( r < 0 )
2013-11-28 17:50:02 +01:00
return r = = - ENOENT ? - ESRCH : r ;
2013-06-29 19:11:44 +02:00
d = endswith ( * name , " (deleted) " ) ;
if ( d )
* d = ' \0 ' ;
return 0 ;
2011-10-07 21:06:39 +02:00
}
2012-09-18 01:53:15 +02:00
static int get_process_id ( pid_t pid , const char * field , uid_t * uid ) {
2013-02-11 05:09:29 +01:00
_cleanup_fclose_ FILE * f = NULL ;
2013-02-13 22:02:40 +01:00
char line [ LINE_MAX ] ;
2013-04-16 14:50:05 +02:00
const char * p ;
2012-01-10 04:20:55 +01:00
2013-02-11 05:09:29 +01:00
assert ( field ) ;
2012-01-10 04:20:55 +01:00
assert ( uid ) ;
if ( pid = = 0 )
return getuid ( ) ;
2013-04-16 14:50:05 +02:00
p = procfs_file_alloca ( pid , " status " ) ;
f = fopen ( p , " re " ) ;
2012-01-10 04:20:55 +01:00
if ( ! f )
return - errno ;
2013-02-13 22:02:40 +01:00
FOREACH_LINE ( line , f , return - errno ) {
2013-02-11 05:09:29 +01:00
char * l ;
2012-01-10 04:20:55 +01:00
l = strstrip ( line ) ;
2012-09-18 01:53:15 +02:00
if ( startswith ( l , field ) ) {
l + = strlen ( field ) ;
2012-01-10 04:20:55 +01:00
l + = strspn ( l , WHITESPACE ) ;
l [ strcspn ( l , WHITESPACE ) ] = 0 ;
2013-02-11 05:09:29 +01:00
return parse_uid ( l , uid ) ;
2012-01-10 04:20:55 +01:00
}
}
2013-02-11 05:09:29 +01:00
return - EIO ;
2012-01-10 04:20:55 +01:00
}
2012-09-18 01:53:15 +02:00
int get_process_uid ( pid_t pid , uid_t * uid ) {
return get_process_id ( pid , " Uid: " , uid ) ;
}
int get_process_gid ( pid_t pid , gid_t * gid ) {
2013-04-16 14:50:05 +02:00
assert_cc ( sizeof ( uid_t ) = = sizeof ( gid_t ) ) ;
2012-09-18 01:53:15 +02:00
return get_process_id ( pid , " Gid: " , gid ) ;
}
2010-07-08 04:09:59 +02:00
char * strnappend ( const char * s , const char * suffix , size_t b ) {
size_t a ;
2010-01-26 07:02:51 +01:00
char * r ;
2010-07-08 04:09:59 +02:00
if ( ! s & & ! suffix )
return strdup ( " " ) ;
if ( ! s )
return strndup ( suffix , b ) ;
if ( ! suffix )
return strdup ( s ) ;
2010-01-26 07:02:51 +01:00
assert ( s ) ;
assert ( suffix ) ;
a = strlen ( s ) ;
2012-09-20 17:53:03 +02:00
if ( b > ( ( size_t ) - 1 ) - a )
2012-09-20 11:08:27 +02:00
return NULL ;
2010-01-26 07:02:51 +01:00
2012-09-20 11:08:27 +02:00
r = new ( char , a + b + 1 ) ;
if ( ! r )
2010-01-26 07:02:51 +01:00
return NULL ;
memcpy ( r , s , a ) ;
memcpy ( r + a , suffix , b ) ;
r [ a + b ] = 0 ;
return r ;
}
2010-01-26 21:39:06 +01:00
2010-07-08 04:09:59 +02:00
char * strappend ( const char * s , const char * suffix ) {
return strnappend ( s , suffix , suffix ? strlen ( suffix ) : 0 ) ;
}
2014-02-11 16:45:35 +01:00
int readlink_malloc ( const char * p , char * * ret ) {
2010-01-26 21:39:06 +01:00
size_t l = 100 ;
2014-02-11 16:45:35 +01:00
int r ;
2010-01-26 21:39:06 +01:00
assert ( p ) ;
2014-02-11 16:45:35 +01:00
assert ( ret ) ;
2010-01-26 21:39:06 +01:00
for ( ; ; ) {
char * c ;
ssize_t n ;
2014-02-11 16:45:35 +01:00
c = new ( char , l ) ;
if ( ! c )
2010-01-26 21:39:06 +01:00
return - ENOMEM ;
2014-02-11 16:45:35 +01:00
n = readlink ( p , c , l - 1 ) ;
if ( n < 0 ) {
r = - errno ;
2010-01-26 21:39:06 +01:00
free ( c ) ;
2014-02-11 16:45:35 +01:00
return r ;
2010-01-26 21:39:06 +01:00
}
if ( ( size_t ) n < l - 1 ) {
c [ n ] = 0 ;
2014-02-11 16:45:35 +01:00
* ret = c ;
2010-01-26 21:39:06 +01:00
return 0 ;
}
free ( c ) ;
l * = 2 ;
}
}
2010-06-16 01:56:00 +02:00
int readlink_and_make_absolute ( const char * p , char * * r ) {
2013-05-31 02:28:09 +02:00
_cleanup_free_ char * target = NULL ;
char * k ;
2010-06-16 01:56:00 +02:00
int j ;
assert ( p ) ;
assert ( r ) ;
2013-05-31 02:28:09 +02:00
j = readlink_malloc ( p , & target ) ;
if ( j < 0 )
2010-06-16 01:56:00 +02:00
return j ;
k = file_in_same_dir ( p , target ) ;
if ( ! k )
return - ENOMEM ;
* r = k ;
return 0 ;
}
2011-07-22 04:21:18 +02:00
int readlink_and_canonicalize ( const char * p , char * * r ) {
char * t , * s ;
int j ;
assert ( p ) ;
assert ( r ) ;
j = readlink_and_make_absolute ( p , & t ) ;
if ( j < 0 )
return j ;
s = canonicalize_file_name ( t ) ;
if ( s ) {
free ( t ) ;
* r = s ;
} else
* r = t ;
path_kill_slashes ( * r ) ;
return 0 ;
}
2010-01-27 06:19:28 +01:00
int reset_all_signal_handlers ( void ) {
int sig ;
for ( sig = 1 ; sig < _NSIG ; sig + + ) {
2013-03-25 00:59:00 +01:00
struct sigaction sa = {
. sa_handler = SIG_DFL ,
. sa_flags = SA_RESTART ,
} ;
2010-01-27 06:19:28 +01:00
if ( sig = = SIGKILL | | sig = = SIGSTOP )
continue ;
/* On Linux the first two RT signals are reserved by
* glibc , and sigaction ( ) will return EINVAL for them . */
if ( ( sigaction ( sig , & sa , NULL ) < 0 ) )
if ( errno ! = EINVAL )
return - errno ;
}
2010-03-31 16:29:55 +02:00
return 0 ;
2010-01-27 06:19:28 +01:00
}
2010-01-27 22:37:50 +01:00
char * strstrip ( char * s ) {
2011-08-01 01:18:33 +02:00
char * e ;
2010-01-27 22:37:50 +01:00
/* Drops trailing whitespace. Modifies the string in
* place . Returns pointer to first non - space character */
s + = strspn ( s , WHITESPACE ) ;
2011-08-01 01:18:33 +02:00
for ( e = strchr ( s , 0 ) ; e > s ; e - - )
if ( ! strchr ( WHITESPACE , e [ - 1 ] ) )
break ;
2010-01-27 22:37:50 +01:00
2011-08-01 01:18:33 +02:00
* e = 0 ;
2010-01-27 22:37:50 +01:00
return s ;
}
2010-04-07 20:27:05 +02:00
char * delete_chars ( char * s , const char * bad ) {
char * f , * t ;
/* Drops all whitespace, regardless where in the string */
for ( f = s , t = s ; * f ; f + + ) {
if ( strchr ( bad , * f ) )
continue ;
* ( t + + ) = * f ;
}
* t = 0 ;
return s ;
}
2011-08-20 00:20:41 +02:00
bool in_charset ( const char * s , const char * charset ) {
const char * i ;
assert ( s ) ;
assert ( charset ) ;
for ( i = s ; * i ; i + + )
if ( ! strchr ( charset , * i ) )
return false ;
return true ;
}
2010-01-27 22:37:50 +01:00
char * file_in_same_dir ( const char * path , const char * filename ) {
char * e , * r ;
size_t k ;
assert ( path ) ;
assert ( filename ) ;
/* This removes the last component of path and appends
* filename , unless the latter is absolute anyway or the
* former isn ' t */
if ( path_is_absolute ( filename ) )
return strdup ( filename ) ;
if ( ! ( e = strrchr ( path , ' / ' ) ) )
return strdup ( filename ) ;
k = strlen ( filename ) ;
if ( ! ( r = new ( char , e - path + 1 + k + 1 ) ) )
return NULL ;
memcpy ( r , path , e - path + 1 ) ;
memcpy ( r + ( e - path ) + 1 , filename , k + 1 ) ;
return r ;
}
2010-01-28 06:45:04 +01:00
2010-06-18 21:33:15 +02:00
int rmdir_parents ( const char * path , const char * stop ) {
size_t l ;
int r = 0 ;
assert ( path ) ;
assert ( stop ) ;
l = strlen ( path ) ;
/* Skip trailing slashes */
while ( l > 0 & & path [ l - 1 ] = = ' / ' )
l - - ;
while ( l > 0 ) {
char * t ;
/* Skip last component */
while ( l > 0 & & path [ l - 1 ] ! = ' / ' )
l - - ;
/* Skip trailing slashes */
while ( l > 0 & & path [ l - 1 ] = = ' / ' )
l - - ;
if ( l < = 0 )
break ;
if ( ! ( t = strndup ( path , l ) ) )
return - ENOMEM ;
if ( path_startswith ( stop , t ) ) {
free ( t ) ;
return 0 ;
}
r = rmdir ( t ) ;
free ( t ) ;
if ( r < 0 )
if ( errno ! = ENOENT )
return - errno ;
}
return 0 ;
}
2010-01-28 06:45:04 +01:00
char hexchar ( int x ) {
static const char table [ 16 ] = " 0123456789abcdef " ;
return table [ x & 15 ] ;
}
2010-01-29 01:48:57 +01:00
int unhexchar ( char c ) {
if ( c > = ' 0 ' & & c < = ' 9 ' )
return c - ' 0 ' ;
if ( c > = ' a ' & & c < = ' f ' )
2010-02-01 03:33:24 +01:00
return c - ' a ' + 10 ;
2010-01-29 01:48:57 +01:00
if ( c > = ' A ' & & c < = ' F ' )
2010-02-01 03:33:24 +01:00
return c - ' A ' + 10 ;
2010-01-29 01:48:57 +01:00
return - 1 ;
}
2013-03-19 20:01:18 +01:00
char * hexmem ( const void * p , size_t l ) {
char * r , * z ;
const uint8_t * x ;
z = r = malloc ( l * 2 + 1 ) ;
if ( ! r )
return NULL ;
for ( x = p ; x < ( const uint8_t * ) p + l ; x + + ) {
* ( z + + ) = hexchar ( * x > > 4 ) ;
* ( z + + ) = hexchar ( * x & 15 ) ;
}
* z = 0 ;
return r ;
}
2013-03-30 15:21:06 +01:00
void * unhexmem ( const char * p , size_t l ) {
uint8_t * r , * z ;
const char * x ;
assert ( p ) ;
z = r = malloc ( ( l + 1 ) / 2 + 1 ) ;
if ( ! r )
return NULL ;
for ( x = p ; x < p + l ; x + = 2 ) {
int a , b ;
a = unhexchar ( x [ 0 ] ) ;
if ( x + 1 < p + l )
b = unhexchar ( x [ 1 ] ) ;
else
b = 0 ;
* ( z + + ) = ( uint8_t ) a < < 4 | ( uint8_t ) b ;
}
* z = 0 ;
return r ;
}
2010-01-29 01:48:57 +01:00
char octchar ( int x ) {
return ' 0 ' + ( x & 7 ) ;
}
int unoctchar ( char c ) {
if ( c > = ' 0 ' & & c < = ' 7 ' )
return c - ' 0 ' ;
return - 1 ;
}
2010-04-06 02:41:03 +02:00
char decchar ( int x ) {
return ' 0 ' + ( x % 10 ) ;
}
int undecchar ( char c ) {
if ( c > = ' 0 ' & & c < = ' 9 ' )
return c - ' 0 ' ;
return - 1 ;
}
2010-01-29 01:48:57 +01:00
char * cescape ( const char * s ) {
char * r , * t ;
const char * f ;
assert ( s ) ;
/* Does C style string escaping. */
2012-04-16 16:47:33 +02:00
r = new ( char , strlen ( s ) * 4 + 1 ) ;
if ( ! r )
2010-01-29 01:48:57 +01:00
return NULL ;
for ( f = s , t = r ; * f ; f + + )
switch ( * f ) {
case ' \a ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' a ' ;
break ;
case ' \b ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' b ' ;
break ;
case ' \f ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' f ' ;
break ;
case ' \n ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' n ' ;
break ;
case ' \r ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' r ' ;
break ;
case ' \t ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' t ' ;
break ;
case ' \v ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' v ' ;
break ;
case ' \\ ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' \\ ' ;
break ;
case ' " ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' " ' ;
break ;
case ' \' ' :
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' \' ' ;
break ;
default :
/* For special chars we prefer octal over
* hexadecimal encoding , simply because glib ' s
* g_strescape ( ) does the same */
if ( ( * f < ' ' ) | | ( * f > = 127 ) ) {
* ( t + + ) = ' \\ ' ;
* ( t + + ) = octchar ( ( unsigned char ) * f > > 6 ) ;
* ( t + + ) = octchar ( ( unsigned char ) * f > > 3 ) ;
* ( t + + ) = octchar ( ( unsigned char ) * f ) ;
} else
* ( t + + ) = * f ;
break ;
}
* t = 0 ;
return r ;
}
2012-08-09 16:49:28 +02:00
char * cunescape_length_with_prefix ( const char * s , size_t length , const char * prefix ) {
2010-01-29 01:48:57 +01:00
char * r , * t ;
const char * f ;
2012-08-09 16:49:28 +02:00
size_t pl ;
2010-01-29 01:48:57 +01:00
assert ( s ) ;
2012-08-09 16:49:28 +02:00
/* Undoes C style string escaping, and optionally prefixes it. */
pl = prefix ? strlen ( prefix ) : 0 ;
2010-01-29 01:48:57 +01:00
2012-08-09 16:49:28 +02:00
r = new ( char , pl + length + 1 ) ;
2012-03-12 22:22:16 +01:00
if ( ! r )
2010-01-29 01:48:57 +01:00
return r ;
2012-08-09 16:49:28 +02:00
if ( prefix )
memcpy ( r , prefix , pl ) ;
for ( f = s , t = r + pl ; f < s + length ; f + + ) {
2010-01-29 01:48:57 +01:00
if ( * f ! = ' \\ ' ) {
* ( t + + ) = * f ;
continue ;
}
f + + ;
switch ( * f ) {
case ' a ' :
* ( t + + ) = ' \a ' ;
break ;
case ' b ' :
* ( t + + ) = ' \b ' ;
break ;
case ' f ' :
* ( t + + ) = ' \f ' ;
break ;
case ' n ' :
* ( t + + ) = ' \n ' ;
break ;
case ' r ' :
* ( t + + ) = ' \r ' ;
break ;
case ' t ' :
* ( t + + ) = ' \t ' ;
break ;
case ' v ' :
* ( t + + ) = ' \v ' ;
break ;
case ' \\ ' :
* ( t + + ) = ' \\ ' ;
break ;
case ' " ' :
* ( t + + ) = ' " ' ;
break ;
case ' \' ' :
* ( t + + ) = ' \' ' ;
break ;
2010-07-07 22:28:51 +02:00
case ' s ' :
/* This is an extension of the XDG syntax files */
* ( t + + ) = ' ' ;
break ;
2010-01-29 01:48:57 +01:00
case ' x ' : {
/* hexadecimal encoding */
int a , b ;
2012-03-12 22:22:16 +01:00
a = unhexchar ( f [ 1 ] ) ;
b = unhexchar ( f [ 2 ] ) ;
if ( a < 0 | | b < 0 ) {
2010-01-29 01:48:57 +01:00
/* Invalid escape code, let's take it literal then */
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' x ' ;
} else {
* ( t + + ) = ( char ) ( ( a < < 4 ) | b ) ;
f + = 2 ;
}
break ;
}
case ' 0 ' :
case ' 1 ' :
case ' 2 ' :
case ' 3 ' :
case ' 4 ' :
case ' 5 ' :
case ' 6 ' :
case ' 7 ' : {
/* octal encoding */
int a , b , c ;
2012-03-12 22:22:16 +01:00
a = unoctchar ( f [ 0 ] ) ;
b = unoctchar ( f [ 1 ] ) ;
c = unoctchar ( f [ 2 ] ) ;
if ( a < 0 | | b < 0 | | c < 0 ) {
2010-01-29 01:48:57 +01:00
/* Invalid escape code, let's take it literal then */
* ( t + + ) = ' \\ ' ;
* ( t + + ) = f [ 0 ] ;
} else {
* ( t + + ) = ( char ) ( ( a < < 6 ) | ( b < < 3 ) | c ) ;
f + = 2 ;
}
break ;
}
case 0 :
/* premature end of string.*/
* ( t + + ) = ' \\ ' ;
goto finish ;
default :
/* Invalid escape code, let's take it literal then */
* ( t + + ) = ' \\ ' ;
2010-07-07 20:57:10 +02:00
* ( t + + ) = * f ;
2010-01-29 01:48:57 +01:00
break ;
}
}
finish :
* t = 0 ;
return r ;
}
2012-08-09 16:49:28 +02:00
char * cunescape_length ( const char * s , size_t length ) {
return cunescape_length_with_prefix ( s , length , NULL ) ;
}
2010-07-07 20:58:02 +02:00
char * cunescape ( const char * s ) {
2012-08-09 16:49:28 +02:00
assert ( s ) ;
2010-07-07 20:58:02 +02:00
return cunescape_length ( s , strlen ( s ) ) ;
}
2010-01-29 01:48:57 +01:00
char * xescape ( const char * s , const char * bad ) {
char * r , * t ;
const char * f ;
/* Escapes all chars in bad, in addition to \ and all special
* chars , in \ xFF style escaping . May be reversed with
* cunescape . */
2012-09-27 23:27:10 +02:00
r = new ( char , strlen ( s ) * 4 + 1 ) ;
if ( ! r )
2010-01-29 01:48:57 +01:00
return NULL ;
for ( f = s , t = r ; * f ; f + + ) {
2010-03-31 20:08:05 +02:00
if ( ( * f < ' ' ) | | ( * f > = 127 ) | |
( * f = = ' \\ ' ) | | strchr ( bad , * f ) ) {
2010-01-29 01:48:57 +01:00
* ( t + + ) = ' \\ ' ;
* ( t + + ) = ' x ' ;
* ( t + + ) = hexchar ( * f > > 4 ) ;
* ( t + + ) = hexchar ( * f ) ;
} else
* ( t + + ) = * f ;
}
* t = 0 ;
return r ;
}
2010-04-06 17:14:04 +02:00
char * ascii_strlower ( char * t ) {
2010-01-29 01:48:57 +01:00
char * p ;
2010-04-06 17:14:04 +02:00
assert ( t ) ;
2010-01-29 01:48:57 +01:00
2010-04-06 17:14:04 +02:00
for ( p = t ; * p ; p + + )
2010-01-29 01:48:57 +01:00
if ( * p > = ' A ' & & * p < = ' Z ' )
* p = * p - ' A ' + ' a ' ;
2010-04-06 17:14:04 +02:00
return t ;
2010-01-29 01:48:57 +01:00
}
2010-01-30 01:52:32 +01:00
2013-05-03 04:51:50 +02:00
_pure_ static bool ignore_file_allow_backup ( const char * filename ) {
2010-02-14 01:07:01 +01:00
assert ( filename ) ;
return
filename [ 0 ] = = ' . ' | |
2010-04-30 02:16:55 +02:00
streq ( filename , " lost+found " ) | |
2010-10-18 22:39:17 +02:00
streq ( filename , " aquota.user " ) | |
streq ( filename , " aquota.group " ) | |
2010-02-14 01:07:01 +01:00
endswith ( filename , " .rpmnew " ) | |
endswith ( filename , " .rpmsave " ) | |
endswith ( filename , " .rpmorig " ) | |
endswith ( filename , " .dpkg-old " ) | |
endswith ( filename , " .dpkg-new " ) | |
endswith ( filename , " .swp " ) ;
}
2012-08-21 02:13:21 +02:00
bool ignore_file ( const char * filename ) {
assert ( filename ) ;
if ( endswith ( filename , " ~ " ) )
return false ;
return ignore_file_allow_backup ( filename ) ;
}
2010-04-06 21:53:02 +02:00
int fd_nonblock ( int fd , bool nonblock ) {
int flags ;
assert ( fd > = 0 ) ;
if ( ( flags = fcntl ( fd , F_GETFL , 0 ) ) < 0 )
return - errno ;
if ( nonblock )
flags | = O_NONBLOCK ;
else
flags & = ~ O_NONBLOCK ;
if ( fcntl ( fd , F_SETFL , flags ) < 0 )
return - errno ;
return 0 ;
}
int fd_cloexec ( int fd , bool cloexec ) {
int flags ;
assert ( fd > = 0 ) ;
if ( ( flags = fcntl ( fd , F_GETFD , 0 ) ) < 0 )
return - errno ;
if ( cloexec )
flags | = FD_CLOEXEC ;
else
flags & = ~ FD_CLOEXEC ;
if ( fcntl ( fd , F_SETFD , flags ) < 0 )
return - errno ;
return 0 ;
}
2013-05-03 04:51:50 +02:00
_pure_ static bool fd_in_set ( int fd , const int fdset [ ] , unsigned n_fdset ) {
2012-03-13 02:29:27 +01:00
unsigned i ;
assert ( n_fdset = = 0 | | fdset ) ;
for ( i = 0 ; i < n_fdset ; i + + )
if ( fdset [ i ] = = fd )
return true ;
return false ;
}
2010-04-06 23:35:59 +02:00
int close_all_fds ( const int except [ ] , unsigned n_except ) {
DIR * d ;
struct dirent * de ;
int r = 0 ;
2012-03-13 02:29:27 +01:00
assert ( n_except = = 0 | | except ) ;
d = opendir ( " /proc/self/fd " ) ;
if ( ! d ) {
int fd ;
struct rlimit rl ;
/* When /proc isn't available (for example in chroots)
* the fallback is brute forcing through the fd
* table */
assert_se ( getrlimit ( RLIMIT_NOFILE , & rl ) > = 0 ) ;
for ( fd = 3 ; fd < ( int ) rl . rlim_max ; fd + + ) {
if ( fd_in_set ( fd , except , n_except ) )
continue ;
if ( close_nointr ( fd ) < 0 )
if ( errno ! = EBADF & & r = = 0 )
r = - errno ;
}
return r ;
}
2010-04-06 23:35:59 +02:00
while ( ( de = readdir ( d ) ) ) {
2010-04-07 16:39:07 +02:00
int fd = - 1 ;
2010-04-06 23:35:59 +02:00
2010-04-21 03:27:44 +02:00
if ( ignore_file ( de - > d_name ) )
2010-04-06 23:35:59 +02:00
continue ;
2011-03-11 00:52:13 +01:00
if ( safe_atoi ( de - > d_name , & fd ) < 0 )
/* Let's better ignore this, just in case */
continue ;
2010-04-06 23:35:59 +02:00
if ( fd < 3 )
continue ;
if ( fd = = dirfd ( d ) )
continue ;
2012-03-13 02:29:27 +01:00
if ( fd_in_set ( fd , except , n_except ) )
continue ;
2010-04-06 23:35:59 +02:00
2011-03-11 00:52:13 +01:00
if ( close_nointr ( fd ) < 0 ) {
2010-04-07 00:09:59 +02:00
/* Valgrind has its own FD and doesn't want to have it closed */
2011-03-11 00:52:13 +01:00
if ( errno ! = EBADF & & r = = 0 )
r = - errno ;
2010-04-07 00:09:59 +02:00
}
2010-04-06 23:35:59 +02:00
}
closedir ( d ) ;
return r ;
}
2010-04-07 20:27:19 +02:00
bool chars_intersect ( const char * a , const char * b ) {
const char * p ;
/* Returns true if any of the chars in a are in b. */
for ( p = a ; * p ; p + + )
if ( strchr ( b , * p ) )
return true ;
return false ;
}
2010-04-10 17:41:34 +02:00
bool fstype_is_network ( const char * fstype ) {
2012-09-14 10:24:27 +02:00
static const char table [ ] =
" cifs \0 "
" smbfs \0 "
" ncpfs \0 "
2013-07-15 18:33:57 +02:00
" ncp \0 "
2012-09-14 10:24:27 +02:00
" nfs \0 "
" nfs4 \0 "
" gfs \0 "
2012-09-14 10:36:50 +02:00
" gfs2 \0 " ;
2010-04-10 17:41:34 +02:00
2012-09-14 10:24:27 +02:00
return nulstr_contains ( table , fstype ) ;
2010-04-10 17:41:34 +02:00
}
2010-04-10 23:36:43 +02:00
int chvt ( int vt ) {
2012-09-14 10:24:27 +02:00
_cleanup_close_ int fd ;
2010-04-10 23:36:43 +02:00
2012-09-14 10:24:27 +02:00
fd = open_terminal ( " /dev/tty0 " , O_RDWR | O_NOCTTY | O_CLOEXEC ) ;
if ( fd < 0 )
2010-04-10 23:36:43 +02:00
return - errno ;
if ( vt < 0 ) {
int tiocl [ 2 ] = {
TIOCL_GETKMSGREDIRECT ,
0
} ;
2012-09-14 10:24:27 +02:00
if ( ioctl ( fd , TIOCLINUX , tiocl ) < 0 )
return - errno ;
2010-04-10 23:36:43 +02:00
vt = tiocl [ 0 ] < = 0 ? 1 : tiocl [ 0 ] ;
}
if ( ioctl ( fd , VT_ACTIVATE , vt ) < 0 )
2012-09-14 10:24:27 +02:00
return - errno ;
2010-04-10 23:36:43 +02:00
2012-09-14 10:24:27 +02:00
return 0 ;
2010-04-10 23:36:43 +02:00
}
2012-01-22 18:21:15 +01:00
int read_one_char ( FILE * f , char * ret , usec_t t , bool * need_nl ) {
2010-04-13 02:06:27 +02:00
struct termios old_termios , new_termios ;
char c ;
2011-04-07 18:48:50 +02:00
char line [ LINE_MAX ] ;
2010-04-13 02:06:27 +02:00
assert ( f ) ;
assert ( ret ) ;
if ( tcgetattr ( fileno ( f ) , & old_termios ) > = 0 ) {
new_termios = old_termios ;
new_termios . c_lflag & = ~ ICANON ;
new_termios . c_cc [ VMIN ] = 1 ;
new_termios . c_cc [ VTIME ] = 0 ;
if ( tcsetattr ( fileno ( f ) , TCSADRAIN , & new_termios ) > = 0 ) {
size_t k ;
2012-01-22 18:21:15 +01:00
if ( t ! = ( usec_t ) - 1 ) {
if ( fd_wait_for_event ( fileno ( f ) , POLLIN , t ) < = 0 ) {
tcsetattr ( fileno ( f ) , TCSADRAIN , & old_termios ) ;
return - ETIMEDOUT ;
}
}
2010-04-13 02:06:27 +02:00
k = fread ( & c , 1 , 1 , f ) ;
tcsetattr ( fileno ( f ) , TCSADRAIN , & old_termios ) ;
if ( k < = 0 )
return - EIO ;
if ( need_nl )
* need_nl = c ! = ' \n ' ;
* ret = c ;
return 0 ;
}
}
2012-01-22 18:21:15 +01:00
if ( t ! = ( usec_t ) - 1 )
if ( fd_wait_for_event ( fileno ( f ) , POLLIN , t ) < = 0 )
return - ETIMEDOUT ;
if ( ! fgets ( line , sizeof ( line ) , f ) )
2010-04-13 02:06:27 +02:00
return - EIO ;
truncate_nl ( line ) ;
if ( strlen ( line ) ! = 1 )
return - EBADMSG ;
if ( need_nl )
* need_nl = false ;
* ret = line [ 0 ] ;
return 0 ;
}
int ask ( char * ret , const char * replies , const char * text , . . . ) {
2010-09-17 02:10:08 +02:00
2010-04-13 02:06:27 +02:00
assert ( ret ) ;
assert ( replies ) ;
assert ( text ) ;
for ( ; ; ) {
va_list ap ;
char c ;
int r ;
bool need_nl = true ;
2012-10-18 23:59:41 +02:00
if ( on_tty ( ) )
2012-01-13 21:56:09 +01:00
fputs ( ANSI_HIGHLIGHT_ON , stdout ) ;
2010-04-23 20:51:06 +02:00
2010-04-13 02:06:27 +02:00
va_start ( ap , text ) ;
vprintf ( text , ap ) ;
va_end ( ap ) ;
2012-10-18 23:59:41 +02:00
if ( on_tty ( ) )
2012-01-13 21:56:09 +01:00
fputs ( ANSI_HIGHLIGHT_OFF , stdout ) ;
2010-04-23 20:51:06 +02:00
2010-04-13 02:06:27 +02:00
fflush ( stdout ) ;
2012-01-22 18:21:15 +01:00
r = read_one_char ( stdin , & c , ( usec_t ) - 1 , & need_nl ) ;
if ( r < 0 ) {
2010-04-13 02:06:27 +02:00
if ( r = = - EBADMSG ) {
puts ( " Bad input, please try again. " ) ;
continue ;
}
putchar ( ' \n ' ) ;
return r ;
}
if ( need_nl )
putchar ( ' \n ' ) ;
if ( strchr ( replies , c ) ) {
* ret = c ;
return 0 ;
}
puts ( " Read unexpected character, please try again. " ) ;
}
}
2012-01-29 21:55:51 +01:00
int reset_terminal_fd ( int fd , bool switch_to_text ) {
2010-04-13 02:06:27 +02:00
struct termios termios ;
int r = 0 ;
2010-07-12 21:40:43 +02:00
/* Set terminal to some sane defaults */
2010-04-13 02:06:27 +02:00
assert ( fd > = 0 ) ;
2010-09-01 00:10:41 +02:00
/* We leave locked terminal attributes untouched, so that
* Plymouth may set whatever it wants to set , and we don ' t
* interfere with that . */
2010-07-12 21:40:43 +02:00
/* Disable exclusive mode, just in case */
ioctl ( fd , TIOCNXCL ) ;
2012-01-06 01:32:34 +01:00
/* Switch to text mode */
2012-01-29 21:55:51 +01:00
if ( switch_to_text )
ioctl ( fd , KDSETMODE , KD_TEXT ) ;
2012-01-06 01:32:34 +01:00
2010-07-12 21:40:43 +02:00
/* Enable console unicode mode */
2012-01-06 01:28:30 +01:00
ioctl ( fd , KDSKBMODE , K_UNICODE ) ;
2010-04-13 02:06:27 +02:00
if ( tcgetattr ( fd , & termios ) < 0 ) {
r = - errno ;
goto finish ;
}
2010-04-13 18:51:22 +02:00
/* We only reset the stuff that matters to the software. How
* hardware is set up we don ' t touch assuming that somebody
* else will do that for us */
termios . c_iflag & = ~ ( IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC ) ;
2010-04-13 02:06:27 +02:00
termios . c_iflag | = ICRNL | IMAXBEL | IUTF8 ;
termios . c_oflag | = ONLCR ;
termios . c_cflag | = CREAD ;
termios . c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE ;
termios . c_cc [ VINTR ] = 03 ; /* ^C */
termios . c_cc [ VQUIT ] = 034 ; /* ^\ */
termios . c_cc [ VERASE ] = 0177 ;
termios . c_cc [ VKILL ] = 025 ; /* ^X */
termios . c_cc [ VEOF ] = 04 ; /* ^D */
termios . c_cc [ VSTART ] = 021 ; /* ^Q */
termios . c_cc [ VSTOP ] = 023 ; /* ^S */
termios . c_cc [ VSUSP ] = 032 ; /* ^Z */
termios . c_cc [ VLNEXT ] = 026 ; /* ^V */
termios . c_cc [ VWERASE ] = 027 ; /* ^W */
termios . c_cc [ VREPRINT ] = 022 ; /* ^R */
2010-04-13 18:51:22 +02:00
termios . c_cc [ VEOL ] = 0 ;
termios . c_cc [ VEOL2 ] = 0 ;
2010-04-13 02:06:27 +02:00
termios . c_cc [ VTIME ] = 0 ;
termios . c_cc [ VMIN ] = 1 ;
if ( tcsetattr ( fd , TCSANOW , & termios ) < 0 )
r = - errno ;
finish :
/* Just in case, flush all crap out */
tcflush ( fd , TCIOFLUSH ) ;
return r ;
}
2011-05-18 01:07:31 +02:00
int reset_terminal ( const char * name ) {
int fd , r ;
fd = open_terminal ( name , O_RDWR | O_NOCTTY | O_CLOEXEC ) ;
if ( fd < 0 )
return fd ;
2012-01-29 21:55:51 +01:00
r = reset_terminal_fd ( fd , true ) ;
2011-05-18 01:07:31 +02:00
close_nointr_nofail ( fd ) ;
return r ;
}
2010-04-13 02:06:27 +02:00
int open_terminal ( const char * name , int mode ) {
int fd , r ;
2011-02-17 16:29:47 +01:00
unsigned c = 0 ;
2010-04-13 02:06:27 +02:00
2011-02-17 16:29:47 +01:00
/*
* If a TTY is in the process of being closed opening it might
* cause EIO . This is horribly awful , but unlikely to be
* changed in the kernel . Hence we work around this problem by
* retrying a couple of times .
*
* https : //bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245
*/
2013-07-17 02:52:41 +02:00
assert ( ! ( mode & O_CREAT ) ) ;
2011-02-17 16:29:47 +01:00
for ( ; ; ) {
2013-07-17 02:52:41 +02:00
fd = open ( name , mode , 0 ) ;
2012-06-26 12:16:18 +02:00
if ( fd > = 0 )
2011-02-17 16:29:47 +01:00
break ;
if ( errno ! = EIO )
return - errno ;
2012-06-26 12:16:18 +02:00
/* Max 1s in total */
2011-02-17 16:29:47 +01:00
if ( c > = 20 )
return - errno ;
usleep ( 50 * USEC_PER_MSEC ) ;
c + + ;
}
if ( fd < 0 )
2010-04-13 02:06:27 +02:00
return - errno ;
2012-06-26 12:16:18 +02:00
r = isatty ( fd ) ;
if ( r < 0 ) {
2010-04-13 02:06:27 +02:00
close_nointr_nofail ( fd ) ;
return - errno ;
}
if ( ! r ) {
close_nointr_nofail ( fd ) ;
return - ENOTTY ;
}
return fd ;
}
int flush_fd ( int fd ) {
2013-03-25 00:59:00 +01:00
struct pollfd pollfd = {
. fd = fd ,
. events = POLLIN ,
} ;
2010-04-13 02:06:27 +02:00
for ( ; ; ) {
2011-04-07 18:48:50 +02:00
char buf [ LINE_MAX ] ;
2010-04-13 02:06:27 +02:00
ssize_t l ;
int r ;
2013-03-25 00:45:16 +01:00
r = poll ( & pollfd , 1 , 0 ) ;
if ( r < 0 ) {
2010-04-13 02:06:27 +02:00
if ( errno = = EINTR )
continue ;
return - errno ;
2013-03-25 00:45:16 +01:00
} else if ( r = = 0 )
2010-04-13 02:06:27 +02:00
return 0 ;
2013-03-25 00:45:16 +01:00
l = read ( fd , buf , sizeof ( buf ) ) ;
if ( l < 0 ) {
2010-04-13 02:06:27 +02:00
if ( errno = = EINTR )
continue ;
if ( errno = = EAGAIN )
return 0 ;
return - errno ;
2013-03-25 00:45:16 +01:00
} else if ( l = = 0 )
2010-04-13 02:06:27 +02:00
return 0 ;
}
}
2012-06-26 12:16:18 +02:00
int acquire_terminal (
const char * name ,
bool fail ,
bool force ,
bool ignore_tiocstty_eperm ,
usec_t timeout ) {
2012-07-15 15:34:22 +02:00
int fd = - 1 , notify = - 1 , r = 0 , wd = - 1 ;
2012-06-26 12:16:18 +02:00
usec_t ts = 0 ;
2010-04-13 02:06:27 +02:00
assert ( name ) ;
/* We use inotify to be notified when the tty is closed. We
* create the watch before checking if we can actually acquire
* it , so that we don ' t lose any event .
*
* Note : strictly speaking this actually watches for the
* device being closed , it does * not * really watch whether a
* tty loses its controlling process . However , unless some
* rogue process uses TIOCNOTTY on / dev / tty * after * closing
* its tty otherwise this will not become a problem . As long
* as the administrator makes sure not configure any service
* on the same tty as an untrusted user this should not be a
* problem . ( Which he probably should not do anyway . ) */
2012-06-26 12:16:18 +02:00
if ( timeout ! = ( usec_t ) - 1 )
ts = now ( CLOCK_MONOTONIC ) ;
2010-04-13 02:06:27 +02:00
if ( ! fail & & ! force ) {
2012-06-26 12:16:18 +02:00
notify = inotify_init1 ( IN_CLOEXEC | ( timeout ! = ( usec_t ) - 1 ? IN_NONBLOCK : 0 ) ) ;
if ( notify < 0 ) {
2010-04-13 02:06:27 +02:00
r = - errno ;
goto fail ;
}
2012-06-26 12:16:18 +02:00
wd = inotify_add_watch ( notify , name , IN_CLOSE ) ;
if ( wd < 0 ) {
2010-04-13 02:06:27 +02:00
r = - errno ;
goto fail ;
}
}
for ( ; ; ) {
2013-03-25 00:59:00 +01:00
struct sigaction sa_old , sa_new = {
. sa_handler = SIG_IGN ,
. sa_flags = SA_RESTART ,
} ;
2012-06-26 12:16:18 +02:00
if ( notify > = 0 ) {
r = flush_fd ( notify ) ;
if ( r < 0 )
2010-05-14 04:36:47 +02:00
goto fail ;
2012-06-26 12:16:18 +02:00
}
2010-04-13 02:06:27 +02:00
/* We pass here O_NOCTTY only so that we can check the return
* value TIOCSCTTY and have a reliable way to figure out if we
* successfully became the controlling process of the tty */
2012-06-26 12:16:18 +02:00
fd = open_terminal ( name , O_RDWR | O_NOCTTY | O_CLOEXEC ) ;
if ( fd < 0 )
2011-05-18 01:07:31 +02:00
return fd ;
2010-04-13 02:06:27 +02:00
2012-07-13 13:55:35 +02:00
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* if we already own the tty . */
assert_se ( sigaction ( SIGHUP , & sa_new , & sa_old ) = = 0 ) ;
2010-04-13 02:06:27 +02:00
/* First, try to get the tty */
2012-07-13 13:55:35 +02:00
if ( ioctl ( fd , TIOCSCTTY , force ) < 0 )
r = - errno ;
assert_se ( sigaction ( SIGHUP , & sa_old , NULL ) = = 0 ) ;
2010-05-18 03:40:19 +02:00
/* Sometimes it makes sense to ignore TIOCSCTTY
* returning EPERM , i . e . when very likely we already
* are have this controlling terminal . */
2012-07-13 13:55:35 +02:00
if ( r < 0 & & r = = - EPERM & & ignore_tiocstty_eperm )
2010-05-18 03:40:19 +02:00
r = 0 ;
2012-07-13 13:55:35 +02:00
if ( r < 0 & & ( force | | fail | | r ! = - EPERM ) ) {
2010-04-13 02:06:27 +02:00
goto fail ;
}
if ( r > = 0 )
break ;
assert ( ! fail ) ;
assert ( ! force ) ;
assert ( notify > = 0 ) ;
for ( ; ; ) {
2010-10-13 02:34:00 +02:00
uint8_t inotify_buffer [ sizeof ( struct inotify_event ) + FILENAME_MAX ] ;
2010-04-13 02:06:27 +02:00
ssize_t l ;
2010-10-13 02:34:00 +02:00
struct inotify_event * e ;
2010-04-13 02:06:27 +02:00
2012-06-26 12:16:18 +02:00
if ( timeout ! = ( usec_t ) - 1 ) {
usec_t n ;
n = now ( CLOCK_MONOTONIC ) ;
if ( ts + timeout < n ) {
r = - ETIMEDOUT ;
goto fail ;
}
r = fd_wait_for_event ( fd , POLLIN , ts + timeout - n ) ;
if ( r < 0 )
goto fail ;
if ( r = = 0 ) {
r = - ETIMEDOUT ;
goto fail ;
}
}
l = read ( notify , inotify_buffer , sizeof ( inotify_buffer ) ) ;
if ( l < 0 ) {
2010-04-13 02:06:27 +02:00
2012-06-26 12:16:18 +02:00
if ( errno = = EINTR | | errno = = EAGAIN )
2010-10-13 02:34:00 +02:00
continue ;
r = - errno ;
goto fail ;
}
e = ( struct inotify_event * ) inotify_buffer ;
2010-04-13 02:06:27 +02:00
2010-10-13 02:34:00 +02:00
while ( l > 0 ) {
size_t step ;
2010-04-13 02:06:27 +02:00
2010-10-13 02:34:00 +02:00
if ( e - > wd ! = wd | | ! ( e - > mask & IN_CLOSE ) ) {
2010-04-13 02:06:27 +02:00
r = - EIO ;
2010-10-13 02:34:00 +02:00
goto fail ;
}
2010-04-13 02:06:27 +02:00
2010-10-13 02:34:00 +02:00
step = sizeof ( struct inotify_event ) + e - > len ;
assert ( step < = ( size_t ) l ) ;
2010-04-13 02:06:27 +02:00
2010-10-13 02:34:00 +02:00
e = ( struct inotify_event * ) ( ( uint8_t * ) e + step ) ;
l - = step ;
2010-04-13 02:06:27 +02:00
}
break ;
}
/* We close the tty fd here since if the old session
* ended our handle will be dead . It ' s important that
* we do this after sleeping , so that we don ' t enter
* an endless loop . */
close_nointr_nofail ( fd ) ;
}
if ( notify > = 0 )
2010-04-21 03:27:44 +02:00
close_nointr_nofail ( notify ) ;
2010-04-13 02:06:27 +02:00
2012-01-29 21:55:51 +01:00
r = reset_terminal_fd ( fd , true ) ;
if ( r < 0 )
2010-04-13 02:06:27 +02:00
log_warning ( " Failed to reset terminal: %s " , strerror ( - r ) ) ;
return fd ;
fail :
if ( fd > = 0 )
2010-04-21 03:27:44 +02:00
close_nointr_nofail ( fd ) ;
2010-04-13 02:06:27 +02:00
if ( notify > = 0 )
2010-04-21 03:27:44 +02:00
close_nointr_nofail ( notify ) ;
2010-04-13 02:06:27 +02:00
return r ;
}
int release_terminal ( void ) {
2013-03-25 00:45:16 +01:00
int r = 0 ;
2013-03-25 00:59:00 +01:00
struct sigaction sa_old , sa_new = {
. sa_handler = SIG_IGN ,
. sa_flags = SA_RESTART ,
} ;
2013-04-18 09:11:22 +02:00
_cleanup_close_ int fd ;
2010-04-13 02:06:27 +02:00
2013-03-25 00:45:16 +01:00
fd = open ( " /dev/tty " , O_RDWR | O_NOCTTY | O_NDELAY | O_CLOEXEC ) ;
if ( fd < 0 )
2010-04-13 02:06:27 +02:00
return - errno ;
2010-04-24 02:32:07 +02:00
/* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed
* by our own TIOCNOTTY */
assert_se ( sigaction ( SIGHUP , & sa_new , & sa_old ) = = 0 ) ;
2010-04-13 02:06:27 +02:00
if ( ioctl ( fd , TIOCNOTTY ) < 0 )
r = - errno ;
2010-04-24 02:32:07 +02:00
assert_se ( sigaction ( SIGHUP , & sa_old , NULL ) = = 0 ) ;
2010-04-13 02:06:27 +02:00
return r ;
}
2010-05-22 01:46:08 +02:00
int sigaction_many ( const struct sigaction * sa , . . . ) {
va_list ap ;
int r = 0 , sig ;
va_start ( ap , sa ) ;
while ( ( sig = va_arg ( ap , int ) ) > 0 )
if ( sigaction ( sig , sa , NULL ) < 0 )
r = - errno ;
va_end ( ap ) ;
return r ;
}
int ignore_signals ( int sig , . . . ) {
2013-03-25 00:59:00 +01:00
struct sigaction sa = {
. sa_handler = SIG_IGN ,
. sa_flags = SA_RESTART ,
} ;
2010-05-22 01:46:08 +02:00
va_list ap ;
int r = 0 ;
2010-04-13 02:36:19 +02:00
2010-05-22 01:46:08 +02:00
if ( sigaction ( sig , & sa , NULL ) < 0 )
r = - errno ;
va_start ( ap , sig ) ;
while ( ( sig = va_arg ( ap , int ) ) > 0 )
if ( sigaction ( sig , & sa , NULL ) < 0 )
r = - errno ;
va_end ( ap ) ;
return r ;
}
int default_signals ( int sig , . . . ) {
2013-03-25 00:59:00 +01:00
struct sigaction sa = {
. sa_handler = SIG_DFL ,
. sa_flags = SA_RESTART ,
} ;
2010-05-22 01:46:08 +02:00
va_list ap ;
int r = 0 ;
if ( sigaction ( sig , & sa , NULL ) < 0 )
r = - errno ;
va_start ( ap , sig ) ;
while ( ( sig = va_arg ( ap , int ) ) > 0 )
if ( sigaction ( sig , & sa , NULL ) < 0 )
r = - errno ;
va_end ( ap ) ;
return r ;
2010-04-13 02:36:19 +02:00
}
2010-04-16 23:24:39 +02:00
int close_pipe ( int p [ ] ) {
int a = 0 , b = 0 ;
assert ( p ) ;
if ( p [ 0 ] > = 0 ) {
a = close_nointr ( p [ 0 ] ) ;
p [ 0 ] = - 1 ;
}
if ( p [ 1 ] > = 0 ) {
b = close_nointr ( p [ 1 ] ) ;
p [ 1 ] = - 1 ;
}
return a < 0 ? a : b ;
}
2010-06-18 04:44:53 +02:00
ssize_t loop_read ( int fd , void * buf , size_t nbytes , bool do_poll ) {
2014-01-28 13:06:44 +01:00
uint8_t * p = buf ;
2010-04-16 23:24:39 +02:00
ssize_t n = 0 ;
assert ( fd > = 0 ) ;
assert ( buf ) ;
while ( nbytes > 0 ) {
ssize_t k ;
2014-01-28 13:06:44 +01:00
k = read ( fd , p , nbytes ) ;
if ( k < 0 & & errno = = EINTR )
continue ;
2010-04-16 23:24:39 +02:00
2014-01-28 13:06:44 +01:00
if ( k < 0 & & errno = = EAGAIN & & do_poll ) {
2010-04-16 23:24:39 +02:00
2014-01-28 13:06:44 +01:00
/* We knowingly ignore any return value here,
* and expect that any error / EOF is reported
* via read ( ) */
2010-04-16 23:24:39 +02:00
2014-01-28 13:06:44 +01:00
fd_wait_for_event ( fd , POLLIN , ( usec_t ) - 1 ) ;
continue ;
}
2010-04-16 23:24:39 +02:00
2014-01-28 13:06:44 +01:00
if ( k < = 0 )
2010-04-16 23:24:39 +02:00
return n > 0 ? n : ( k < 0 ? - errno : 0 ) ;
p + = k ;
nbytes - = k ;
n + = k ;
}
return n ;
}
2010-06-18 04:44:53 +02:00
ssize_t loop_write ( int fd , const void * buf , size_t nbytes , bool do_poll ) {
2014-01-28 13:06:44 +01:00
const uint8_t * p = buf ;
2010-06-18 04:44:53 +02:00
ssize_t n = 0 ;
assert ( fd > = 0 ) ;
assert ( buf ) ;
while ( nbytes > 0 ) {
ssize_t k ;
2011-12-23 20:50:48 +01:00
k = write ( fd , p , nbytes ) ;
2014-01-28 13:06:44 +01:00
if ( k < 0 & & errno = = EINTR )
continue ;
2010-06-18 04:44:53 +02:00
2014-01-28 13:06:44 +01:00
if ( k < 0 & & errno = = EAGAIN & & do_poll ) {
2010-06-18 04:44:53 +02:00
2014-01-28 13:06:44 +01:00
/* We knowingly ignore any return value here,
* and expect that any error / EOF is reported
* via write ( ) */
2010-06-18 04:44:53 +02:00
2014-01-28 13:06:44 +01:00
fd_wait_for_event ( fd , POLLOUT , ( usec_t ) - 1 ) ;
continue ;
}
2010-06-18 04:44:53 +02:00
2014-01-28 13:06:44 +01:00
if ( k < = 0 )
2010-06-18 04:44:53 +02:00
return n > 0 ? n : ( k < 0 ? - errno : 0 ) ;
p + = k ;
nbytes - = k ;
n + = k ;
}
return n ;
}
2014-02-23 03:13:54 +01:00
int parse_size ( const char * t , off_t base , off_t * size ) {
/* Soo, sometimes we want to parse IEC binary suffxies, and
* sometimes SI decimal suffixes . This function can parse
* both . Which one is the right way depends on the
* context . Wikipedia suggests that SI is customary for
* hardrware metrics and network speeds , while IEC is
* customary for most data sizes used by software and volatile
* ( RAM ) memory . Hence be careful which one you pick !
*
* In either case we use just K , M , G as suffix , and not Ki ,
* Mi , Gi or so ( as IEC would suggest ) . That ' s because that ' s
* frickin ' ugly . But this means you really need to make sure
* to document which base you are parsing when you use this
* call . */
struct table {
2011-08-20 00:20:41 +02:00
const char * suffix ;
2013-06-06 01:33:45 +02:00
unsigned long long factor ;
2014-02-23 03:13:54 +01:00
} ;
static const struct table iec [ ] = {
2012-01-14 03:07:29 +01:00
{ " E " , 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL } ,
Disallow sizes with increasing unit size
Things like 3B4T, 4B50B, 400 100 (meaning 4*1024**4+3, 54, and 500,
respectively) are now disallowed. It is necessary to say 4T3B, 54B,
500 instead. I think this was confusing and error prone.
As a special form, 400B 100 is allowed, i.e. "B" suffix is treated
as different from "", although they mean the same thing.
2014-03-02 19:28:05 +01:00
{ " P " , 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL } ,
{ " T " , 1024ULL * 1024ULL * 1024ULL * 1024ULL } ,
{ " G " , 1024ULL * 1024ULL * 1024ULL } ,
{ " M " , 1024ULL * 1024ULL } ,
{ " K " , 1024ULL } ,
{ " B " , 1 } ,
2011-08-20 00:20:41 +02:00
{ " " , 1 } ,
} ;
2014-02-23 03:13:54 +01:00
static const struct table si [ ] = {
{ " E " , 1000ULL * 1000ULL * 1000ULL * 1000ULL * 1000ULL * 1000ULL } ,
Disallow sizes with increasing unit size
Things like 3B4T, 4B50B, 400 100 (meaning 4*1024**4+3, 54, and 500,
respectively) are now disallowed. It is necessary to say 4T3B, 54B,
500 instead. I think this was confusing and error prone.
As a special form, 400B 100 is allowed, i.e. "B" suffix is treated
as different from "", although they mean the same thing.
2014-03-02 19:28:05 +01:00
{ " P " , 1000ULL * 1000ULL * 1000ULL * 1000ULL * 1000ULL } ,
{ " T " , 1000ULL * 1000ULL * 1000ULL * 1000ULL } ,
{ " G " , 1000ULL * 1000ULL * 1000ULL } ,
{ " M " , 1000ULL * 1000ULL } ,
{ " K " , 1000ULL } ,
{ " B " , 1 } ,
2014-02-23 03:13:54 +01:00
{ " " , 1 } ,
} ;
const struct table * table ;
2011-08-20 00:20:41 +02:00
const char * p ;
2013-06-06 01:33:45 +02:00
unsigned long long r = 0 ;
Disallow sizes with increasing unit size
Things like 3B4T, 4B50B, 400 100 (meaning 4*1024**4+3, 54, and 500,
respectively) are now disallowed. It is necessary to say 4T3B, 54B,
500 instead. I think this was confusing and error prone.
As a special form, 400B 100 is allowed, i.e. "B" suffix is treated
as different from "", although they mean the same thing.
2014-03-02 19:28:05 +01:00
unsigned n_entries , start_pos = 0 ;
2011-08-20 00:20:41 +02:00
assert ( t ) ;
2014-02-23 03:13:54 +01:00
assert ( base = = 1000 | | base = = 1024 ) ;
assert ( size ) ;
if ( base = = 1000 ) {
table = si ;
n_entries = ELEMENTSOF ( si ) ;
} else {
table = iec ;
n_entries = ELEMENTSOF ( iec ) ;
}
2011-08-20 00:20:41 +02:00
p = t ;
do {
long long l ;
2014-03-02 06:05:16 +01:00
unsigned long long l2 ;
double frac = 0 ;
2011-08-20 00:20:41 +02:00
char * e ;
unsigned i ;
errno = 0 ;
l = strtoll ( p , & e , 10 ) ;
2013-03-28 14:24:15 +01:00
if ( errno > 0 )
2011-08-20 00:20:41 +02:00
return - errno ;
if ( l < 0 )
return - ERANGE ;
if ( e = = p )
return - EINVAL ;
2014-03-02 06:05:16 +01:00
if ( * e = = ' . ' ) {
e + + ;
if ( * e > = ' 0 ' & & * e < = ' 9 ' ) {
char * e2 ;
/* strotoull itself would accept space/+/- */
l2 = strtoull ( e , & e2 , 10 ) ;
if ( errno = = ERANGE )
return - errno ;
/* Ignore failure. E.g. 10.M is valid */
frac = l2 ;
for ( ; e < e2 ; e + + )
frac / = 10 ;
}
}
2011-08-20 00:20:41 +02:00
e + = strspn ( e , WHITESPACE ) ;
Disallow sizes with increasing unit size
Things like 3B4T, 4B50B, 400 100 (meaning 4*1024**4+3, 54, and 500,
respectively) are now disallowed. It is necessary to say 4T3B, 54B,
500 instead. I think this was confusing and error prone.
As a special form, 400B 100 is allowed, i.e. "B" suffix is treated
as different from "", although they mean the same thing.
2014-03-02 19:28:05 +01:00
for ( i = start_pos ; i < n_entries ; i + + )
2011-08-20 00:20:41 +02:00
if ( startswith ( e , table [ i ] . suffix ) ) {
2013-06-06 01:33:45 +02:00
unsigned long long tmp ;
2014-03-02 06:05:16 +01:00
if ( ( unsigned long long ) l + ( frac > 0 ) > ULLONG_MAX / table [ i ] . factor )
2013-06-06 01:33:45 +02:00
return - ERANGE ;
2014-03-02 06:05:16 +01:00
tmp = l * table [ i ] . factor + ( unsigned long long ) ( frac * table [ i ] . factor ) ;
2013-06-06 01:33:45 +02:00
if ( tmp > ULLONG_MAX - r )
return - ERANGE ;
r + = tmp ;
if ( ( unsigned long long ) ( off_t ) r ! = r )
return - ERANGE ;
2011-08-20 00:20:41 +02:00
p = e + strlen ( table [ i ] . suffix ) ;
Disallow sizes with increasing unit size
Things like 3B4T, 4B50B, 400 100 (meaning 4*1024**4+3, 54, and 500,
respectively) are now disallowed. It is necessary to say 4T3B, 54B,
500 instead. I think this was confusing and error prone.
As a special form, 400B 100 is allowed, i.e. "B" suffix is treated
as different from "", although they mean the same thing.
2014-03-02 19:28:05 +01:00
start_pos = i + 1 ;
2011-08-20 00:20:41 +02:00
break ;
}
2014-02-23 03:13:54 +01:00
if ( i > = n_entries )
2011-08-20 00:20:41 +02:00
return - EINVAL ;
2013-06-06 01:33:45 +02:00
} while ( * p ) ;
2011-08-20 00:20:41 +02:00
2014-02-23 03:13:54 +01:00
* size = r ;
2011-08-20 00:20:41 +02:00
return 0 ;
}
2010-05-15 17:25:08 +02:00
int make_stdio ( int fd ) {
int r , s , t ;
assert ( fd > = 0 ) ;
2012-09-14 10:36:50 +02:00
r = dup3 ( fd , STDIN_FILENO , 0 ) ;
s = dup3 ( fd , STDOUT_FILENO , 0 ) ;
t = dup3 ( fd , STDERR_FILENO , 0 ) ;
2010-05-15 17:25:08 +02:00
if ( fd > = 3 )
close_nointr_nofail ( fd ) ;
if ( r < 0 | | s < 0 | | t < 0 )
return - errno ;
2012-09-14 10:36:50 +02:00
/* We rely here that the new fd has O_CLOEXEC not set */
2011-07-03 23:20:56 +02:00
2010-05-15 17:25:08 +02:00
return 0 ;
}
2010-10-27 05:45:57 +02:00
int make_null_stdio ( void ) {
int null_fd ;
2012-07-10 19:19:59 +02:00
null_fd = open ( " /dev/null " , O_RDWR | O_NOCTTY ) ;
if ( null_fd < 0 )
2010-10-27 05:45:57 +02:00
return - errno ;
return make_stdio ( null_fd ) ;
}
2010-05-16 18:13:58 +02:00
bool is_device_path ( const char * path ) {
/* Returns true on paths that refer to a device, either in
* sysfs or in / dev */
return
path_startswith ( path , " /dev/ " ) | |
path_startswith ( path , " /sys/ " ) ;
}
2010-05-24 05:25:33 +02:00
int dir_is_empty ( const char * path ) {
2012-09-14 10:24:27 +02:00
_cleanup_closedir_ DIR * d ;
2010-05-24 05:25:33 +02:00
2012-09-14 10:24:27 +02:00
d = opendir ( path ) ;
if ( ! d )
2010-05-24 05:25:33 +02:00
return - errno ;
for ( ; ; ) {
2012-09-19 22:21:09 +02:00
struct dirent * de ;
2010-05-24 05:25:33 +02:00
2013-12-19 12:05:41 +01:00
errno = 0 ;
de = readdir ( d ) ;
if ( ! de & & errno ! = 0 )
return - errno ;
2010-05-24 05:25:33 +02:00
2012-09-14 10:24:27 +02:00
if ( ! de )
return 1 ;
2010-05-24 05:25:33 +02:00
2012-09-14 10:24:27 +02:00
if ( ! ignore_file ( de - > d_name ) )
return 0 ;
}
2010-05-24 05:25:33 +02:00
}
2013-03-29 01:17:24 +01:00
char * dirname_malloc ( const char * path ) {
char * d , * dir , * dir2 ;
d = strdup ( path ) ;
if ( ! d )
return NULL ;
dir = dirname ( d ) ;
assert ( dir ) ;
if ( dir ! = d ) {
dir2 = strdup ( dir ) ;
free ( d ) ;
return dir2 ;
}
return dir ;
}
2014-01-28 13:07:28 +01:00
int dev_urandom ( void * p , size_t n ) {
2012-09-14 10:24:27 +02:00
_cleanup_close_ int fd ;
2013-12-22 19:59:12 +01:00
ssize_t k ;
2010-06-16 05:05:36 +02:00
2012-08-13 15:27:04 +02:00
fd = open ( " /dev/urandom " , O_RDONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( fd < 0 )
2014-01-28 13:07:28 +01:00
return errno = = ENOENT ? - ENOSYS : - errno ;
2010-06-16 05:05:36 +02:00
2013-12-22 19:59:12 +01:00
k = loop_read ( fd , p , n , true ) ;
2014-01-28 13:07:28 +01:00
if ( k < 0 )
return ( int ) k ;
if ( ( size_t ) k ! = n )
return - EIO ;
return 0 ;
}
void random_bytes ( void * p , size_t n ) {
static bool srand_called = false ;
uint8_t * q ;
int r ;
2010-06-16 05:05:36 +02:00
2014-01-28 13:07:28 +01:00
r = dev_urandom ( p , n ) ;
if ( r > = 0 )
return ;
2010-06-16 05:05:36 +02:00
2014-01-28 13:07:28 +01:00
/* If some idiot made /dev/urandom unavailable to us, he'll
* get a PRNG instead . */
2010-06-16 05:05:36 +02:00
2013-12-22 19:59:12 +01:00
if ( ! srand_called ) {
2014-01-28 13:07:28 +01:00
unsigned x = 0 ;
2013-10-01 23:11:23 +02:00
2013-12-22 19:59:12 +01:00
# ifdef HAVE_SYS_AUXV_H
/* The kernel provides us with a bit of entropy in
* auxv , so let ' s try to make use of that to seed the
* pseudo - random generator . It ' s better than
* nothing . . . */
2013-10-01 23:11:23 +02:00
2013-12-22 19:59:12 +01:00
void * auxv ;
auxv = ( void * ) getauxval ( AT_RANDOM ) ;
if ( auxv )
2014-01-28 13:07:28 +01:00
x ^ = * ( unsigned * ) auxv ;
2013-12-22 19:59:12 +01:00
# endif
2013-10-01 23:11:23 +02:00
2014-01-28 13:07:28 +01:00
x ^ = ( unsigned ) now ( CLOCK_REALTIME ) ;
x ^ = ( unsigned ) gettid ( ) ;
srand ( x ) ;
2013-12-22 19:59:12 +01:00
srand_called = true ;
}
2013-10-01 23:11:23 +02:00
2013-12-22 19:59:12 +01:00
for ( q = p ; q < ( uint8_t * ) p + n ; q + + )
* q = rand ( ) ;
2013-10-01 23:11:23 +02:00
}
2010-06-16 21:54:17 +02:00
void rename_process ( const char name [ 8 ] ) {
assert ( name ) ;
2012-02-01 22:33:15 +01:00
/* This is a like a poor man's setproctitle(). It changes the
* comm field , argv [ 0 ] , and also the glibc ' s internally used
* name of the process . For the first one a limit of 16 chars
* applies , to the second one usually one of 10 ( i . e . length
* of " /sbin/init " ) , to the third one one of 7 ( i . e . length of
* " systemd " ) . If you pass a longer string it will be
* truncated */
2010-06-16 21:54:17 +02:00
2012-02-01 22:33:15 +01:00
prctl ( PR_SET_NAME , name ) ;
2010-06-16 21:54:17 +02:00
if ( program_invocation_name )
strncpy ( program_invocation_name , name , strlen ( program_invocation_name ) ) ;
2011-06-30 04:16:10 +02:00
if ( saved_argc > 0 ) {
int i ;
if ( saved_argv [ 0 ] )
strncpy ( saved_argv [ 0 ] , name , strlen ( saved_argv [ 0 ] ) ) ;
for ( i = 1 ; i < saved_argc ; i + + ) {
if ( ! saved_argv [ i ] )
break ;
2014-01-31 06:51:32 +01:00
memzero ( saved_argv [ i ] , strlen ( saved_argv [ i ] ) ) ;
2011-06-30 04:16:10 +02:00
}
}
2010-06-16 21:54:17 +02:00
}
2010-06-17 23:22:56 +02:00
void sigset_add_many ( sigset_t * ss , . . . ) {
va_list ap ;
int sig ;
assert ( ss ) ;
va_start ( ap , ss ) ;
while ( ( sig = va_arg ( ap , int ) ) > 0 )
assert_se ( sigaddset ( ss , sig ) = = 0 ) ;
va_end ( ap ) ;
}
2010-06-18 02:28:35 +02:00
char * gethostname_malloc ( void ) {
struct utsname u ;
assert_se ( uname ( & u ) > = 0 ) ;
2012-05-21 17:19:58 +02:00
if ( ! isempty ( u . nodename ) & & ! streq ( u . nodename , " (none) " ) )
2010-06-18 02:28:35 +02:00
return strdup ( u . nodename ) ;
return strdup ( u . sysname ) ;
}
2012-05-21 17:19:58 +02:00
bool hostname_is_set ( void ) {
struct utsname u ;
assert_se ( uname ( & u ) > = 0 ) ;
return ! isempty ( u . nodename ) & & ! streq ( u . nodename , " (none) " ) ;
}
2012-07-16 12:15:22 +02:00
static char * lookup_uid ( uid_t uid ) {
2010-06-18 02:28:35 +02:00
long bufsize ;
2012-09-14 10:24:27 +02:00
char * name ;
_cleanup_free_ char * buf = NULL ;
2010-06-18 02:28:35 +02:00
struct passwd pwbuf , * pw = NULL ;
/* Shortcut things to avoid NSS lookups */
if ( uid = = 0 )
return strdup ( " root " ) ;
2012-07-16 12:15:22 +02:00
bufsize = sysconf ( _SC_GETPW_R_SIZE_MAX ) ;
if ( bufsize < = 0 )
2010-06-18 02:28:35 +02:00
bufsize = 4096 ;
2012-07-16 12:15:22 +02:00
buf = malloc ( bufsize ) ;
if ( ! buf )
2010-06-18 02:28:35 +02:00
return NULL ;
2012-09-14 10:24:27 +02:00
if ( getpwuid_r ( uid , & pwbuf , buf , bufsize , & pw ) = = 0 & & pw )
return strdup ( pw - > pw_name ) ;
2010-06-18 02:28:35 +02:00
if ( asprintf ( & name , " %lu " , ( unsigned long ) uid ) < 0 )
return NULL ;
return name ;
}
2012-07-16 12:15:22 +02:00
char * getlogname_malloc ( void ) {
uid_t uid ;
struct stat st ;
if ( isatty ( STDIN_FILENO ) & & fstat ( STDIN_FILENO , & st ) > = 0 )
uid = st . st_uid ;
else
uid = getuid ( ) ;
return lookup_uid ( uid ) ;
}
char * getusername_malloc ( void ) {
const char * e ;
e = getenv ( " USER " ) ;
if ( e )
return strdup ( e ) ;
return lookup_uid ( getuid ( ) ) ;
}
2011-02-17 16:29:04 +01:00
int getttyname_malloc ( int fd , char * * r ) {
char path [ PATH_MAX ] , * c ;
2010-08-16 21:25:09 +02:00
int k ;
2010-06-21 23:27:18 +02:00
assert ( r ) ;
2010-06-18 02:28:35 +02:00
2012-09-14 10:24:27 +02:00
k = ttyname_r ( fd , path , sizeof ( path ) ) ;
2013-11-30 23:45:31 +01:00
if ( k > 0 )
2010-08-16 21:25:09 +02:00
return - k ;
2010-06-18 02:28:35 +02:00
char_array_0 ( path ) ;
2012-09-14 10:24:27 +02:00
c = strdup ( startswith ( path , " /dev/ " ) ? path + 5 : path ) ;
if ( ! c )
2010-06-21 23:27:18 +02:00
return - ENOMEM ;
* r = c ;
return 0 ;
}
2011-02-17 16:29:04 +01:00
int getttyname_harder ( int fd , char * * r ) {
int k ;
char * s ;
2012-09-14 10:24:27 +02:00
k = getttyname_malloc ( fd , & s ) ;
if ( k < 0 )
2011-02-17 16:29:04 +01:00
return k ;
if ( streq ( s , " tty " ) ) {
free ( s ) ;
2011-06-27 22:44:12 +02:00
return get_ctty ( 0 , NULL , r ) ;
2011-02-17 16:29:04 +01:00
}
* r = s ;
return 0 ;
}
2011-06-27 22:44:12 +02:00
int get_ctty_devnr ( pid_t pid , dev_t * d ) {
2014-01-04 02:35:26 +01:00
int r ;
_cleanup_free_ char * line = NULL ;
const char * p ;
2011-02-17 16:29:04 +01:00
unsigned long ttynr ;
2013-04-16 14:50:05 +02:00
assert ( pid > = 0 ) ;
2014-01-04 02:35:26 +01:00
p = procfs_file_alloca ( pid , " stat " ) ;
r = read_one_line_file ( p , & line ) ;
if ( r < 0 )
return r ;
2011-02-17 16:29:04 +01:00
2011-06-27 22:44:12 +02:00
p = strrchr ( line , ' ) ' ) ;
if ( ! p )
2011-02-17 16:29:04 +01:00
return - EIO ;
p + + ;
if ( sscanf ( p , " "
" %*c " /* state */
" %*d " /* ppid */
" %*d " /* pgrp */
" %*d " /* session */
" %lu " , /* ttynr */
& ttynr ) ! = 1 )
return - EIO ;
2012-12-23 17:28:17 +01:00
if ( major ( ttynr ) = = 0 & & minor ( ttynr ) = = 0 )
return - ENOENT ;
2013-11-25 18:08:02 +01:00
if ( d )
* d = ( dev_t ) ttynr ;
2011-02-17 16:29:04 +01:00
return 0 ;
}
2011-06-27 22:44:12 +02:00
int get_ctty ( pid_t pid , dev_t * _devnr , char * * r ) {
2014-02-19 17:52:28 +01:00
char fn [ sizeof ( " /dev/char/ " ) - 1 + 2 * DECIMAL_STR_MAX ( unsigned ) + 1 + 1 ] , * b = NULL ;
_cleanup_free_ char * s = NULL ;
const char * p ;
2011-02-17 16:29:04 +01:00
dev_t devnr ;
2014-02-19 17:52:28 +01:00
int k ;
2011-02-17 16:29:04 +01:00
assert ( r ) ;
2011-06-27 22:44:12 +02:00
k = get_ctty_devnr ( pid , & devnr ) ;
if ( k < 0 )
2011-02-17 16:29:04 +01:00
return k ;
snprintf ( fn , sizeof ( fn ) , " /dev/char/%u:%u " , major ( devnr ) , minor ( devnr ) ) ;
2012-12-23 22:32:48 +01:00
k = readlink_malloc ( fn , & s ) ;
if ( k < 0 ) {
2011-02-17 16:29:04 +01:00
if ( k ! = - ENOENT )
return k ;
2011-03-14 02:33:23 +01:00
/* This is an ugly hack */
if ( major ( devnr ) = = 136 ) {
2014-02-19 17:52:28 +01:00
asprintf ( & b , " pts/%lu " , ( unsigned long ) minor ( devnr ) ) ;
goto finish ;
2011-03-14 02:33:23 +01:00
}
2011-02-17 16:29:04 +01:00
/* Probably something like the ptys which have no
* symlink in / dev / char . Let ' s return something
* vaguely useful . */
2012-12-23 22:32:48 +01:00
b = strdup ( fn + 5 ) ;
2014-02-19 17:52:28 +01:00
goto finish ;
2011-02-17 16:29:04 +01:00
}
if ( startswith ( s , " /dev/ " ) )
p = s + 5 ;
else if ( startswith ( s , " ../ " ) )
p = s + 3 ;
else
p = s ;
b = strdup ( p ) ;
2014-02-19 17:52:28 +01:00
finish :
2011-02-17 16:29:04 +01:00
if ( ! b )
return - ENOMEM ;
* r = b ;
2011-03-14 02:33:23 +01:00
if ( _devnr )
* _devnr = devnr ;
2011-02-17 16:29:04 +01:00
return 0 ;
}
2012-07-10 19:05:58 +02:00
int rm_rf_children_dangerous ( int fd , bool only_dirs , bool honour_sticky , struct stat * root_dev ) {
2010-06-21 23:27:18 +02:00
DIR * d ;
int ret = 0 ;
assert ( fd > = 0 ) ;
/* This returns the first error we run into, but nevertheless
2012-05-22 16:14:34 +02:00
* tries to go on . This closes the passed fd . */
2010-06-21 23:27:18 +02:00
2012-05-09 01:25:52 +02:00
d = fdopendir ( fd ) ;
if ( ! d ) {
2010-06-21 23:27:18 +02:00
close_nointr_nofail ( fd ) ;
2010-07-13 19:00:01 +02:00
return errno = = ENOENT ? 0 : - errno ;
2010-06-21 23:27:18 +02:00
}
for ( ; ; ) {
2012-09-19 22:21:09 +02:00
struct dirent * de ;
2012-05-22 16:14:34 +02:00
bool is_dir , keep_around ;
struct stat st ;
2010-06-21 23:27:18 +02:00
int r ;
2013-12-19 12:05:41 +01:00
errno = 0 ;
de = readdir ( d ) ;
if ( ! de & & errno ! = 0 ) {
if ( ret = = 0 )
ret = - errno ;
2010-06-21 23:27:18 +02:00
break ;
}
if ( ! de )
break ;
if ( streq ( de - > d_name , " . " ) | | streq ( de - > d_name , " .. " ) )
continue ;
2012-05-22 16:14:34 +02:00
if ( de - > d_type = = DT_UNKNOWN | |
honour_sticky | |
( de - > d_type = = DT_DIR & & root_dev ) ) {
2010-06-21 23:27:18 +02:00
if ( fstatat ( fd , de - > d_name , & st , AT_SYMLINK_NOFOLLOW ) < 0 ) {
2010-07-13 19:00:01 +02:00
if ( ret = = 0 & & errno ! = ENOENT )
2010-06-21 23:27:18 +02:00
ret = - errno ;
continue ;
}
is_dir = S_ISDIR ( st . st_mode ) ;
2012-05-22 16:14:34 +02:00
keep_around =
honour_sticky & &
( st . st_uid = = 0 | | st . st_uid = = getuid ( ) ) & &
( st . st_mode & S_ISVTX ) ;
2011-08-21 20:05:51 +02:00
} else {
2010-06-21 23:27:18 +02:00
is_dir = de - > d_type = = DT_DIR ;
2012-05-22 16:14:34 +02:00
keep_around = false ;
2011-08-21 20:05:51 +02:00
}
2010-06-21 23:27:18 +02:00
if ( is_dir ) {
int subdir_fd ;
2012-05-16 15:08:27 +02:00
/* if root_dev is set, remove subdirectories only, if device is same as dir */
2012-05-22 16:14:34 +02:00
if ( root_dev & & st . st_dev ! = root_dev - > st_dev )
continue ;
2010-06-21 23:27:18 +02:00
2012-05-22 16:14:34 +02:00
subdir_fd = openat ( fd , de - > d_name ,
O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_NOATIME ) ;
if ( subdir_fd < 0 ) {
if ( ret = = 0 & & errno ! = ENOENT )
ret = - errno ;
continue ;
}
2012-09-03 15:40:37 +02:00
r = rm_rf_children_dangerous ( subdir_fd , only_dirs , honour_sticky , root_dev ) ;
2012-05-22 16:14:34 +02:00
if ( r < 0 & & ret = = 0 )
ret = r ;
if ( ! keep_around )
if ( unlinkat ( fd , de - > d_name , AT_REMOVEDIR ) < 0 ) {
2011-08-21 20:05:51 +02:00
if ( ret = = 0 & & errno ! = ENOENT )
ret = - errno ;
}
} else if ( ! only_dirs & & ! keep_around ) {
2010-06-21 23:27:18 +02:00
if ( unlinkat ( fd , de - > d_name , 0 ) < 0 ) {
2010-07-13 19:00:01 +02:00
if ( ret = = 0 & & errno ! = ENOENT )
2010-06-21 23:27:18 +02:00
ret = - errno ;
}
}
}
closedir ( d ) ;
return ret ;
}
2013-05-03 04:51:50 +02:00
_pure_ static int is_temporary_fs ( struct statfs * s ) {
2012-11-16 17:17:21 +01:00
assert ( s ) ;
2013-12-16 01:56:21 +01:00
return F_TYPE_EQUAL ( s - > f_type , TMPFS_MAGIC ) | |
F_TYPE_EQUAL ( s - > f_type , RAMFS_MAGIC ) ;
2012-11-16 17:17:21 +01:00
}
2012-07-10 19:05:58 +02:00
int rm_rf_children ( int fd , bool only_dirs , bool honour_sticky , struct stat * root_dev ) {
struct statfs s ;
assert ( fd > = 0 ) ;
if ( fstatfs ( fd , & s ) < 0 ) {
close_nointr_nofail ( fd ) ;
return - errno ;
}
/* We refuse to clean disk file systems with this call. This
* is extra paranoia just to be sure we never ever remove
* non - state data */
2012-11-16 17:17:21 +01:00
if ( ! is_temporary_fs ( & s ) ) {
2012-07-10 19:05:58 +02:00
log_error ( " Attempted to remove disk file system, and we can't allow that. " ) ;
close_nointr_nofail ( fd ) ;
return - EPERM ;
}
return rm_rf_children_dangerous ( fd , only_dirs , honour_sticky , root_dev ) ;
}
static int rm_rf_internal ( const char * path , bool only_dirs , bool delete_root , bool honour_sticky , bool dangerous ) {
int fd , r ;
struct statfs s ;
2010-06-21 23:27:18 +02:00
assert ( path ) ;
2012-07-10 19:05:58 +02:00
/* We refuse to clean the root file system with this
* call . This is extra paranoia to never cause a really
* seriously broken system . */
if ( path_equal ( path , " / " ) ) {
log_error ( " Attempted to remove entire root file system, and we can't allow that. " ) ;
return - EPERM ;
}
2012-07-09 17:30:22 +02:00
2012-05-09 01:25:52 +02:00
fd = open ( path , O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW | O_NOATIME ) ;
if ( fd < 0 ) {
2010-06-21 23:27:18 +02:00
if ( errno ! = ENOTDIR )
return - errno ;
2012-07-10 19:05:58 +02:00
if ( ! dangerous ) {
if ( statfs ( path , & s ) < 0 )
return - errno ;
2012-11-16 17:17:21 +01:00
if ( ! is_temporary_fs ( & s ) ) {
2012-07-10 19:05:58 +02:00
log_error ( " Attempted to remove disk file system, and we can't allow that. " ) ;
return - EPERM ;
}
}
2010-06-21 23:27:18 +02:00
if ( delete_root & & ! only_dirs )
2012-05-09 01:25:52 +02:00
if ( unlink ( path ) < 0 & & errno ! = ENOENT )
2010-06-21 23:27:18 +02:00
return - errno ;
return 0 ;
}
2012-07-10 19:05:58 +02:00
if ( ! dangerous ) {
if ( fstatfs ( fd , & s ) < 0 ) {
close_nointr_nofail ( fd ) ;
return - errno ;
}
2011-08-21 20:05:51 +02:00
2012-11-16 17:17:21 +01:00
if ( ! is_temporary_fs ( & s ) ) {
2012-07-10 19:05:58 +02:00
log_error ( " Attempted to remove disk file system, and we can't allow that. " ) ;
close_nointr_nofail ( fd ) ;
return - EPERM ;
}
}
r = rm_rf_children_dangerous ( fd , only_dirs , honour_sticky , NULL ) ;
2011-08-21 20:05:51 +02:00
if ( delete_root ) {
2012-01-18 15:40:21 +01:00
if ( honour_sticky & & file_is_priv_sticky ( path ) > 0 )
2011-08-21 20:05:51 +02:00
return r ;
2010-06-21 23:27:18 +02:00
2011-08-21 21:00:41 +02:00
if ( rmdir ( path ) < 0 & & errno ! = ENOENT ) {
2010-06-21 23:27:18 +02:00
if ( r = = 0 )
r = - errno ;
}
2011-08-21 20:05:51 +02:00
}
2010-06-21 23:27:18 +02:00
return r ;
}
2012-07-10 19:05:58 +02:00
int rm_rf ( const char * path , bool only_dirs , bool delete_root , bool honour_sticky ) {
return rm_rf_internal ( path , only_dirs , delete_root , honour_sticky , false ) ;
}
int rm_rf_dangerous ( const char * path , bool only_dirs , bool delete_root , bool honour_sticky ) {
return rm_rf_internal ( path , only_dirs , delete_root , honour_sticky , true ) ;
}
2010-06-21 23:27:18 +02:00
int chmod_and_chown ( const char * path , mode_t mode , uid_t uid , gid_t gid ) {
assert ( path ) ;
/* Under the assumption that we are running privileged we
* first change the access mode and only then hand out
* ownership to avoid a window where access is too open . */
2012-01-18 15:40:21 +01:00
if ( mode ! = ( mode_t ) - 1 )
if ( chmod ( path , mode ) < 0 )
return - errno ;
2010-06-21 23:27:18 +02:00
2012-01-18 15:40:21 +01:00
if ( uid ! = ( uid_t ) - 1 | | gid ! = ( gid_t ) - 1 )
if ( chown ( path , uid , gid ) < 0 )
return - errno ;
2010-06-21 23:27:18 +02:00
return 0 ;
2010-06-18 02:28:35 +02:00
}
2011-10-07 23:03:07 +02:00
int fchmod_and_fchown ( int fd , mode_t mode , uid_t uid , gid_t gid ) {
assert ( fd > = 0 ) ;
/* Under the assumption that we are running privileged we
* first change the access mode and only then hand out
* ownership to avoid a window where access is too open . */
2013-11-08 18:11:09 +01:00
if ( mode ! = ( mode_t ) - 1 )
if ( fchmod ( fd , mode ) < 0 )
return - errno ;
2011-10-07 23:03:07 +02:00
2013-11-08 18:11:09 +01:00
if ( uid ! = ( uid_t ) - 1 | | gid ! = ( gid_t ) - 1 )
if ( fchown ( fd , uid , gid ) < 0 )
return - errno ;
2011-10-07 23:03:07 +02:00
return 0 ;
}
2010-07-04 16:44:58 +02:00
cpu_set_t * cpu_set_malloc ( unsigned * ncpus ) {
cpu_set_t * r ;
unsigned n = 1024 ;
/* Allocates the cpuset in the right size */
for ( ; ; ) {
if ( ! ( r = CPU_ALLOC ( n ) ) )
return NULL ;
if ( sched_getaffinity ( 0 , CPU_ALLOC_SIZE ( n ) , r ) > = 0 ) {
CPU_ZERO_S ( CPU_ALLOC_SIZE ( n ) , r ) ;
if ( ncpus )
* ncpus = n ;
return r ;
}
CPU_FREE ( r ) ;
if ( errno ! = EINVAL )
return NULL ;
n * = 2 ;
}
}
2013-02-27 22:52:43 +01:00
int status_vprintf ( const char * status , bool ellipse , bool ephemeral , const char * format , va_list ap ) {
2012-05-14 12:50:33 +02:00
static const char status_indent [ ] = " " ; /* "[" STATUS "] " */
2012-09-17 18:23:10 +02:00
_cleanup_free_ char * s = NULL ;
_cleanup_close_ int fd = - 1 ;
2013-03-25 00:59:00 +01:00
struct iovec iovec [ 6 ] = { } ;
2012-01-05 03:24:39 +01:00
int n = 0 ;
2013-02-27 22:52:43 +01:00
static bool prev_ephemeral ;
2010-07-07 00:00:59 +02:00
assert ( format ) ;
2012-05-14 12:50:33 +02:00
/* This is independent of logging, as status messages are
2010-07-07 00:00:59 +02:00
* optional and go exclusively to the console . */
if ( vasprintf ( & s , format , ap ) < 0 )
2012-09-17 18:23:10 +02:00
return log_oom ( ) ;
2010-07-07 00:00:59 +02:00
2012-01-05 15:35:16 +01:00
fd = open_terminal ( " /dev/console " , O_WRONLY | O_NOCTTY | O_CLOEXEC ) ;
2012-01-05 03:24:39 +01:00
if ( fd < 0 )
2012-09-17 18:23:10 +02:00
return fd ;
2010-07-07 00:00:59 +02:00
2012-01-05 15:35:16 +01:00
if ( ellipse ) {
2012-05-14 12:50:33 +02:00
char * e ;
size_t emax , sl ;
int c ;
2012-01-05 15:35:16 +01:00
c = fd_columns ( fd ) ;
if ( c < = 0 )
c = 80 ;
2012-01-05 03:24:39 +01:00
2012-09-17 18:23:10 +02:00
sl = status ? sizeof ( status_indent ) - 1 : 0 ;
2012-05-14 12:50:33 +02:00
emax = c - sl - 1 ;
if ( emax < 3 )
emax = 3 ;
2012-01-05 03:24:39 +01:00
2012-01-05 15:35:16 +01:00
e = ellipsize ( s , emax , 75 ) ;
if ( e ) {
free ( s ) ;
s = e ;
}
2012-01-05 03:24:39 +01:00
}
2013-02-27 22:52:43 +01:00
if ( prev_ephemeral )
IOVEC_SET_STRING ( iovec [ n + + ] , " \r " ANSI_ERASE_TO_END_OF_LINE ) ;
prev_ephemeral = ephemeral ;
2012-05-14 12:50:33 +02:00
if ( status ) {
if ( ! isempty ( status ) ) {
IOVEC_SET_STRING ( iovec [ n + + ] , " [ " ) ;
IOVEC_SET_STRING ( iovec [ n + + ] , status ) ;
IOVEC_SET_STRING ( iovec [ n + + ] , " ] " ) ;
} else
IOVEC_SET_STRING ( iovec [ n + + ] , status_indent ) ;
2012-01-05 03:24:39 +01:00
}
2012-05-14 12:50:33 +02:00
IOVEC_SET_STRING ( iovec [ n + + ] , s ) ;
2013-02-27 22:52:43 +01:00
if ( ! ephemeral )
IOVEC_SET_STRING ( iovec [ n + + ] , " \n " ) ;
2012-01-05 03:24:39 +01:00
2012-09-17 18:23:10 +02:00
if ( writev ( fd , iovec , n ) < 0 )
return - errno ;
2010-07-07 00:00:59 +02:00
2012-09-17 18:23:10 +02:00
return 0 ;
2010-07-07 00:00:59 +02:00
}
2013-02-27 22:52:43 +01:00
int status_printf ( const char * status , bool ellipse , bool ephemeral , const char * format , . . . ) {
2010-07-07 00:25:41 +02:00
va_list ap ;
2012-09-17 18:23:10 +02:00
int r ;
2010-07-07 00:25:41 +02:00
assert ( format ) ;
va_start ( ap , format ) ;
2013-02-27 22:52:43 +01:00
r = status_vprintf ( status , ellipse , ephemeral , format , ap ) ;
2010-07-07 00:25:41 +02:00
va_end ( ap ) ;
2012-09-17 18:23:10 +02:00
return r ;
2010-07-07 00:25:41 +02:00
}
2010-07-08 04:09:59 +02:00
char * replace_env ( const char * format , char * * env ) {
enum {
WORD ,
2010-07-21 02:57:35 +02:00
CURLY ,
2010-07-08 04:09:59 +02:00
VARIABLE
} state = WORD ;
const char * e , * word = format ;
char * r = NULL , * k ;
assert ( format ) ;
for ( e = format ; * e ; e + + ) {
switch ( state ) {
case WORD :
if ( * e = = ' $ ' )
2010-07-21 02:57:35 +02:00
state = CURLY ;
2010-07-08 04:09:59 +02:00
break ;
2010-07-21 02:57:35 +02:00
case CURLY :
if ( * e = = ' { ' ) {
2010-07-08 04:09:59 +02:00
if ( ! ( k = strnappend ( r , word , e - word - 1 ) ) )
goto fail ;
free ( r ) ;
r = k ;
word = e - 1 ;
state = VARIABLE ;
} else if ( * e = = ' $ ' ) {
if ( ! ( k = strnappend ( r , word , e - word ) ) )
goto fail ;
free ( r ) ;
r = k ;
word = e + 1 ;
state = WORD ;
} else
state = WORD ;
break ;
case VARIABLE :
2010-07-21 02:57:35 +02:00
if ( * e = = ' } ' ) {
2010-08-10 21:05:19 +02:00
const char * t ;
2010-07-08 04:09:59 +02:00
2013-02-11 03:46:08 +01:00
t = strempty ( strv_env_get_n ( env , word + 2 , e - word - 2 ) ) ;
2010-07-08 04:09:59 +02:00
2013-02-11 03:46:08 +01:00
k = strappend ( r , t ) ;
if ( ! k )
2010-08-10 21:05:19 +02:00
goto fail ;
2010-07-08 04:09:59 +02:00
2010-08-10 21:05:19 +02:00
free ( r ) ;
r = k ;
2010-07-08 04:09:59 +02:00
2010-08-10 21:05:19 +02:00
word = e + 1 ;
2010-07-08 04:09:59 +02:00
state = WORD ;
}
break ;
}
}
if ( ! ( k = strnappend ( r , word , e - word ) ) )
goto fail ;
free ( r ) ;
return k ;
fail :
free ( r ) ;
return NULL ;
}
char * * replace_env_argv ( char * * argv , char * * env ) {
char * * r , * * i ;
2010-07-21 02:57:35 +02:00
unsigned k = 0 , l = 0 ;
l = strv_length ( argv ) ;
2010-07-08 04:09:59 +02:00
2010-07-21 02:57:35 +02:00
if ( ! ( r = new ( char * , l + 1 ) ) )
2010-07-08 04:09:59 +02:00
return NULL ;
STRV_FOREACH ( i , argv ) {
2010-07-21 02:57:35 +02:00
/* If $FOO appears as single word, replace it by the split up variable */
2010-08-10 21:05:19 +02:00
if ( ( * i ) [ 0 ] = = ' $ ' & & ( * i ) [ 1 ] ! = ' { ' ) {
char * e ;
char * * w , * * m ;
unsigned q ;
2010-07-21 02:57:35 +02:00
2013-02-11 03:46:08 +01:00
e = strv_env_get ( env , * i + 1 ) ;
if ( e ) {
2010-07-21 02:57:35 +02:00
if ( ! ( m = strv_split_quoted ( e ) ) ) {
r [ k ] = NULL ;
strv_free ( r ) ;
return NULL ;
}
2010-08-10 21:05:19 +02:00
} else
m = NULL ;
2010-07-21 02:57:35 +02:00
2010-08-10 21:05:19 +02:00
q = strv_length ( m ) ;
l = l + q - 1 ;
2010-07-21 02:57:35 +02:00
2010-08-10 21:05:19 +02:00
if ( ! ( w = realloc ( r , sizeof ( char * ) * ( l + 1 ) ) ) ) {
r [ k ] = NULL ;
strv_free ( r ) ;
strv_free ( m ) ;
return NULL ;
}
2010-07-21 02:57:35 +02:00
2010-08-10 21:05:19 +02:00
r = w ;
if ( m ) {
2010-07-21 02:57:35 +02:00
memcpy ( r + k , m , q * sizeof ( char * ) ) ;
free ( m ) ;
}
2010-08-10 21:05:19 +02:00
k + = q ;
continue ;
2010-07-21 02:57:35 +02:00
}
/* If ${FOO} appears as part of a word, replace it by the variable as-is */
2010-07-08 04:09:59 +02:00
if ( ! ( r [ k + + ] = replace_env ( * i , env ) ) ) {
strv_free ( r ) ;
return NULL ;
}
}
r [ k ] = NULL ;
return r ;
}
2012-01-05 03:24:39 +01:00
int fd_columns ( int fd ) {
2013-03-25 00:59:00 +01:00
struct winsize ws = { } ;
2012-01-05 03:24:39 +01:00
if ( ioctl ( fd , TIOCGWINSZ , & ws ) < 0 )
return - errno ;
if ( ws . ws_col < = 0 )
return - EIO ;
return ws . ws_col ;
}
2012-10-18 23:50:26 +02:00
unsigned columns ( void ) {
2010-07-08 21:01:42 +02:00
const char * e ;
2012-11-19 15:25:36 +01:00
int c ;
2010-07-08 21:01:42 +02:00
2012-10-18 23:50:26 +02:00
if ( _likely_ ( cached_columns > 0 ) )
return cached_columns ;
2012-07-26 20:23:28 +02:00
2012-10-18 23:50:26 +02:00
c = 0 ;
e = getenv ( " COLUMNS " ) ;
if ( e )
2012-11-19 15:25:36 +01:00
safe_atoi ( e , & c ) ;
2010-07-08 21:01:42 +02:00
2012-10-18 23:50:26 +02:00
if ( c < = 0 )
c = fd_columns ( STDOUT_FILENO ) ;
2010-07-08 21:01:42 +02:00
2012-10-18 23:50:26 +02:00
if ( c < = 0 )
c = 80 ;
2012-07-26 20:23:28 +02:00
2012-10-18 23:50:26 +02:00
cached_columns = c ;
return c ;
2012-07-26 20:23:28 +02:00
}
2012-01-22 18:21:15 +01:00
int fd_lines ( int fd ) {
2013-03-25 00:59:00 +01:00
struct winsize ws = { } ;
2012-01-22 18:21:15 +01:00
if ( ioctl ( fd , TIOCGWINSZ , & ws ) < 0 )
return - errno ;
if ( ws . ws_row < = 0 )
return - EIO ;
return ws . ws_row ;
}
unsigned lines ( void ) {
const char * e ;
2012-10-19 00:06:47 +02:00
unsigned l ;
2012-01-22 18:21:15 +01:00
2012-10-19 00:06:47 +02:00
if ( _likely_ ( cached_lines > 0 ) )
return cached_lines ;
2012-01-22 18:21:15 +01:00
2012-10-19 00:06:47 +02:00
l = 0 ;
2012-01-22 18:21:15 +01:00
e = getenv ( " LINES " ) ;
if ( e )
2012-10-19 00:06:47 +02:00
safe_atou ( e , & l ) ;
2012-01-22 18:21:15 +01:00
2012-10-19 00:06:47 +02:00
if ( l < = 0 )
l = fd_lines ( STDOUT_FILENO ) ;
2012-01-22 18:21:15 +01:00
2012-10-19 00:06:47 +02:00
if ( l < = 0 )
l = 24 ;
2012-01-22 18:21:15 +01:00
2012-10-19 00:06:47 +02:00
cached_lines = l ;
return cached_lines ;
}
/* intended to be used as a SIGWINCH sighandler */
void columns_lines_cache_reset ( int signum ) {
cached_columns = 0 ;
cached_lines = 0 ;
}
bool on_tty ( void ) {
static int cached_on_tty = - 1 ;
if ( _unlikely_ ( cached_on_tty < 0 ) )
cached_on_tty = isatty ( STDOUT_FILENO ) > 0 ;
return cached_on_tty ;
2012-01-22 18:21:15 +01:00
}
2010-07-08 21:34:51 +02:00
int running_in_chroot ( void ) {
2013-03-25 00:59:00 +01:00
struct stat a = { } , b = { } ;
2010-07-08 21:34:51 +02:00
/* Only works as root */
if ( stat ( " /proc/1/root " , & a ) < 0 )
return - errno ;
if ( stat ( " / " , & b ) < 0 )
return - errno ;
return
a . st_dev ! = b . st_dev | |
a . st_ino ! = b . st_ino ;
}
2013-09-21 03:37:33 +02:00
static char * ascii_ellipsize_mem ( const char * s , size_t old_length , size_t new_length , unsigned percent ) {
2011-12-21 18:17:22 +01:00
size_t x ;
2010-07-20 20:33:19 +02:00
char * r ;
assert ( s ) ;
assert ( percent < = 100 ) ;
2011-12-21 18:17:22 +01:00
assert ( new_length > = 3 ) ;
2010-07-20 20:33:19 +02:00
2011-12-21 18:17:22 +01:00
if ( old_length < = 3 | | old_length < = new_length )
return strndup ( s , old_length ) ;
2010-07-20 20:33:19 +02:00
2011-12-21 18:17:22 +01:00
r = new0 ( char , new_length + 1 ) ;
if ( ! r )
logs-show: limit to 3 lines and use dots if not showing full message
So far, we would show up to 128 bytes from a message, simply
cutting of the rest. With multiline messages, it is quite common
for a message to be longer than that, and this model doesn't really
work anymore.
A new limit is added: up to 3 lines will be shown, unless --full is
used (c.f. first line below). The limit for bytes is extended to 300
bytes. An ellipsis will always be used, if some form of truncation
occurs. If the tail of the message is cut off, either because of
length or line limit, dots will be shown at the end of the last
line. If this last line is short, the dots will be simply appended. If
the last line is too long for that, it will be ellipsized with dots at
the very end.
Note that the limits are in bytes, not characters, and we suck at
outputting unicode strings (c.f. last three lines below).
Aug 11 10:46:21 fedora python[67]: test message
line
line...
Aug 11 10:50:47 fedora python[76]: test message word word word word word word word word word word word wor...
Aug 11 10:55:11 fedora python[83]: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
Aug 11 11:03:21 fedora python[90]: ąąąąąąąąąąąąąąąąąąąąąąąąąąąąąą...
Aug 11 11:03:53 fedora python[97]: aąąąąąąąąąąąąąąąąąąąąąąąąąąąąąą...
Aug 11 11:25:45 fedora python[121]: aąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąąą�...
2013-08-11 16:56:09 +02:00
return NULL ;
2010-07-20 20:33:19 +02:00
2011-12-21 18:17:22 +01:00
x = ( new_length * percent ) / 100 ;
2010-07-20 20:33:19 +02:00
2011-12-21 18:17:22 +01:00
if ( x > new_length - 3 )
x = new_length - 3 ;
2010-07-20 20:33:19 +02:00
memcpy ( r , s , x ) ;
r [ x ] = ' . ' ;
r [ x + 1 ] = ' . ' ;
r [ x + 2 ] = ' . ' ;
memcpy ( r + x + 3 ,
2011-12-21 18:17:22 +01:00
s + old_length - ( new_length - x - 3 ) ,
new_length - x - 3 ) ;
2010-07-20 20:33:19 +02:00
return r ;
}
2013-09-21 03:37:33 +02:00
char * ellipsize_mem ( const char * s , size_t old_length , size_t new_length , unsigned percent ) {
size_t x ;
char * e ;
const char * i , * j ;
unsigned k , len , len2 ;
assert ( s ) ;
assert ( percent < = 100 ) ;
assert ( new_length > = 3 ) ;
/* if no multibyte characters use ascii_ellipsize_mem for speed */
if ( ascii_is_valid ( s ) )
return ascii_ellipsize_mem ( s , old_length , new_length , percent ) ;
if ( old_length < = 3 | | old_length < = new_length )
return strndup ( s , old_length ) ;
x = ( new_length * percent ) / 100 ;
if ( x > new_length - 3 )
x = new_length - 3 ;
k = 0 ;
for ( i = s ; k < x & & i < s + old_length ; i = utf8_next_char ( i ) ) {
int c ;
c = utf8_encoded_to_unichar ( i ) ;
if ( c < 0 )
return NULL ;
k + = unichar_iswide ( c ) ? 2 : 1 ;
}
if ( k > x ) /* last character was wide and went over quota */
x + + ;
for ( j = s + old_length ; k < new_length & & j > i ; ) {
int c ;
j = utf8_prev_char ( j ) ;
c = utf8_encoded_to_unichar ( j ) ;
if ( c < 0 )
return NULL ;
k + = unichar_iswide ( c ) ? 2 : 1 ;
}
assert ( i < = j ) ;
/* we don't actually need to ellipsize */
if ( i = = j )
return memdup ( s , old_length + 1 ) ;
/* make space for ellipsis */
j = utf8_next_char ( j ) ;
len = i - s ;
len2 = s + old_length - j ;
e = new ( char , len + 3 + len2 + 1 ) ;
if ( ! e )
return NULL ;
/*
printf ( " old_length=%zu new_length=%zu x=%zu len=%u len2=%u k=%u \n " ,
old_length , new_length , x , len , len2 , k ) ;
*/
memcpy ( e , s , len ) ;
e [ len ] = 0xe2 ; /* tri-dot ellipsis: … */
e [ len + 1 ] = 0x80 ;
e [ len + 2 ] = 0xa6 ;
memcpy ( e + len + 3 , j , len2 + 1 ) ;
return e ;
}
2011-12-21 18:17:22 +01:00
char * ellipsize ( const char * s , size_t length , unsigned percent ) {
return ellipsize_mem ( s , strlen ( s ) , length , percent ) ;
}
2010-08-16 15:37:52 +02:00
int touch ( const char * path ) {
int fd ;
assert ( path ) ;
2012-09-14 10:36:50 +02:00
/* This just opens the file for writing, ensuring it
* exists . It doesn ' t call utimensat ( ) the way / usr / bin / touch
* does it . */
fd = open ( path , O_WRONLY | O_CREAT | O_CLOEXEC | O_NOCTTY , 0644 ) ;
if ( fd < 0 )
2010-08-16 15:37:52 +02:00
return - errno ;
close_nointr_nofail ( fd ) ;
return 0 ;
}
2010-08-11 23:31:07 +02:00
2010-09-16 00:36:41 +02:00
char * unquote ( const char * s , const char * quotes ) {
2010-08-20 02:46:15 +02:00
size_t l ;
assert ( s ) ;
2012-09-14 10:36:50 +02:00
/* This is rather stupid, simply removes the heading and
* trailing quotes if there is one . Doesn ' t care about
2012-09-19 22:01:31 +02:00
* escaping or anything . We should make this smarter one
* day . . . */
2012-09-14 10:36:50 +02:00
2012-01-18 16:39:04 +01:00
l = strlen ( s ) ;
if ( l < 2 )
2010-08-20 02:46:15 +02:00
return strdup ( s ) ;
2010-09-16 00:36:41 +02:00
if ( strchr ( quotes , s [ 0 ] ) & & s [ l - 1 ] = = s [ 0 ] )
2010-08-20 02:46:15 +02:00
return strndup ( s + 1 , l - 2 ) ;
return strdup ( s ) ;
}
2011-01-05 16:06:35 +01:00
char * normalize_env_assignment ( const char * s ) {
2012-09-19 22:01:31 +02:00
_cleanup_free_ char * name = NULL , * value = NULL , * p = NULL ;
char * eq , * r ;
2011-01-05 16:06:35 +01:00
2012-09-19 22:01:31 +02:00
eq = strchr ( s , ' = ' ) ;
if ( ! eq ) {
char * t ;
2011-01-05 16:06:35 +01:00
2012-09-19 22:01:31 +02:00
r = strdup ( s ) ;
if ( ! r )
2011-01-05 16:06:35 +01:00
return NULL ;
2012-09-19 22:01:31 +02:00
t = strstrip ( r ) ;
if ( t = = r )
return r ;
memmove ( r , t , strlen ( t ) + 1 ) ;
return r ;
2011-01-05 16:06:35 +01:00
}
2012-09-19 22:01:31 +02:00
name = strndup ( s , eq - s ) ;
if ( ! name )
2011-01-05 16:06:35 +01:00
return NULL ;
2012-09-19 22:01:31 +02:00
p = strdup ( eq + 1 ) ;
if ( ! p )
2011-01-05 16:06:35 +01:00
return NULL ;
value = unquote ( strstrip ( p ) , QUOTES ) ;
2012-09-19 22:01:31 +02:00
if ( ! value )
2011-01-05 16:06:35 +01:00
return NULL ;
2012-09-19 22:01:31 +02:00
if ( asprintf ( & r , " %s=%s " , strstrip ( name ) , value ) < 0 )
2011-01-05 16:06:35 +01:00
r = NULL ;
return r ;
}
2010-09-15 14:48:59 +02:00
int wait_for_terminate ( pid_t pid , siginfo_t * status ) {
2011-07-07 02:34:35 +02:00
siginfo_t dummy ;
2010-09-15 14:37:16 +02:00
assert ( pid > = 1 ) ;
2011-07-07 02:34:35 +02:00
if ( ! status )
status = & dummy ;
2010-09-15 14:37:16 +02:00
for ( ; ; ) {
2010-09-15 14:48:59 +02:00
zero ( * status ) ;
if ( waitid ( P_PID , pid , status , WEXITED ) < 0 ) {
2010-09-15 14:37:16 +02:00
if ( errno = = EINTR )
continue ;
return - errno ;
}
return 0 ;
}
}
2010-09-16 00:36:41 +02:00
int wait_for_terminate_and_warn ( const char * name , pid_t pid ) {
int r ;
siginfo_t status ;
assert ( name ) ;
assert ( pid > 1 ) ;
2012-09-06 01:23:41 +02:00
r = wait_for_terminate ( pid , & status ) ;
if ( r < 0 ) {
2010-09-16 00:36:41 +02:00
log_warning ( " Failed to wait for %s: %s " , name , strerror ( - r ) ) ;
return r ;
}
if ( status . si_code = = CLD_EXITED ) {
if ( status . si_status ! = 0 ) {
log_warning ( " %s failed with error code %i. " , name , status . si_status ) ;
2011-03-14 02:33:51 +01:00
return status . si_status ;
2010-09-16 00:36:41 +02:00
}
log_debug ( " %s succeeded. " , name ) ;
return 0 ;
} else if ( status . si_code = = CLD_KILLED | |
status . si_code = = CLD_DUMPED ) {
log_warning ( " %s terminated by signal %s. " , name , signal_to_string ( status . si_status ) ) ;
return - EPROTO ;
}
log_warning ( " %s failed due to unknown reason. " , name ) ;
return - EPROTO ;
}
2013-12-16 17:53:53 +01:00
noreturn void freeze ( void ) {
2011-03-11 00:52:13 +01:00
/* Make sure nobody waits for us on a socket anymore */
close_all_fds ( NULL , 0 ) ;
2011-01-01 19:50:32 +01:00
sync ( ) ;
2010-10-07 19:34:56 +02:00
for ( ; ; )
pause ( ) ;
}
2010-10-08 02:31:36 +02:00
bool null_or_empty ( struct stat * st ) {
assert ( st ) ;
if ( S_ISREG ( st - > st_mode ) & & st - > st_size < = 0 )
return true ;
2010-10-08 18:22:28 +02:00
if ( S_ISCHR ( st - > st_mode ) | | S_ISBLK ( st - > st_mode ) )
2010-10-08 02:31:36 +02:00
return true ;
return false ;
}
2011-07-22 04:21:18 +02:00
int null_or_empty_path ( const char * fn ) {
struct stat st ;
assert ( fn ) ;
if ( stat ( fn , & st ) < 0 )
return - errno ;
return null_or_empty ( & st ) ;
}
2010-12-28 14:20:21 +01:00
DIR * xopendirat ( int fd , const char * name , int flags ) {
2011-01-05 16:17:26 +01:00
int nfd ;
DIR * d ;
2013-07-17 02:52:41 +02:00
assert ( ! ( flags & O_CREAT ) ) ;
nfd = openat ( fd , name , O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | flags , 0 ) ;
2012-09-14 10:36:50 +02:00
if ( nfd < 0 )
2011-01-05 16:17:26 +01:00
return NULL ;
2012-09-14 10:36:50 +02:00
d = fdopendir ( nfd ) ;
if ( ! d ) {
2011-01-05 16:17:26 +01:00
close_nointr_nofail ( nfd ) ;
return NULL ;
}
return d ;
2010-10-18 22:38:41 +02:00
}
2010-10-22 16:11:50 +02:00
int signal_from_string_try_harder ( const char * s ) {
int signo ;
assert ( s ) ;
2012-09-14 10:36:50 +02:00
signo = signal_from_string ( s ) ;
if ( signo < = 0 )
2010-10-22 16:11:50 +02:00
if ( startswith ( s , " SIG " ) )
return signal_from_string ( s + 3 ) ;
return signo ;
}
2012-08-11 19:31:50 +02:00
static char * tag_to_udev_node ( const char * tagvalue , const char * by ) {
2013-09-17 21:47:08 +02:00
_cleanup_free_ char * t = NULL , * u = NULL ;
char * dn ;
size_t enc_len ;
2010-11-08 05:02:45 +01:00
2012-08-11 19:31:50 +02:00
u = unquote ( tagvalue , " \" \' " ) ;
if ( u = = NULL )
return NULL ;
2010-11-08 05:02:45 +01:00
2013-10-07 00:26:23 +02:00
enc_len = strlen ( u ) * 4 + 1 ;
2013-09-17 21:47:08 +02:00
t = new ( char , enc_len ) ;
2012-08-11 19:31:50 +02:00
if ( t = = NULL )
return NULL ;
2010-11-08 05:02:45 +01:00
2013-09-18 18:12:04 +02:00
if ( encode_devnode_name ( u , t , enc_len ) < 0 )
2013-09-17 21:47:08 +02:00
return NULL ;
2010-11-08 05:02:45 +01:00
2013-09-17 21:47:08 +02:00
if ( asprintf ( & dn , " /dev/disk/by-%s/%s " , by , t ) < 0 )
2012-08-11 19:31:50 +02:00
return NULL ;
2010-11-08 05:02:45 +01:00
2012-08-11 19:31:50 +02:00
return dn ;
}
2010-11-08 05:02:45 +01:00
2012-08-11 19:31:50 +02:00
char * fstab_node_to_udev_node ( const char * p ) {
2012-09-17 21:58:03 +02:00
assert ( p ) ;
2012-08-11 19:31:50 +02:00
if ( startswith ( p , " LABEL= " ) )
return tag_to_udev_node ( p + 6 , " label " ) ;
2010-11-08 05:02:45 +01:00
2012-08-11 19:31:50 +02:00
if ( startswith ( p , " UUID= " ) )
return tag_to_udev_node ( p + 5 , " uuid " ) ;
2010-11-08 05:02:45 +01:00
2012-08-11 19:32:29 +02:00
if ( startswith ( p , " PARTUUID= " ) )
return tag_to_udev_node ( p + 9 , " partuuid " ) ;
if ( startswith ( p , " PARTLABEL= " ) )
return tag_to_udev_node ( p + 10 , " partlabel " ) ;
2010-11-08 05:02:45 +01:00
return strdup ( p ) ;
}
2011-02-14 18:56:51 +01:00
bool tty_is_vc ( const char * tty ) {
assert ( tty ) ;
if ( startswith ( tty , " /dev/ " ) )
tty + = 5 ;
2011-06-24 18:50:50 +02:00
return vtnr_from_tty ( tty ) > = 0 ;
}
2012-04-22 02:30:13 +02:00
bool tty_is_console ( const char * tty ) {
assert ( tty ) ;
if ( startswith ( tty , " /dev/ " ) )
tty + = 5 ;
return streq ( tty , " console " ) ;
}
2011-06-24 18:50:50 +02:00
int vtnr_from_tty ( const char * tty ) {
int i , r ;
assert ( tty ) ;
if ( startswith ( tty , " /dev/ " ) )
tty + = 5 ;
if ( ! startswith ( tty , " tty " ) )
return - EINVAL ;
if ( tty [ 3 ] < ' 0 ' | | tty [ 3 ] > ' 9 ' )
return - EINVAL ;
r = safe_atoi ( tty + 3 , & i ) ;
if ( r < 0 )
return r ;
if ( i < 0 | | i > 63 )
return - EINVAL ;
return i ;
2011-02-14 18:56:51 +01:00
}
2013-02-28 01:30:38 +01:00
char * resolve_dev_console ( char * * active ) {
char * tty ;
/* Resolve where /dev/console is pointing to, if /sys is actually ours
* ( i . e . not read - only - mounted which is a sign for container setups ) */
if ( path_is_read_only_fs ( " /sys " ) > 0 )
return NULL ;
if ( read_one_line_file ( " /sys/class/tty/console/active " , active ) < 0 )
return NULL ;
/* If multiple log outputs are configured the last one is what
* / dev / console points to */
tty = strrchr ( * active , ' ' ) ;
if ( tty )
tty + + ;
else
tty = * active ;
2013-09-20 22:18:28 +02:00
if ( streq ( tty , " tty0 " ) ) {
char * tmp ;
/* Get the active VC (e.g. tty1) */
if ( read_one_line_file ( " /sys/class/tty/tty0/active " , & tmp ) > = 0 ) {
free ( * active ) ;
tty = * active = tmp ;
}
}
2013-02-28 01:30:38 +01:00
return tty ;
}
2012-01-13 21:56:28 +01:00
bool tty_is_vc_resolve ( const char * tty ) {
2013-11-08 18:11:09 +01:00
_cleanup_free_ char * active = NULL ;
2011-02-13 19:01:47 +01:00
2010-11-15 23:49:02 +01:00
assert ( tty ) ;
if ( startswith ( tty , " /dev/ " ) )
tty + = 5 ;
2013-02-28 01:30:38 +01:00
if ( streq ( tty , " console " ) ) {
tty = resolve_dev_console ( & active ) ;
if ( ! tty )
return false ;
}
2011-02-13 19:01:47 +01:00
2013-11-08 18:11:09 +01:00
return tty_is_vc ( tty ) ;
2012-01-13 21:56:28 +01:00
}
const char * default_term_for_tty ( const char * tty ) {
assert ( tty ) ;
2012-04-22 02:45:39 +02:00
return tty_is_vc_resolve ( tty ) ? " TERM=linux " : " TERM=vt102 " ;
2010-11-15 23:49:02 +01:00
}
2011-10-07 21:06:39 +02:00
bool dirent_is_file ( const struct dirent * de ) {
2011-05-23 21:39:15 +02:00
assert ( de ) ;
if ( ignore_file ( de - > d_name ) )
return false ;
if ( de - > d_type ! = DT_REG & &
de - > d_type ! = DT_LNK & &
de - > d_type ! = DT_UNKNOWN )
return false ;
return true ;
}
2011-10-07 21:06:39 +02:00
bool dirent_is_file_with_suffix ( const struct dirent * de , const char * suffix ) {
assert ( de ) ;
2012-08-21 02:13:21 +02:00
if ( de - > d_type ! = DT_REG & &
de - > d_type ! = DT_LNK & &
de - > d_type ! = DT_UNKNOWN )
return false ;
if ( ignore_file_allow_backup ( de - > d_name ) )
2011-10-07 21:06:39 +02:00
return false ;
return endswith ( de - > d_name , suffix ) ;
}
2011-02-15 00:30:11 +01:00
void execute_directory ( const char * directory , DIR * d , char * argv [ ] ) {
DIR * _d = NULL ;
struct dirent * de ;
Hashmap * pids = NULL ;
assert ( directory ) ;
2013-03-07 15:12:46 +01:00
/* Executes all binaries in a directory in parallel and
* waits for them to finish . */
2011-02-15 00:30:11 +01:00
if ( ! d ) {
if ( ! ( _d = opendir ( directory ) ) ) {
if ( errno = = ENOENT )
return ;
log_error ( " Failed to enumerate directory %s: %m " , directory ) ;
return ;
}
d = _d ;
}
if ( ! ( pids = hashmap_new ( trivial_hash_func , trivial_compare_func ) ) ) {
log_error ( " Failed to allocate set. " ) ;
goto finish ;
}
while ( ( de = readdir ( d ) ) ) {
char * path ;
pid_t pid ;
int k ;
2011-05-23 21:39:15 +02:00
if ( ! dirent_is_file ( de ) )
2011-02-15 00:30:11 +01:00
continue ;
if ( asprintf ( & path , " %s/%s " , directory , de - > d_name ) < 0 ) {
2012-07-25 23:55:59 +02:00
log_oom ( ) ;
2011-02-15 00:30:11 +01:00
continue ;
}
if ( ( pid = fork ( ) ) < 0 ) {
log_error ( " Failed to fork: %m " ) ;
free ( path ) ;
continue ;
}
if ( pid = = 0 ) {
char * _argv [ 2 ] ;
/* Child */
if ( ! argv ) {
_argv [ 0 ] = path ;
_argv [ 1 ] = NULL ;
argv = _argv ;
} else
2012-05-05 02:06:58 +02:00
argv [ 0 ] = path ;
2011-02-15 00:30:11 +01:00
execv ( path , argv ) ;
log_error ( " Failed to execute %s: %m " , path ) ;
_exit ( EXIT_FAILURE ) ;
}
log_debug ( " Spawned %s as %lu " , path , ( unsigned long ) pid ) ;
if ( ( k = hashmap_put ( pids , UINT_TO_PTR ( pid ) , path ) ) < 0 ) {
log_error ( " Failed to add PID to set: %s " , strerror ( - k ) ) ;
free ( path ) ;
}
}
while ( ! hashmap_isempty ( pids ) ) {
2012-02-02 18:32:05 +01:00
pid_t pid = PTR_TO_UINT ( hashmap_first_key ( pids ) ) ;
2013-03-25 00:59:00 +01:00
siginfo_t si = { } ;
2011-02-15 00:30:11 +01:00
char * path ;
2012-02-02 18:32:05 +01:00
if ( waitid ( P_PID , pid , & si , WEXITED ) < 0 ) {
2011-02-15 00:30:11 +01:00
if ( errno = = EINTR )
continue ;
log_error ( " waitid() failed: %m " ) ;
goto finish ;
}
if ( ( path = hashmap_remove ( pids , UINT_TO_PTR ( si . si_pid ) ) ) ) {
2012-08-13 13:58:01 +02:00
if ( ! is_clean_exit ( si . si_code , si . si_status , NULL ) ) {
2011-02-15 00:30:11 +01:00
if ( si . si_code = = CLD_EXITED )
log_error ( " %s exited with exit status %i. " , path , si . si_status ) ;
else
log_error ( " %s terminated by signal %s. " , path , signal_to_string ( si . si_status ) ) ;
} else
log_debug ( " %s exited successfully. " , path ) ;
free ( path ) ;
}
}
finish :
if ( _d )
closedir ( _d ) ;
if ( pids )
hashmap_free_free ( pids ) ;
}
2011-03-03 23:55:30 +01:00
int kill_and_sigcont ( pid_t pid , int sig ) {
int r ;
r = kill ( pid , sig ) < 0 ? - errno : 0 ;
if ( r > = 0 )
kill ( pid , SIGCONT ) ;
return r ;
}
2011-03-09 20:01:53 +01:00
bool nulstr_contains ( const char * nulstr , const char * needle ) {
const char * i ;
if ( ! nulstr )
return false ;
NULSTR_FOREACH ( i , nulstr )
if ( streq ( i , needle ) )
return true ;
return false ;
}
2011-03-30 02:21:48 +02:00
bool plymouth_running ( void ) {
2011-03-31 04:26:40 +02:00
return access ( " /run/plymouth/pid " , F_OK ) > = 0 ;
2011-03-30 02:21:48 +02:00
}
2011-04-16 01:57:23 +02:00
char * strshorten ( char * s , size_t l ) {
assert ( s ) ;
if ( l < strlen ( s ) )
s [ l ] = 0 ;
return s ;
}
static bool hostname_valid_char ( char c ) {
return
( c > = ' a ' & & c < = ' z ' ) | |
( c > = ' A ' & & c < = ' Z ' ) | |
( c > = ' 0 ' & & c < = ' 9 ' ) | |
c = = ' - ' | |
c = = ' _ ' | |
c = = ' . ' ;
}
bool hostname_is_valid ( const char * s ) {
const char * p ;
2013-03-22 17:59:49 +01:00
bool dot ;
2011-04-16 01:57:23 +02:00
if ( isempty ( s ) )
return false ;
2013-03-22 17:59:49 +01:00
for ( p = s , dot = true ; * p ; p + + ) {
if ( * p = = ' . ' ) {
if ( dot )
return false ;
dot = true ;
} else {
if ( ! hostname_valid_char ( * p ) )
return false ;
dot = false ;
}
}
if ( dot )
return false ;
2011-04-16 01:57:23 +02:00
if ( p - s > HOST_NAME_MAX )
return false ;
return true ;
}
2013-05-07 20:55:11 +02:00
char * hostname_cleanup ( char * s , bool lowercase ) {
2011-04-16 01:57:23 +02:00
char * p , * d ;
2013-04-16 03:57:50 +02:00
bool dot ;
for ( p = s , d = s , dot = true ; * p ; p + + ) {
if ( * p = = ' . ' ) {
2013-05-07 20:55:11 +02:00
if ( dot )
2013-04-16 03:57:50 +02:00
continue ;
2011-04-16 01:57:23 +02:00
2013-05-07 20:55:11 +02:00
* ( d + + ) = ' . ' ;
2013-04-16 03:57:50 +02:00
dot = true ;
2013-05-07 20:55:11 +02:00
} else if ( hostname_valid_char ( * p ) ) {
* ( d + + ) = lowercase ? tolower ( * p ) : * p ;
2013-04-16 03:57:50 +02:00
dot = false ;
2013-05-07 20:55:11 +02:00
}
2013-04-16 03:57:50 +02:00
}
2011-04-16 01:57:23 +02:00
2013-05-07 20:55:11 +02:00
if ( dot & & d > s )
d [ - 1 ] = 0 ;
else
* d = 0 ;
2011-04-16 01:57:23 +02:00
strshorten ( s , HOST_NAME_MAX ) ;
2013-04-16 03:57:50 +02:00
2011-04-16 01:57:23 +02:00
return s ;
}
2011-05-23 23:54:00 +02:00
int pipe_eof ( int fd ) {
2013-03-25 00:59:00 +01:00
struct pollfd pollfd = {
. fd = fd ,
. events = POLLIN | POLLHUP ,
} ;
2011-05-23 23:54:00 +02:00
2014-01-28 13:08:34 +01:00
int r ;
2011-05-23 23:54:00 +02:00
r = poll ( & pollfd , 1 , 0 ) ;
if ( r < 0 )
return - errno ;
if ( r = = 0 )
return 0 ;
return pollfd . revents & POLLHUP ;
}
2012-01-22 18:21:15 +01:00
int fd_wait_for_event ( int fd , int event , usec_t t ) {
2014-01-27 20:12:14 +01:00
2013-03-25 00:59:00 +01:00
struct pollfd pollfd = {
. fd = fd ,
. events = event ,
} ;
2012-01-04 18:33:36 +01:00
2014-01-27 20:12:14 +01:00
struct timespec ts ;
int r ;
r = ppoll ( & pollfd , 1 , t = = ( usec_t ) - 1 ? NULL : timespec_store ( & ts , t ) , NULL ) ;
2012-01-04 18:33:36 +01:00
if ( r < 0 )
return - errno ;
if ( r = = 0 )
return 0 ;
return pollfd . revents ;
}
2011-05-25 00:54:32 +02:00
int fopen_temporary ( const char * path , FILE * * _f , char * * _temp_path ) {
FILE * f ;
char * t ;
const char * fn ;
size_t k ;
int fd ;
assert ( path ) ;
assert ( _f ) ;
assert ( _temp_path ) ;
t = new ( char , strlen ( path ) + 1 + 6 + 1 ) ;
if ( ! t )
return - ENOMEM ;
2013-12-07 03:29:55 +01:00
fn = basename ( path ) ;
k = fn - path ;
2011-05-25 00:54:32 +02:00
memcpy ( t , path , k ) ;
t [ k ] = ' . ' ;
stpcpy ( stpcpy ( t + k + 1 , fn ) , " XXXXXX " ) ;
2014-01-28 13:47:35 +01:00
fd = mkostemp_safe ( t , O_WRONLY | O_CLOEXEC ) ;
2011-05-25 00:54:32 +02:00
if ( fd < 0 ) {
free ( t ) ;
return - errno ;
}
f = fdopen ( fd , " we " ) ;
if ( ! f ) {
unlink ( t ) ;
free ( t ) ;
return - errno ;
}
* _f = f ;
* _temp_path = t ;
return 0 ;
}
2011-05-18 01:07:31 +02:00
int terminal_vhangup_fd ( int fd ) {
2011-05-25 00:54:32 +02:00
assert ( fd > = 0 ) ;
2011-05-18 01:07:31 +02:00
if ( ioctl ( fd , TIOCVHANGUP ) < 0 )
return - errno ;
return 0 ;
}
int terminal_vhangup ( const char * name ) {
int fd , r ;
fd = open_terminal ( name , O_RDWR | O_NOCTTY | O_CLOEXEC ) ;
if ( fd < 0 )
return fd ;
r = terminal_vhangup_fd ( fd ) ;
close_nointr_nofail ( fd ) ;
return r ;
}
int vt_disallocate ( const char * name ) {
int fd , r ;
unsigned u ;
/* Deallocate the VT if possible. If not possible
* ( i . e . because it is the active one ) , at least clear it
* entirely ( including the scrollback buffer ) */
2011-05-20 14:37:14 +02:00
if ( ! startswith ( name , " /dev/ " ) )
return - EINVAL ;
if ( ! tty_is_vc ( name ) ) {
/* So this is not a VT. I guess we cannot deallocate
* it then . But let ' s at least clear the screen */
fd = open_terminal ( name , O_RDWR | O_NOCTTY | O_CLOEXEC ) ;
if ( fd < 0 )
return fd ;
2011-06-29 21:17:31 +02:00
loop_write ( fd ,
" \033 [r " /* clear scrolling region */
" \033 [H " /* move home */
" \033 [2J " , /* clear screen */
10 , false ) ;
2011-05-20 14:37:14 +02:00
close_nointr_nofail ( fd ) ;
return 0 ;
}
2011-05-18 01:07:31 +02:00
if ( ! startswith ( name , " /dev/tty " ) )
return - EINVAL ;
r = safe_atou ( name + 8 , & u ) ;
if ( r < 0 )
return r ;
if ( u < = 0 )
2011-05-20 14:37:14 +02:00
return - EINVAL ;
2011-05-18 01:07:31 +02:00
2011-05-20 14:37:14 +02:00
/* Try to deallocate */
2011-05-18 01:07:31 +02:00
fd = open_terminal ( " /dev/tty0 " , O_RDWR | O_NOCTTY | O_CLOEXEC ) ;
if ( fd < 0 )
return fd ;
r = ioctl ( fd , VT_DISALLOCATE , u ) ;
2011-05-20 14:37:14 +02:00
close_nointr_nofail ( fd ) ;
2011-05-18 01:07:31 +02:00
2011-05-20 14:37:14 +02:00
if ( r > = 0 )
return 0 ;
2011-05-18 01:07:31 +02:00
2011-05-20 14:37:14 +02:00
if ( errno ! = EBUSY )
2011-05-18 01:07:31 +02:00
return - errno ;
2011-05-20 14:37:14 +02:00
/* Couldn't deallocate, so let's clear it fully with
* scrollback */
fd = open_terminal ( name , O_RDWR | O_NOCTTY | O_CLOEXEC ) ;
2011-05-18 01:07:31 +02:00
if ( fd < 0 )
2011-05-20 14:37:14 +02:00
return fd ;
2011-05-18 01:07:31 +02:00
2011-06-29 21:17:31 +02:00
loop_write ( fd ,
" \033 [r " /* clear scrolling region */
" \033 [H " /* move home */
" \033 [3J " , /* clear screen including scrollback, requires Linux 2.6.40 */
10 , false ) ;
2011-05-20 14:37:14 +02:00
close_nointr_nofail ( fd ) ;
2011-05-18 01:07:31 +02:00
2011-05-20 14:37:14 +02:00
return 0 ;
2011-05-18 01:07:31 +02:00
}
2013-10-02 19:40:43 +02:00
int copy_file ( const char * from , const char * to , int flags ) {
_cleanup_close_ int fdf = - 1 ;
int r , fdt ;
2011-06-15 15:35:23 +02:00
assert ( from ) ;
assert ( to ) ;
fdf = open ( from , O_RDONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( fdf < 0 )
return - errno ;
2013-10-02 19:40:43 +02:00
fdt = open ( to , flags | O_WRONLY | O_CREAT | O_CLOEXEC | O_NOCTTY , 0644 ) ;
if ( fdt < 0 )
2011-06-15 15:35:23 +02:00
return - errno ;
for ( ; ; ) {
char buf [ PIPE_BUF ] ;
ssize_t n , k ;
n = read ( fdf , buf , sizeof ( buf ) ) ;
if ( n < 0 ) {
r = - errno ;
close_nointr ( fdt ) ;
unlink ( to ) ;
return r ;
}
if ( n = = 0 )
break ;
errno = 0 ;
k = loop_write ( fdt , buf , n , false ) ;
if ( n ! = k ) {
r = k < 0 ? k : ( errno ? - errno : - EIO ) ;
close_nointr ( fdt ) ;
unlink ( to ) ;
2013-10-02 19:40:43 +02:00
2011-06-15 15:35:23 +02:00
return r ;
}
}
r = close_nointr ( fdt ) ;
if ( r < 0 ) {
unlink ( to ) ;
return r ;
}
return 0 ;
}
2012-09-14 20:02:52 +02:00
int symlink_atomic ( const char * from , const char * to ) {
char * x ;
_cleanup_free_ char * t ;
2011-06-15 15:35:23 +02:00
const char * fn ;
size_t k ;
2013-12-22 19:59:12 +01:00
uint64_t u ;
2011-06-15 15:35:23 +02:00
unsigned i ;
int r ;
assert ( from ) ;
assert ( to ) ;
t = new ( char , strlen ( to ) + 1 + 16 + 1 ) ;
if ( ! t )
return - ENOMEM ;
2013-12-07 03:29:55 +01:00
fn = basename ( to ) ;
2011-06-15 15:35:23 +02:00
k = fn - to ;
memcpy ( t , to , k ) ;
t [ k ] = ' . ' ;
x = stpcpy ( t + k + 1 , fn ) ;
2013-12-22 19:59:12 +01:00
u = random_u64 ( ) ;
2011-06-15 15:35:23 +02:00
for ( i = 0 ; i < 16 ; i + + ) {
2013-12-22 19:59:12 +01:00
* ( x + + ) = hexchar ( u & 0xF ) ;
u > > = 4 ;
2011-06-15 15:35:23 +02:00
}
* x = 0 ;
2012-09-14 20:02:52 +02:00
if ( symlink ( from , t ) < 0 )
return - errno ;
2011-06-15 15:35:23 +02:00
if ( rename ( t , to ) < 0 ) {
r = - errno ;
unlink ( t ) ;
return r ;
}
2012-09-14 20:02:52 +02:00
return 0 ;
2011-06-15 15:35:23 +02:00
}
2011-06-27 22:44:12 +02:00
bool display_is_local ( const char * display ) {
assert ( display ) ;
return
display [ 0 ] = = ' : ' & &
display [ 1 ] > = ' 0 ' & &
display [ 1 ] < = ' 9 ' ;
}
int socket_from_display ( const char * display , char * * path ) {
size_t k ;
char * f , * c ;
assert ( display ) ;
assert ( path ) ;
if ( ! display_is_local ( display ) )
return - EINVAL ;
k = strspn ( display + 1 , " 0123456789 " ) ;
f = new ( char , sizeof ( " /tmp/.X11-unix/X " ) + k ) ;
if ( ! f )
return - ENOMEM ;
c = stpcpy ( f , " /tmp/.X11-unix/X " ) ;
memcpy ( c , display + 1 , k ) ;
c [ k ] = 0 ;
* path = f ;
return 0 ;
}
2012-07-16 12:34:54 +02:00
int get_user_creds (
const char * * username ,
uid_t * uid , gid_t * gid ,
const char * * home ,
const char * * shell ) {
2011-07-01 23:49:56 +02:00
struct passwd * p ;
2011-07-23 00:47:17 +02:00
uid_t u ;
2011-07-01 23:49:56 +02:00
assert ( username ) ;
assert ( * username ) ;
/* We enforce some special rules for uid=0: in order to avoid
* NSS lookups for root we hardcode its data . */
if ( streq ( * username , " root " ) | | streq ( * username , " 0 " ) ) {
* username = " root " ;
2011-07-23 01:17:59 +02:00
if ( uid )
* uid = 0 ;
if ( gid )
* gid = 0 ;
if ( home )
* home = " /root " ;
2012-07-16 12:34:54 +02:00
if ( shell )
* shell = " /bin/sh " ;
2011-07-01 23:49:56 +02:00
return 0 ;
}
2011-07-23 00:47:17 +02:00
if ( parse_uid ( * username , & u ) > = 0 ) {
2011-07-01 23:49:56 +02:00
errno = 0 ;
2011-07-23 00:47:17 +02:00
p = getpwuid ( u ) ;
2011-07-01 23:49:56 +02:00
/* If there are multiple users with the same id, make
* sure to leave $ USER to the configured value instead
* of the first occurrence in the database . However if
* the uid was configured by a numeric uid , then let ' s
* pick the real username from / etc / passwd . */
if ( p )
* username = p - > pw_name ;
} else {
errno = 0 ;
p = getpwnam ( * username ) ;
}
if ( ! p )
2013-03-28 14:24:15 +01:00
return errno > 0 ? - errno : - ESRCH ;
2011-07-01 23:49:56 +02:00
2011-07-23 01:17:59 +02:00
if ( uid )
* uid = p - > pw_uid ;
if ( gid )
* gid = p - > pw_gid ;
if ( home )
* home = p - > pw_dir ;
2012-07-16 12:34:54 +02:00
if ( shell )
* shell = p - > pw_shell ;
2011-07-23 01:17:59 +02:00
return 0 ;
}
2013-01-15 03:00:43 +01:00
char * uid_to_name ( uid_t uid ) {
struct passwd * p ;
char * r ;
if ( uid = = 0 )
return strdup ( " root " ) ;
p = getpwuid ( uid ) ;
if ( p )
return strdup ( p - > pw_name ) ;
if ( asprintf ( & r , " %lu " , ( unsigned long ) uid ) < 0 )
return NULL ;
return r ;
}
2013-03-22 17:44:15 +01:00
char * gid_to_name ( gid_t gid ) {
struct group * p ;
char * r ;
if ( gid = = 0 )
return strdup ( " root " ) ;
p = getgrgid ( gid ) ;
if ( p )
return strdup ( p - > gr_name ) ;
if ( asprintf ( & r , " %lu " , ( unsigned long ) gid ) < 0 )
return NULL ;
return r ;
}
2011-07-23 01:17:59 +02:00
int get_group_creds ( const char * * groupname , gid_t * gid ) {
struct group * g ;
gid_t id ;
assert ( groupname ) ;
/* We enforce some special rules for gid=0: in order to avoid
* NSS lookups for root we hardcode its data . */
if ( streq ( * groupname , " root " ) | | streq ( * groupname , " 0 " ) ) {
* groupname = " root " ;
if ( gid )
* gid = 0 ;
return 0 ;
}
if ( parse_gid ( * groupname , & id ) > = 0 ) {
errno = 0 ;
g = getgrgid ( id ) ;
if ( g )
* groupname = g - > gr_name ;
} else {
errno = 0 ;
g = getgrnam ( * groupname ) ;
}
if ( ! g )
2013-03-28 14:24:15 +01:00
return errno > 0 ? - errno : - ESRCH ;
2011-07-23 01:17:59 +02:00
if ( gid )
* gid = g - > gr_gid ;
2011-07-01 23:49:56 +02:00
return 0 ;
}
2013-03-22 17:44:15 +01:00
int in_gid ( gid_t gid ) {
gid_t * gids ;
2012-03-14 19:54:22 +01:00
int ngroups_max , r , i ;
if ( getgid ( ) = = gid )
return 1 ;
if ( getegid ( ) = = gid )
return 1 ;
ngroups_max = sysconf ( _SC_NGROUPS_MAX ) ;
assert ( ngroups_max > 0 ) ;
gids = alloca ( sizeof ( gid_t ) * ngroups_max ) ;
r = getgroups ( ngroups_max , gids ) ;
if ( r < 0 )
return - errno ;
for ( i = 0 ; i < r ; i + + )
if ( gids [ i ] = = gid )
return 1 ;
return 0 ;
}
2013-03-22 17:44:15 +01:00
int in_group ( const char * name ) {
int r ;
gid_t gid ;
r = get_group_creds ( & name , & gid ) ;
if ( r < 0 )
return r ;
return in_gid ( gid ) ;
}
2011-07-07 02:07:39 +02:00
int glob_exists ( const char * path ) {
2013-04-18 09:11:22 +02:00
_cleanup_globfree_ glob_t g = { } ;
2013-06-06 01:30:17 +02:00
int k ;
2011-07-07 02:07:39 +02:00
assert ( path ) ;
errno = 0 ;
k = glob ( path , GLOB_NOSORT | GLOB_BRACE , NULL , & g ) ;
if ( k = = GLOB_NOMATCH )
2013-06-06 01:30:17 +02:00
return 0 ;
2011-07-07 02:07:39 +02:00
else if ( k = = GLOB_NOSPACE )
2013-06-06 01:30:17 +02:00
return - ENOMEM ;
2011-07-07 02:07:39 +02:00
else if ( k = = 0 )
2013-06-06 01:30:17 +02:00
return ! strv_isempty ( g . gl_pathv ) ;
2011-07-07 02:07:39 +02:00
else
2013-06-06 01:30:17 +02:00
return errno ? - errno : - EIO ;
}
2011-07-07 02:07:39 +02:00
2013-06-06 01:30:17 +02:00
int glob_extend ( char * * * strv , const char * path ) {
_cleanup_globfree_ glob_t g = { } ;
int k ;
char * * p ;
errno = 0 ;
2013-10-14 08:15:51 +02:00
k = glob ( path , GLOB_NOSORT | GLOB_BRACE , NULL , & g ) ;
2013-06-06 01:30:17 +02:00
if ( k = = GLOB_NOMATCH )
return - ENOENT ;
else if ( k = = GLOB_NOSPACE )
return - ENOMEM ;
else if ( k ! = 0 | | strv_isempty ( g . gl_pathv ) )
return errno ? - errno : - EIO ;
STRV_FOREACH ( p , g . gl_pathv ) {
k = strv_extend ( strv , * p ) ;
if ( k < 0 )
break ;
}
return k ;
2011-07-07 02:07:39 +02:00
}
2011-07-22 04:21:18 +02:00
int dirent_ensure_type ( DIR * d , struct dirent * de ) {
struct stat st ;
assert ( d ) ;
assert ( de ) ;
if ( de - > d_type ! = DT_UNKNOWN )
return 0 ;
if ( fstatat ( dirfd ( d ) , de - > d_name , & st , AT_SYMLINK_NOFOLLOW ) < 0 )
return - errno ;
de - > d_type =
S_ISREG ( st . st_mode ) ? DT_REG :
S_ISDIR ( st . st_mode ) ? DT_DIR :
S_ISLNK ( st . st_mode ) ? DT_LNK :
S_ISFIFO ( st . st_mode ) ? DT_FIFO :
S_ISSOCK ( st . st_mode ) ? DT_SOCK :
S_ISCHR ( st . st_mode ) ? DT_CHR :
S_ISBLK ( st . st_mode ) ? DT_BLK :
DT_UNKNOWN ;
return 0 ;
}
int in_search_path ( const char * path , char * * search ) {
2013-09-29 14:40:58 +02:00
char * * i ;
_cleanup_free_ char * parent = NULL ;
2011-07-22 04:21:18 +02:00
int r ;
2012-05-07 21:36:12 +02:00
r = path_get_parent ( path , & parent ) ;
2011-07-22 04:21:18 +02:00
if ( r < 0 )
return r ;
2013-09-29 14:40:58 +02:00
STRV_FOREACH ( i , search )
if ( path_equal ( parent , * i ) )
return 1 ;
2011-07-22 04:21:18 +02:00
2013-09-29 14:40:58 +02:00
return 0 ;
2011-07-22 04:21:18 +02:00
}
2011-07-22 21:01:15 +02:00
int get_files_in_directory ( const char * path , char * * * list ) {
2013-09-29 14:40:58 +02:00
_cleanup_closedir_ DIR * d = NULL ;
size_t bufsize = 0 , n = 0 ;
_cleanup_strv_free_ char * * l = NULL ;
2011-07-22 21:01:15 +02:00
assert ( path ) ;
2011-07-29 03:08:49 +02:00
/* Returns all files in a directory in *list, and the number
* of files as return value . If list is NULL returns only the
2013-09-29 14:40:58 +02:00
* number . */
2011-07-22 21:01:15 +02:00
d = opendir ( path ) ;
2011-09-23 01:43:28 +02:00
if ( ! d )
return - errno ;
2011-07-22 21:01:15 +02:00
for ( ; ; ) {
2012-09-19 22:21:09 +02:00
struct dirent * de ;
2011-07-22 21:01:15 +02:00
2013-12-19 12:05:41 +01:00
errno = 0 ;
de = readdir ( d ) ;
if ( ! de & & errno ! = 0 )
return - errno ;
2011-07-22 21:01:15 +02:00
if ( ! de )
break ;
dirent_ensure_type ( d , de ) ;
if ( ! dirent_is_file ( de ) )
continue ;
2011-07-29 03:08:49 +02:00
if ( list ) {
2013-09-29 14:40:58 +02:00
/* one extra slot is needed for the terminating NULL */
if ( ! GREEDY_REALLOC ( l , bufsize , n + 2 ) )
return - ENOMEM ;
2011-07-22 21:01:15 +02:00
2013-09-29 14:40:58 +02:00
l [ n ] = strdup ( de - > d_name ) ;
if ( ! l [ n ] )
return - ENOMEM ;
2011-07-22 21:01:15 +02:00
2013-09-29 14:40:58 +02:00
l [ + + n ] = NULL ;
2011-07-29 03:08:49 +02:00
} else
2013-09-29 14:40:58 +02:00
n + + ;
2011-07-22 21:01:15 +02:00
}
2013-09-29 14:40:58 +02:00
if ( list ) {
* list = l ;
l = NULL ; /* avoid freeing */
}
2011-07-22 21:01:15 +02:00
2013-09-29 14:40:58 +02:00
return n ;
2011-07-22 21:01:15 +02:00
}
2012-07-13 13:41:01 +02:00
char * strjoin ( const char * x , . . . ) {
2011-08-01 01:28:01 +02:00
va_list ap ;
size_t l ;
char * r , * p ;
va_start ( ap , x ) ;
if ( x ) {
l = strlen ( x ) ;
for ( ; ; ) {
const char * t ;
2012-09-20 11:08:27 +02:00
size_t n ;
2011-08-01 01:28:01 +02:00
t = va_arg ( ap , const char * ) ;
if ( ! t )
break ;
2012-09-20 11:08:27 +02:00
n = strlen ( t ) ;
2012-09-21 10:22:46 +02:00
if ( n > ( ( size_t ) - 1 ) - l ) {
va_end ( ap ) ;
2012-09-20 11:08:27 +02:00
return NULL ;
2012-09-21 10:22:46 +02:00
}
2012-09-20 11:08:27 +02:00
l + = n ;
2011-08-01 01:28:01 +02:00
}
} else
l = 0 ;
va_end ( ap ) ;
r = new ( char , l + 1 ) ;
if ( ! r )
return NULL ;
if ( x ) {
p = stpcpy ( r , x ) ;
va_start ( ap , x ) ;
for ( ; ; ) {
const char * t ;
t = va_arg ( ap , const char * ) ;
if ( ! t )
break ;
p = stpcpy ( p , t ) ;
}
2011-09-23 01:43:28 +02:00
va_end ( ap ) ;
2011-08-01 01:28:01 +02:00
} else
r [ 0 ] = 0 ;
return r ;
}
2011-08-01 05:05:12 +02:00
bool is_main_thread ( void ) {
2013-12-16 01:24:14 +01:00
static thread_local int cached = 0 ;
2011-08-01 05:05:12 +02:00
if ( _unlikely_ ( cached = = 0 ) )
cached = getpid ( ) = = gettid ( ) ? 1 : - 1 ;
return cached > 0 ;
}
2011-08-21 00:28:30 +02:00
int block_get_whole_disk ( dev_t d , dev_t * ret ) {
char * p , * s ;
int r ;
unsigned n , m ;
assert ( ret ) ;
/* If it has a queue this is good enough for us */
if ( asprintf ( & p , " /sys/dev/block/%u:%u/queue " , major ( d ) , minor ( d ) ) < 0 )
return - ENOMEM ;
r = access ( p , F_OK ) ;
free ( p ) ;
if ( r > = 0 ) {
* ret = d ;
return 0 ;
}
/* If it is a partition find the originating device */
if ( asprintf ( & p , " /sys/dev/block/%u:%u/partition " , major ( d ) , minor ( d ) ) < 0 )
return - ENOMEM ;
r = access ( p , F_OK ) ;
free ( p ) ;
if ( r < 0 )
return - ENOENT ;
/* Get parent dev_t */
if ( asprintf ( & p , " /sys/dev/block/%u:%u/../dev " , major ( d ) , minor ( d ) ) < 0 )
return - ENOMEM ;
r = read_one_line_file ( p , & s ) ;
free ( p ) ;
if ( r < 0 )
return r ;
r = sscanf ( s , " %u:%u " , & m , & n ) ;
free ( s ) ;
if ( r ! = 2 )
return - EINVAL ;
/* Only return this if it is really good enough for us. */
if ( asprintf ( & p , " /sys/dev/block/%u:%u/queue " , m , n ) < 0 )
return - ENOMEM ;
r = access ( p , F_OK ) ;
free ( p ) ;
if ( r > = 0 ) {
* ret = makedev ( m , n ) ;
return 0 ;
}
return - ENOENT ;
}
2012-01-18 15:40:21 +01:00
int file_is_priv_sticky ( const char * p ) {
2011-08-21 20:05:51 +02:00
struct stat st ;
assert ( p ) ;
if ( lstat ( p , & st ) < 0 )
return - errno ;
return
2012-01-18 15:40:21 +01:00
( st . st_uid = = 0 | | st . st_uid = = getuid ( ) ) & &
2011-08-21 20:05:51 +02:00
( st . st_mode & S_ISVTX ) ;
}
2011-08-21 00:28:30 +02:00
2011-05-23 23:54:22 +02:00
static const char * const ioprio_class_table [ ] = {
[ IOPRIO_CLASS_NONE ] = " none " ,
[ IOPRIO_CLASS_RT ] = " realtime " ,
[ IOPRIO_CLASS_BE ] = " best-effort " ,
[ IOPRIO_CLASS_IDLE ] = " idle "
} ;
2012-10-30 14:29:38 +01:00
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK ( ioprio_class , int , INT_MAX ) ;
2011-05-23 23:54:22 +02:00
static const char * const sigchld_code_table [ ] = {
[ CLD_EXITED ] = " exited " ,
[ CLD_KILLED ] = " killed " ,
[ CLD_DUMPED ] = " dumped " ,
[ CLD_TRAPPED ] = " trapped " ,
[ CLD_STOPPED ] = " stopped " ,
[ CLD_CONTINUED ] = " continued " ,
} ;
DEFINE_STRING_TABLE_LOOKUP ( sigchld_code , int ) ;
static const char * const log_facility_unshifted_table [ LOG_NFACILITIES ] = {
[ LOG_FAC ( LOG_KERN ) ] = " kern " ,
[ LOG_FAC ( LOG_USER ) ] = " user " ,
[ LOG_FAC ( LOG_MAIL ) ] = " mail " ,
[ LOG_FAC ( LOG_DAEMON ) ] = " daemon " ,
[ LOG_FAC ( LOG_AUTH ) ] = " auth " ,
[ LOG_FAC ( LOG_SYSLOG ) ] = " syslog " ,
[ LOG_FAC ( LOG_LPR ) ] = " lpr " ,
[ LOG_FAC ( LOG_NEWS ) ] = " news " ,
[ LOG_FAC ( LOG_UUCP ) ] = " uucp " ,
[ LOG_FAC ( LOG_CRON ) ] = " cron " ,
[ LOG_FAC ( LOG_AUTHPRIV ) ] = " authpriv " ,
[ LOG_FAC ( LOG_FTP ) ] = " ftp " ,
[ LOG_FAC ( LOG_LOCAL0 ) ] = " local0 " ,
[ LOG_FAC ( LOG_LOCAL1 ) ] = " local1 " ,
[ LOG_FAC ( LOG_LOCAL2 ) ] = " local2 " ,
[ LOG_FAC ( LOG_LOCAL3 ) ] = " local3 " ,
[ LOG_FAC ( LOG_LOCAL4 ) ] = " local4 " ,
[ LOG_FAC ( LOG_LOCAL5 ) ] = " local5 " ,
[ LOG_FAC ( LOG_LOCAL6 ) ] = " local6 " ,
[ LOG_FAC ( LOG_LOCAL7 ) ] = " local7 "
} ;
2012-10-30 14:29:38 +01:00
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK ( log_facility_unshifted , int , LOG_FAC ( ~ 0 ) ) ;
2011-05-23 23:54:22 +02:00
static const char * const log_level_table [ ] = {
[ LOG_EMERG ] = " emerg " ,
[ LOG_ALERT ] = " alert " ,
[ LOG_CRIT ] = " crit " ,
[ LOG_ERR ] = " err " ,
[ LOG_WARNING ] = " warning " ,
[ LOG_NOTICE ] = " notice " ,
[ LOG_INFO ] = " info " ,
[ LOG_DEBUG ] = " debug "
} ;
2012-10-30 14:29:38 +01:00
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK ( log_level , int , LOG_DEBUG ) ;
2011-05-23 23:54:22 +02:00
static const char * const sched_policy_table [ ] = {
[ SCHED_OTHER ] = " other " ,
[ SCHED_BATCH ] = " batch " ,
[ SCHED_IDLE ] = " idle " ,
[ SCHED_FIFO ] = " fifo " ,
[ SCHED_RR ] = " rr "
} ;
2012-10-30 14:29:38 +01:00
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK ( sched_policy , int , INT_MAX ) ;
2011-05-23 23:54:22 +02:00
2014-03-05 02:29:58 +01:00
static const char * const rlimit_table [ _RLIMIT_MAX ] = {
2011-05-23 23:54:22 +02:00
[ RLIMIT_CPU ] = " LimitCPU " ,
[ RLIMIT_FSIZE ] = " LimitFSIZE " ,
[ RLIMIT_DATA ] = " LimitDATA " ,
[ RLIMIT_STACK ] = " LimitSTACK " ,
[ RLIMIT_CORE ] = " LimitCORE " ,
[ RLIMIT_RSS ] = " LimitRSS " ,
[ RLIMIT_NOFILE ] = " LimitNOFILE " ,
[ RLIMIT_AS ] = " LimitAS " ,
[ RLIMIT_NPROC ] = " LimitNPROC " ,
[ RLIMIT_MEMLOCK ] = " LimitMEMLOCK " ,
[ RLIMIT_LOCKS ] = " LimitLOCKS " ,
[ RLIMIT_SIGPENDING ] = " LimitSIGPENDING " ,
[ RLIMIT_MSGQUEUE ] = " LimitMSGQUEUE " ,
[ RLIMIT_NICE ] = " LimitNICE " ,
[ RLIMIT_RTPRIO ] = " LimitRTPRIO " ,
[ RLIMIT_RTTIME ] = " LimitRTTIME "
} ;
DEFINE_STRING_TABLE_LOOKUP ( rlimit , int ) ;
static const char * const ip_tos_table [ ] = {
[ IPTOS_LOWDELAY ] = " low-delay " ,
[ IPTOS_THROUGHPUT ] = " throughput " ,
[ IPTOS_RELIABILITY ] = " reliability " ,
[ IPTOS_LOWCOST ] = " low-cost " ,
} ;
2012-10-30 14:29:38 +01:00
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK ( ip_tos , int , 0xff ) ;
2011-05-23 23:54:22 +02:00
2011-09-23 09:39:40 +02:00
static const char * const __signal_table [ ] = {
2011-05-23 23:54:22 +02:00
[ SIGHUP ] = " HUP " ,
[ SIGINT ] = " INT " ,
[ SIGQUIT ] = " QUIT " ,
[ SIGILL ] = " ILL " ,
[ SIGTRAP ] = " TRAP " ,
[ SIGABRT ] = " ABRT " ,
[ SIGBUS ] = " BUS " ,
[ SIGFPE ] = " FPE " ,
[ SIGKILL ] = " KILL " ,
[ SIGUSR1 ] = " USR1 " ,
[ SIGSEGV ] = " SEGV " ,
[ SIGUSR2 ] = " USR2 " ,
[ SIGPIPE ] = " PIPE " ,
[ SIGALRM ] = " ALRM " ,
[ SIGTERM ] = " TERM " ,
# ifdef SIGSTKFLT
[ SIGSTKFLT ] = " STKFLT " , /* Linux on SPARC doesn't know SIGSTKFLT */
# endif
[ SIGCHLD ] = " CHLD " ,
[ SIGCONT ] = " CONT " ,
[ SIGSTOP ] = " STOP " ,
[ SIGTSTP ] = " TSTP " ,
[ SIGTTIN ] = " TTIN " ,
[ SIGTTOU ] = " TTOU " ,
[ SIGURG ] = " URG " ,
[ SIGXCPU ] = " XCPU " ,
[ SIGXFSZ ] = " XFSZ " ,
[ SIGVTALRM ] = " VTALRM " ,
[ SIGPROF ] = " PROF " ,
[ SIGWINCH ] = " WINCH " ,
[ SIGIO ] = " IO " ,
[ SIGPWR ] = " PWR " ,
[ SIGSYS ] = " SYS "
} ;
2011-09-23 09:39:40 +02:00
DEFINE_PRIVATE_STRING_TABLE_LOOKUP ( __signal , int ) ;
const char * signal_to_string ( int signo ) {
2013-12-16 01:24:14 +01:00
static thread_local char buf [ sizeof ( " RTMIN+ " ) - 1 + DECIMAL_STR_MAX ( int ) + 1 ] ;
2011-09-23 09:39:40 +02:00
const char * name ;
name = __signal_to_string ( signo ) ;
if ( name )
return name ;
if ( signo > = SIGRTMIN & & signo < = SIGRTMAX )
2013-04-02 17:33:19 +02:00
snprintf ( buf , sizeof ( buf ) , " RTMIN+%d " , signo - SIGRTMIN ) ;
2011-09-23 09:39:40 +02:00
else
2013-04-02 17:33:19 +02:00
snprintf ( buf , sizeof ( buf ) , " %d " , signo ) ;
2011-09-23 09:39:40 +02:00
return buf ;
}
int signal_from_string ( const char * s ) {
int signo ;
int offset = 0 ;
unsigned u ;
2012-09-20 11:08:27 +02:00
signo = __signal_from_string ( s ) ;
2011-09-23 09:39:40 +02:00
if ( signo > 0 )
return signo ;
if ( startswith ( s , " RTMIN+ " ) ) {
s + = 6 ;
offset = SIGRTMIN ;
}
if ( safe_atou ( s , & u ) > = 0 ) {
signo = ( int ) u + offset ;
if ( signo > 0 & & signo < _NSIG )
return signo ;
}
return - 1 ;
}
2011-08-22 14:58:50 +02:00
bool kexec_loaded ( void ) {
bool loaded = false ;
char * s ;
if ( read_one_line_file ( " /sys/kernel/kexec_loaded " , & s ) > = 0 ) {
if ( s [ 0 ] = = ' 1 ' )
loaded = true ;
free ( s ) ;
}
return loaded ;
}
2011-09-28 04:25:13 +02:00
int strdup_or_null ( const char * a , char * * b ) {
char * c ;
assert ( b ) ;
if ( ! a ) {
* b = NULL ;
return 0 ;
}
c = strdup ( a ) ;
if ( ! c )
return - ENOMEM ;
* b = c ;
return 0 ;
}
2011-10-11 22:30:31 +02:00
2011-10-07 21:06:39 +02:00
int prot_from_flags ( int flags ) {
switch ( flags & O_ACCMODE ) {
case O_RDONLY :
return PROT_READ ;
case O_WRONLY :
return PROT_WRITE ;
case O_RDWR :
return PROT_READ | PROT_WRITE ;
default :
return - EINVAL ;
}
2011-10-12 04:42:38 +02:00
}
2011-10-12 04:29:11 +02:00
2011-12-31 02:31:54 +01:00
char * format_bytes ( char * buf , size_t l , off_t t ) {
2011-12-31 03:35:45 +01:00
unsigned i ;
2011-12-31 02:31:54 +01:00
static const struct {
const char * suffix ;
off_t factor ;
} table [ ] = {
2012-01-14 03:07:29 +01:00
{ " E " , 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL } ,
{ " P " , 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL } ,
2011-12-31 02:31:54 +01:00
{ " T " , 1024ULL * 1024ULL * 1024ULL * 1024ULL } ,
{ " G " , 1024ULL * 1024ULL * 1024ULL } ,
{ " M " , 1024ULL * 1024ULL } ,
{ " K " , 1024ULL } ,
} ;
for ( i = 0 ; i < ELEMENTSOF ( table ) ; i + + ) {
if ( t > = table [ i ] . factor ) {
snprintf ( buf , l ,
" %llu.%llu%s " ,
( unsigned long long ) ( t / table [ i ] . factor ) ,
( unsigned long long ) ( ( ( t * 10ULL ) / table [ i ] . factor ) % 10ULL ) ,
table [ i ] . suffix ) ;
goto finish ;
}
}
snprintf ( buf , l , " %lluB " , ( unsigned long long ) t ) ;
finish :
buf [ l - 1 ] = 0 ;
return buf ;
}
2012-01-05 20:11:47 +01:00
void * memdup ( const void * p , size_t l ) {
void * r ;
assert ( p ) ;
r = malloc ( l ) ;
if ( ! r )
return NULL ;
memcpy ( r , p , l ) ;
return r ;
}
2012-01-27 18:57:37 +01:00
int fd_inc_sndbuf ( int fd , size_t n ) {
int r , value ;
socklen_t l = sizeof ( value ) ;
r = getsockopt ( fd , SOL_SOCKET , SO_SNDBUF , & value , & l ) ;
2013-12-16 17:04:36 +01:00
if ( r > = 0 & & l = = sizeof ( value ) & & ( size_t ) value > = n * 2 )
2012-01-27 18:57:37 +01:00
return 0 ;
2013-12-16 17:04:36 +01:00
/* If we have the privileges we will ignore the kernel limit. */
2012-01-27 18:57:37 +01:00
value = ( int ) n ;
2013-12-16 17:04:36 +01:00
if ( setsockopt ( fd , SOL_SOCKET , SO_SNDBUFFORCE , & value , sizeof ( value ) ) < 0 )
if ( setsockopt ( fd , SOL_SOCKET , SO_SNDBUF , & value , sizeof ( value ) ) < 0 )
return - errno ;
2012-01-27 18:57:37 +01:00
return 1 ;
}
int fd_inc_rcvbuf ( int fd , size_t n ) {
int r , value ;
socklen_t l = sizeof ( value ) ;
r = getsockopt ( fd , SOL_SOCKET , SO_RCVBUF , & value , & l ) ;
2013-12-16 17:04:36 +01:00
if ( r > = 0 & & l = = sizeof ( value ) & & ( size_t ) value > = n * 2 )
2012-01-27 18:57:37 +01:00
return 0 ;
2013-12-16 17:04:36 +01:00
/* If we have the privileges we will ignore the kernel limit. */
2012-01-27 18:57:37 +01:00
2013-12-16 17:04:36 +01:00
value = ( int ) n ;
if ( setsockopt ( fd , SOL_SOCKET , SO_RCVBUFFORCE , & value , sizeof ( value ) ) < 0 )
if ( setsockopt ( fd , SOL_SOCKET , SO_RCVBUF , & value , sizeof ( value ) ) < 0 )
return - errno ;
2012-01-27 18:57:37 +01:00
return 1 ;
}
2012-04-11 18:50:16 +02:00
2012-04-11 22:37:13 +02:00
int fork_agent ( pid_t * pid , const int except [ ] , unsigned n_except , const char * path , . . . ) {
2012-04-11 18:50:16 +02:00
pid_t parent_pid , agent_pid ;
int fd ;
bool stdout_is_tty , stderr_is_tty ;
unsigned n , i ;
va_list ap ;
char * * l ;
assert ( pid ) ;
assert ( path ) ;
parent_pid = getpid ( ) ;
/* Spawns a temporary TTY agent, making sure it goes away when
* we go away */
agent_pid = fork ( ) ;
if ( agent_pid < 0 )
return - errno ;
if ( agent_pid ! = 0 ) {
* pid = agent_pid ;
return 0 ;
}
/* In the child:
*
* Make sure the agent goes away when the parent dies */
if ( prctl ( PR_SET_PDEATHSIG , SIGTERM ) < 0 )
_exit ( EXIT_FAILURE ) ;
/* Check whether our parent died before we were able
* to set the death signal */
if ( getppid ( ) ! = parent_pid )
_exit ( EXIT_SUCCESS ) ;
/* Don't leak fds to the agent */
2012-04-11 22:37:13 +02:00
close_all_fds ( except , n_except ) ;
2012-04-11 18:50:16 +02:00
stdout_is_tty = isatty ( STDOUT_FILENO ) ;
stderr_is_tty = isatty ( STDERR_FILENO ) ;
if ( ! stdout_is_tty | | ! stderr_is_tty ) {
/* Detach from stdout/stderr. and reopen
* / dev / tty for them . This is important to
* ensure that when systemctl is started via
* popen ( ) or a similar call that expects to
* read EOF we actually do generate EOF and
* not delay this indefinitely by because we
* keep an unused copy of stdin around . */
fd = open ( " /dev/tty " , O_WRONLY ) ;
if ( fd < 0 ) {
log_error ( " Failed to open /dev/tty: %m " ) ;
_exit ( EXIT_FAILURE ) ;
}
if ( ! stdout_is_tty )
dup2 ( fd , STDOUT_FILENO ) ;
if ( ! stderr_is_tty )
dup2 ( fd , STDERR_FILENO ) ;
if ( fd > 2 )
close ( fd ) ;
}
/* Count arguments */
va_start ( ap , path ) ;
for ( n = 0 ; va_arg ( ap , char * ) ; n + + )
;
va_end ( ap ) ;
/* Allocate strv */
l = alloca ( sizeof ( char * ) * ( n + 1 ) ) ;
/* Fill in arguments */
va_start ( ap , path ) ;
for ( i = 0 ; i < = n ; i + + )
l [ i ] = va_arg ( ap , char * ) ;
va_end ( ap ) ;
execv ( path , l ) ;
_exit ( EXIT_FAILURE ) ;
}
2012-04-12 03:38:52 +02:00
int setrlimit_closest ( int resource , const struct rlimit * rlim ) {
struct rlimit highest , fixed ;
assert ( rlim ) ;
if ( setrlimit ( resource , rlim ) > = 0 )
return 0 ;
if ( errno ! = EPERM )
return - errno ;
/* So we failed to set the desired setrlimit, then let's try
* to get as close as we can */
assert_se ( getrlimit ( resource , & highest ) = = 0 ) ;
fixed . rlim_cur = MIN ( rlim - > rlim_cur , highest . rlim_max ) ;
fixed . rlim_max = MIN ( rlim - > rlim_max , highest . rlim_max ) ;
if ( setrlimit ( resource , & fixed ) < 0 )
return - errno ;
return 0 ;
}
2012-04-22 01:59:11 +02:00
2012-04-22 14:48:46 +02:00
int getenv_for_pid ( pid_t pid , const char * field , char * * _value ) {
2013-04-16 14:50:05 +02:00
_cleanup_fclose_ FILE * f = NULL ;
char * value = NULL ;
2012-04-22 14:48:46 +02:00
int r ;
bool done = false ;
size_t l ;
2013-04-16 14:50:05 +02:00
const char * path ;
2012-04-22 14:48:46 +02:00
2013-04-16 14:50:05 +02:00
assert ( pid > = 0 ) ;
2012-04-22 14:48:46 +02:00
assert ( field ) ;
assert ( _value ) ;
2014-01-04 02:35:23 +01:00
path = procfs_file_alloca ( pid , " environ " ) ;
2012-04-22 14:48:46 +02:00
f = fopen ( path , " re " ) ;
if ( ! f )
return - errno ;
l = strlen ( field ) ;
r = 0 ;
do {
char line [ LINE_MAX ] ;
unsigned i ;
for ( i = 0 ; i < sizeof ( line ) - 1 ; i + + ) {
int c ;
c = getc ( f ) ;
if ( _unlikely_ ( c = = EOF ) ) {
done = true ;
break ;
} else if ( c = = 0 )
break ;
line [ i ] = c ;
}
line [ i ] = 0 ;
if ( memcmp ( line , field , l ) = = 0 & & line [ l ] = = ' = ' ) {
value = strdup ( line + l + 1 ) ;
2013-04-16 14:50:05 +02:00
if ( ! value )
return - ENOMEM ;
2012-04-22 14:48:46 +02:00
r = 1 ;
break ;
}
} while ( ! done ) ;
2013-04-16 14:50:05 +02:00
* _value = value ;
2012-04-22 14:48:46 +02:00
return r ;
}
2012-05-08 19:02:25 +02:00
2012-05-21 15:12:18 +02:00
bool is_valid_documentation_url ( const char * url ) {
assert ( url ) ;
if ( startswith ( url , " http:// " ) & & url [ 7 ] )
return true ;
if ( startswith ( url , " https:// " ) & & url [ 8 ] )
return true ;
if ( startswith ( url , " file: " ) & & url [ 5 ] )
return true ;
if ( startswith ( url , " info: " ) & & url [ 5 ] )
return true ;
if ( startswith ( url , " man: " ) & & url [ 4 ] )
return true ;
return false ;
}
2012-05-16 14:22:40 +02:00
bool in_initrd ( void ) {
2013-12-16 01:56:21 +01:00
static int saved = - 1 ;
2012-07-10 18:46:26 +02:00
struct statfs s ;
2012-05-21 20:00:58 +02:00
2012-07-10 18:46:26 +02:00
if ( saved > = 0 )
return saved ;
/* We make two checks here:
*
* 1. the flag file / etc / initrd - release must exist
* 2. the root file system must be a memory file system
*
* The second check is extra paranoia , since misdetecting an
* initrd can have bad bad consequences due the initrd
* emptying when transititioning to the main systemd .
*/
saved = access ( " /etc/initrd-release " , F_OK ) > = 0 & &
statfs ( " / " , & s ) > = 0 & &
2012-11-16 17:17:21 +01:00
is_temporary_fs ( & s ) ;
2012-05-16 14:22:40 +02:00
2012-05-21 20:00:58 +02:00
return saved ;
2012-05-16 14:22:40 +02:00
}
2012-05-30 15:01:51 +02:00
void warn_melody ( void ) {
2012-09-14 10:06:42 +02:00
_cleanup_close_ int fd = - 1 ;
2012-05-30 15:01:51 +02:00
fd = open ( " /dev/console " , O_WRONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( fd < 0 )
return ;
2012-09-20 11:08:27 +02:00
/* Yeah, this is synchronous. Kinda sucks. But well... */
2012-05-30 15:01:51 +02:00
ioctl ( fd , KIOCSOUND , ( int ) ( 1193180 / 440 ) ) ;
usleep ( 125 * USEC_PER_MSEC ) ;
ioctl ( fd , KIOCSOUND , ( int ) ( 1193180 / 220 ) ) ;
usleep ( 125 * USEC_PER_MSEC ) ;
ioctl ( fd , KIOCSOUND , ( int ) ( 1193180 / 220 ) ) ;
usleep ( 125 * USEC_PER_MSEC ) ;
ioctl ( fd , KIOCSOUND , 0 ) ;
}
2012-07-10 19:19:59 +02:00
int make_console_stdio ( void ) {
int fd , r ;
/* Make /dev/console the controlling terminal and stdin/stdout/stderr */
fd = acquire_terminal ( " /dev/console " , false , true , true , ( usec_t ) - 1 ) ;
if ( fd < 0 ) {
log_error ( " Failed to acquire terminal: %s " , strerror ( - fd ) ) ;
return fd ;
}
r = make_stdio ( fd ) ;
if ( r < 0 ) {
log_error ( " Failed to duplicate terminal fd: %s " , strerror ( - r ) ) ;
return r ;
}
return 0 ;
}
2012-07-16 12:15:22 +02:00
int get_home_dir ( char * * _h ) {
2013-12-16 04:59:31 +01:00
struct passwd * p ;
2012-07-16 12:15:22 +02:00
const char * e ;
2013-12-16 04:59:31 +01:00
char * h ;
2012-07-16 12:15:22 +02:00
uid_t u ;
assert ( _h ) ;
/* Take the user specified one */
e = getenv ( " HOME " ) ;
if ( e ) {
h = strdup ( e ) ;
if ( ! h )
return - ENOMEM ;
* _h = h ;
return 0 ;
}
/* Hardcode home directory for root to avoid NSS */
u = getuid ( ) ;
if ( u = = 0 ) {
h = strdup ( " /root " ) ;
if ( ! h )
return - ENOMEM ;
* _h = h ;
return 0 ;
}
/* Check the database... */
errno = 0 ;
p = getpwuid ( u ) ;
if ( ! p )
2013-04-12 00:57:42 +02:00
return errno > 0 ? - errno : - ESRCH ;
2012-07-16 12:15:22 +02:00
if ( ! path_is_absolute ( p - > pw_dir ) )
return - EINVAL ;
h = strdup ( p - > pw_dir ) ;
if ( ! h )
return - ENOMEM ;
* _h = h ;
return 0 ;
}
2013-12-16 04:59:31 +01:00
int get_shell ( char * * _s ) {
struct passwd * p ;
const char * e ;
char * s ;
uid_t u ;
assert ( _s ) ;
/* Take the user specified one */
e = getenv ( " SHELL " ) ;
if ( e ) {
s = strdup ( e ) ;
if ( ! s )
return - ENOMEM ;
* _s = s ;
return 0 ;
}
/* Hardcode home directory for root to avoid NSS */
u = getuid ( ) ;
if ( u = = 0 ) {
s = strdup ( " /bin/sh " ) ;
if ( ! s )
return - ENOMEM ;
* _s = s ;
return 0 ;
}
/* Check the database... */
errno = 0 ;
p = getpwuid ( u ) ;
if ( ! p )
return errno > 0 ? - errno : - ESRCH ;
if ( ! path_is_absolute ( p - > pw_shell ) )
return - EINVAL ;
s = strdup ( p - > pw_shell ) ;
if ( ! s )
return - ENOMEM ;
* _s = s ;
return 0 ;
}
2012-10-03 19:29:20 +02:00
bool filename_is_safe ( const char * p ) {
if ( isempty ( p ) )
return false ;
if ( strchr ( p , ' / ' ) )
return false ;
if ( streq ( p , " . " ) )
return false ;
if ( streq ( p , " .. " ) )
return false ;
if ( strlen ( p ) > FILENAME_MAX )
return false ;
return true ;
}
bool string_is_safe ( const char * p ) {
const char * t ;
assert ( p ) ;
for ( t = p ; * t ; t + + ) {
2012-10-17 21:24:14 +02:00
if ( * t > 0 & & * t < ' ' )
2012-10-03 19:29:20 +02:00
return false ;
2012-10-04 14:27:29 +02:00
if ( strchr ( " \\ \" \' " , * t ) )
2012-10-03 19:29:20 +02:00
return false ;
}
return true ;
}
2012-10-11 16:42:46 +02:00
2013-09-12 03:50:16 +02:00
/**
* Check if a string contains control characters .
* Spaces and tabs are not considered control characters .
*/
2013-02-11 03:46:08 +01:00
bool string_has_cc ( const char * p ) {
const char * t ;
assert ( p ) ;
for ( t = p ; * t ; t + + )
2013-09-12 16:03:16 +02:00
if ( * t > 0 & & * t < ' ' & & * t ! = ' \t ' )
2013-02-11 03:46:08 +01:00
return true ;
return false ;
}
2013-01-19 00:59:19 +01:00
bool path_is_safe ( const char * p ) {
if ( isempty ( p ) )
return false ;
if ( streq ( p , " .. " ) | | startswith ( p , " ../ " ) | | endswith ( p , " /.. " ) | | strstr ( p , " /../ " ) )
return false ;
if ( strlen ( p ) > PATH_MAX )
return false ;
/* The following two checks are not really dangerous, but hey, they still are confusing */
if ( streq ( p , " . " ) | | startswith ( p , " ./ " ) | | endswith ( p , " /. " ) | | strstr ( p , " /./ " ) )
return false ;
if ( strstr ( p , " // " ) )
return false ;
return true ;
}
2012-10-22 14:31:46 +02:00
/* hey glibc, APIs with callbacks without a user pointer are so useless */
void * xbsearch_r ( const void * key , const void * base , size_t nmemb , size_t size ,
2012-10-25 21:40:01 +02:00
int ( * compar ) ( const void * , const void * , void * ) , void * arg ) {
2012-10-22 14:31:46 +02:00
size_t l , u , idx ;
const void * p ;
int comparison ;
l = 0 ;
u = nmemb ;
while ( l < u ) {
idx = ( l + u ) / 2 ;
p = ( void * ) ( ( ( const char * ) base ) + ( idx * size ) ) ;
comparison = compar ( key , p , arg ) ;
if ( comparison < 0 )
u = idx ;
else if ( comparison > 0 )
l = idx + 1 ;
else
return ( void * ) p ;
}
return NULL ;
}
2012-11-02 17:27:15 +01:00
bool is_locale_utf8 ( void ) {
const char * set ;
static int cached_answer = - 1 ;
if ( cached_answer > = 0 )
goto out ;
if ( ! setlocale ( LC_ALL , " " ) ) {
cached_answer = true ;
goto out ;
}
set = nl_langinfo ( CODESET ) ;
if ( ! set ) {
cached_answer = true ;
goto out ;
}
2013-12-03 22:27:45 +01:00
if ( streq ( set , " UTF-8 " ) ) {
2013-04-15 18:34:53 +02:00
cached_answer = true ;
goto out ;
}
2013-06-27 11:26:36 +02:00
/* For LC_CTYPE=="C" return true, because CTYPE is effectly
* unset and everything can do to UTF - 8 nowadays . */
2013-04-15 18:34:53 +02:00
set = setlocale ( LC_CTYPE , NULL ) ;
if ( ! set ) {
cached_answer = true ;
goto out ;
}
2013-06-27 11:26:36 +02:00
/* Check result, but ignore the result if C was set
* explicitly . */
cached_answer =
streq ( set , " C " ) & &
! getenv ( " LC_ALL " ) & &
! getenv ( " LC_CTYPE " ) & &
! getenv ( " LANG " ) ;
2013-04-15 18:34:53 +02:00
2012-11-02 17:27:15 +01:00
out :
2013-06-27 11:26:36 +02:00
return ( bool ) cached_answer ;
2012-11-02 17:27:15 +01:00
}
2012-11-02 17:35:30 +01:00
const char * draw_special_char ( DrawSpecialChar ch ) {
static const char * draw_table [ 2 ] [ _DRAW_SPECIAL_CHAR_MAX ] = {
/* UTF-8 */ {
2012-11-12 22:27:48 +01:00
[ DRAW_TREE_VERT ] = " \342 \224 \202 " , /* │ */
[ DRAW_TREE_BRANCH ] = " \342 \224 \234 \342 \224 \200 " , /* ├─ */
[ DRAW_TREE_RIGHT ] = " \342 \224 \224 \342 \224 \200 " , /* └─ */
2013-01-17 21:34:11 +01:00
[ DRAW_TREE_SPACE ] = " " , /* */
2012-11-12 22:27:48 +01:00
[ DRAW_TRIANGULAR_BULLET ] = " \342 \200 \243 " , /* ‣ */
2013-11-07 16:42:54 +01:00
[ DRAW_BLACK_CIRCLE ] = " \342 \227 \217 " , /* ● */
2012-11-02 17:35:30 +01:00
} ,
/* ASCII fallback */ {
2012-11-12 22:27:48 +01:00
[ DRAW_TREE_VERT ] = " | " ,
[ DRAW_TREE_BRANCH ] = " |- " ,
[ DRAW_TREE_RIGHT ] = " `- " ,
2013-01-17 21:34:11 +01:00
[ DRAW_TREE_SPACE ] = " " ,
2012-11-12 22:27:48 +01:00
[ DRAW_TRIANGULAR_BULLET ] = " > " ,
2013-11-07 16:42:54 +01:00
[ DRAW_BLACK_CIRCLE ] = " * " ,
2012-11-02 17:35:30 +01:00
}
} ;
return draw_table [ ! is_locale_utf8 ( ) ] [ ch ] ;
}
2012-11-14 22:16:23 +01:00
char * strreplace ( const char * text , const char * old_string , const char * new_string ) {
const char * f ;
char * t , * r ;
size_t l , old_len , new_len ;
assert ( text ) ;
assert ( old_string ) ;
assert ( new_string ) ;
old_len = strlen ( old_string ) ;
new_len = strlen ( new_string ) ;
l = strlen ( text ) ;
r = new ( char , l + 1 ) ;
if ( ! r )
return NULL ;
f = text ;
t = r ;
while ( * f ) {
char * a ;
size_t d , nl ;
if ( ! startswith ( f , old_string ) ) {
* ( t + + ) = * ( f + + ) ;
continue ;
}
d = t - r ;
nl = l - old_len + new_len ;
a = realloc ( r , nl + 1 ) ;
if ( ! a )
goto oom ;
l = nl ;
r = a ;
t = r + d ;
t = stpcpy ( t , new_string ) ;
f + = old_len ;
}
* t = 0 ;
return r ;
oom :
free ( r ) ;
return NULL ;
}
2012-12-23 11:23:59 +01:00
char * strip_tab_ansi ( char * * ibuf , size_t * _isz ) {
2013-01-04 21:51:47 +01:00
const char * i , * begin = NULL ;
2012-12-23 11:23:59 +01:00
enum {
STATE_OTHER ,
STATE_ESCAPE ,
STATE_BRACKET
} state = STATE_OTHER ;
char * obuf = NULL ;
size_t osz = 0 , isz ;
FILE * f ;
assert ( ibuf ) ;
assert ( * ibuf ) ;
/* Strips ANSI color and replaces TABs by 8 spaces */
isz = _isz ? * _isz : strlen ( * ibuf ) ;
f = open_memstream ( & obuf , & osz ) ;
if ( ! f )
return NULL ;
for ( i = * ibuf ; i < * ibuf + isz + 1 ; i + + ) {
switch ( state ) {
case STATE_OTHER :
if ( i > = * ibuf + isz ) /* EOT */
break ;
else if ( * i = = ' \x1B ' )
state = STATE_ESCAPE ;
else if ( * i = = ' \t ' )
fputs ( " " , f ) ;
else
fputc ( * i , f ) ;
break ;
case STATE_ESCAPE :
if ( i > = * ibuf + isz ) { /* EOT */
fputc ( ' \x1B ' , f ) ;
break ;
} else if ( * i = = ' [ ' ) {
state = STATE_BRACKET ;
begin = i + 1 ;
} else {
fputc ( ' \x1B ' , f ) ;
fputc ( * i , f ) ;
state = STATE_OTHER ;
}
break ;
case STATE_BRACKET :
if ( i > = * ibuf + isz | | /* EOT */
( ! ( * i > = ' 0 ' & & * i < = ' 9 ' ) & & * i ! = ' ; ' & & * i ! = ' m ' ) ) {
fputc ( ' \x1B ' , f ) ;
fputc ( ' [ ' , f ) ;
state = STATE_OTHER ;
i = begin - 1 ;
} else if ( * i = = ' m ' )
state = STATE_OTHER ;
break ;
}
}
if ( ferror ( f ) ) {
fclose ( f ) ;
free ( obuf ) ;
return NULL ;
}
fclose ( f ) ;
free ( * ibuf ) ;
* ibuf = obuf ;
if ( _isz )
* _isz = osz ;
return obuf ;
}
2012-12-25 16:29:51 +01:00
int on_ac_power ( void ) {
bool found_offline = false , found_online = false ;
_cleanup_closedir_ DIR * d = NULL ;
d = opendir ( " /sys/class/power_supply " ) ;
if ( ! d )
return - errno ;
for ( ; ; ) {
struct dirent * de ;
_cleanup_close_ int fd = - 1 , device = - 1 ;
char contents [ 6 ] ;
ssize_t n ;
2013-12-19 12:05:41 +01:00
errno = 0 ;
de = readdir ( d ) ;
if ( ! de & & errno ! = 0 )
return - errno ;
2012-12-25 16:29:51 +01:00
if ( ! de )
break ;
if ( ignore_file ( de - > d_name ) )
continue ;
device = openat ( dirfd ( d ) , de - > d_name , O_DIRECTORY | O_RDONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( device < 0 ) {
if ( errno = = ENOENT | | errno = = ENOTDIR )
continue ;
return - errno ;
}
fd = openat ( device , " type " , O_RDONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( fd < 0 ) {
if ( errno = = ENOENT )
continue ;
return - errno ;
}
n = read ( fd , contents , sizeof ( contents ) ) ;
if ( n < 0 )
return - errno ;
if ( n ! = 6 | | memcmp ( contents , " Mains \n " , 6 ) )
continue ;
close_nointr_nofail ( fd ) ;
fd = openat ( device , " online " , O_RDONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( fd < 0 ) {
if ( errno = = ENOENT )
continue ;
return - errno ;
}
n = read ( fd , contents , sizeof ( contents ) ) ;
if ( n < 0 )
return - errno ;
if ( n ! = 2 | | contents [ 1 ] ! = ' \n ' )
return - EIO ;
if ( contents [ 0 ] = = ' 1 ' ) {
found_online = true ;
break ;
} else if ( contents [ 0 ] = = ' 0 ' )
found_offline = true ;
else
return - EIO ;
}
return found_online | | ! found_offline ;
}
2013-02-11 23:48:36 +01:00
static int search_and_fopen_internal ( const char * path , const char * mode , char * * search , FILE * * _f ) {
char * * i ;
assert ( path ) ;
assert ( mode ) ;
assert ( _f ) ;
2014-02-01 00:35:04 +01:00
if ( ! path_strv_canonicalize_absolute_uniq ( search , NULL ) )
2013-02-11 23:48:36 +01:00
return - ENOMEM ;
STRV_FOREACH ( i , search ) {
_cleanup_free_ char * p = NULL ;
FILE * f ;
p = strjoin ( * i , " / " , path , NULL ) ;
if ( ! p )
return - ENOMEM ;
f = fopen ( p , mode ) ;
if ( f ) {
* _f = f ;
return 0 ;
}
if ( errno ! = ENOENT )
return - errno ;
}
return - ENOENT ;
}
int search_and_fopen ( const char * path , const char * mode , const char * * search , FILE * * _f ) {
_cleanup_strv_free_ char * * copy = NULL ;
assert ( path ) ;
assert ( mode ) ;
assert ( _f ) ;
if ( path_is_absolute ( path ) ) {
FILE * f ;
f = fopen ( path , mode ) ;
if ( f ) {
* _f = f ;
return 0 ;
}
return - errno ;
}
copy = strv_copy ( ( char * * ) search ) ;
if ( ! copy )
return - ENOMEM ;
return search_and_fopen_internal ( path , mode , copy , _f ) ;
}
int search_and_fopen_nulstr ( const char * path , const char * mode , const char * search , FILE * * _f ) {
_cleanup_strv_free_ char * * s = NULL ;
if ( path_is_absolute ( path ) ) {
FILE * f ;
f = fopen ( path , mode ) ;
if ( f ) {
* _f = f ;
return 0 ;
}
return - errno ;
}
s = strv_split_nulstr ( search ) ;
if ( ! s )
return - ENOMEM ;
return search_and_fopen_internal ( path , mode , s , _f ) ;
}
2013-03-14 18:12:27 +01:00
2013-03-19 20:01:18 +01:00
char * strextend ( char * * x , . . . ) {
va_list ap ;
size_t f , l ;
char * r , * p ;
assert ( x ) ;
l = f = * x ? strlen ( * x ) : 0 ;
va_start ( ap , x ) ;
for ( ; ; ) {
const char * t ;
size_t n ;
t = va_arg ( ap , const char * ) ;
if ( ! t )
break ;
n = strlen ( t ) ;
if ( n > ( ( size_t ) - 1 ) - l ) {
va_end ( ap ) ;
return NULL ;
}
l + = n ;
}
va_end ( ap ) ;
r = realloc ( * x , l + 1 ) ;
if ( ! r )
return NULL ;
p = r + f ;
va_start ( ap , x ) ;
for ( ; ; ) {
const char * t ;
t = va_arg ( ap , const char * ) ;
if ( ! t )
break ;
p = stpcpy ( p , t ) ;
}
va_end ( ap ) ;
* p = 0 ;
* x = r ;
return r + l ;
}
2013-03-20 03:15:03 +01:00
char * strrep ( const char * s , unsigned n ) {
size_t l ;
char * r , * p ;
unsigned i ;
assert ( s ) ;
l = strlen ( s ) ;
p = r = malloc ( l * n + 1 ) ;
if ( ! r )
return NULL ;
for ( i = 0 ; i < n ; i + + )
p = stpcpy ( p , s ) ;
* p = 0 ;
return r ;
}
2013-03-31 16:16:37 +02:00
void * greedy_realloc ( void * * p , size_t * allocated , size_t need ) {
size_t a ;
void * q ;
2013-12-10 19:53:03 +01:00
assert ( p ) ;
2013-12-01 01:09:26 +01:00
assert ( allocated ) ;
2013-03-31 16:16:37 +02:00
if ( * allocated > = need )
return * p ;
2013-04-01 08:08:05 +02:00
a = MAX ( 64u , need * 2 ) ;
2013-12-10 19:53:03 +01:00
/* check for overflows */
if ( a < need )
return NULL ;
2013-03-31 16:16:37 +02:00
q = realloc ( * p , a ) ;
if ( ! q )
return NULL ;
* p = q ;
* allocated = a ;
return q ;
}
2013-04-29 23:39:12 +02:00
2013-11-30 11:31:59 +01:00
void * greedy_realloc0 ( void * * p , size_t * allocated , size_t need ) {
2013-12-10 19:53:03 +01:00
size_t prev ;
2013-11-30 11:31:59 +01:00
uint8_t * q ;
2013-12-10 19:53:03 +01:00
assert ( p ) ;
assert ( allocated ) ;
prev = * allocated ;
2013-11-30 11:31:59 +01:00
q = greedy_realloc ( p , allocated , need ) ;
if ( ! q )
return NULL ;
if ( * allocated > prev )
2014-01-31 06:51:32 +01:00
memzero ( & q [ prev ] , * allocated - prev ) ;
2013-11-30 11:31:59 +01:00
return q ;
}
2013-04-29 23:39:12 +02:00
bool id128_is_valid ( const char * s ) {
size_t i , l ;
l = strlen ( s ) ;
if ( l = = 32 ) {
/* Simple formatted 128bit hex string */
for ( i = 0 ; i < l ; i + + ) {
char c = s [ i ] ;
if ( ! ( c > = ' 0 ' & & c < = ' 9 ' ) & &
! ( c > = ' a ' & & c < = ' z ' ) & &
! ( c > = ' A ' & & c < = ' Z ' ) )
return false ;
}
} else if ( l = = 36 ) {
/* Formatted UUID */
for ( i = 0 ; i < l ; i + + ) {
char c = s [ i ] ;
if ( ( i = = 8 | | i = = 13 | | i = = 18 | | i = = 23 ) ) {
if ( c ! = ' - ' )
return false ;
} else {
if ( ! ( c > = ' 0 ' & & c < = ' 9 ' ) & &
! ( c > = ' a ' & & c < = ' z ' ) & &
! ( c > = ' A ' & & c < = ' Z ' ) )
return false ;
}
}
} else
return false ;
return true ;
}
2013-06-09 22:54:39 +02:00
2013-07-18 20:22:29 +02:00
int split_pair ( const char * s , const char * sep , char * * l , char * * r ) {
char * x , * a , * b ;
assert ( s ) ;
assert ( sep ) ;
assert ( l ) ;
assert ( r ) ;
if ( isempty ( sep ) )
return - EINVAL ;
x = strstr ( s , sep ) ;
if ( ! x )
return - EINVAL ;
a = strndup ( s , x - s ) ;
if ( ! a )
return - ENOMEM ;
b = strdup ( x + strlen ( sep ) ) ;
if ( ! b ) {
free ( a ) ;
return - ENOMEM ;
}
* l = a ;
* r = b ;
return 0 ;
}
2013-10-19 00:46:07 +02:00
2013-11-06 03:15:16 +01:00
int shall_restore_state ( void ) {
2013-10-19 00:46:07 +02:00
_cleanup_free_ char * line ;
char * w , * state ;
size_t l ;
2013-11-06 03:15:16 +01:00
int r ;
2013-10-19 00:46:07 +02:00
2013-11-06 03:15:16 +01:00
r = proc_cmdline ( & line ) ;
if ( r < 0 )
return r ;
if ( r = = 0 ) /* Container ... */
return 1 ;
2013-10-19 00:46:07 +02:00
2013-11-06 03:15:16 +01:00
FOREACH_WORD_QUOTED ( w , l , line , state )
2013-12-05 15:30:04 +01:00
if ( l = = 23 & & strneq ( w , " systemd.restore_state=0 " , 23 ) )
2013-11-06 03:15:16 +01:00
return 0 ;
return 1 ;
}
int proc_cmdline ( char * * ret ) {
int r ;
if ( detect_container ( NULL ) > 0 ) {
2014-02-19 17:47:11 +01:00
char * buf = NULL , * p ;
2013-12-23 18:10:57 +01:00
size_t sz = 0 ;
r = read_full_file ( " /proc/1/cmdline " , & buf , & sz ) ;
if ( r < 0 )
return r ;
for ( p = buf ; p + 1 < buf + sz ; p + + )
if ( * p = = 0 )
* p = ' ' ;
* p = 0 ;
* ret = buf ;
return 1 ;
2013-10-19 00:46:07 +02:00
}
2013-11-06 03:15:16 +01:00
r = read_one_line_file ( " /proc/cmdline " , ret ) ;
if ( r < 0 )
return r ;
2013-10-19 00:46:07 +02:00
2013-11-06 03:15:16 +01:00
return 1 ;
2013-10-19 00:46:07 +02:00
}
2013-12-13 22:02:47 +01:00
2014-02-16 00:08:59 +01:00
int parse_proc_cmdline ( int ( * parse_word ) ( const char * word ) ) {
_cleanup_free_ char * line = NULL ;
char * w , * state ;
size_t l ;
int r ;
r = proc_cmdline ( & line ) ;
if ( r < 0 )
log_warning ( " Failed to read /proc/cmdline, ignoring: %s " , strerror ( - r ) ) ;
if ( r < = 0 )
return 0 ;
FOREACH_WORD_QUOTED ( w , l , line , state ) {
_cleanup_free_ char * word ;
word = strndup ( w , l ) ;
if ( ! word )
return log_oom ( ) ;
r = parse_word ( word ) ;
if ( r < 0 ) {
log_error ( " Failed on cmdline argument %s: %s " , word , strerror ( - r ) ) ;
return r ;
}
}
return 0 ;
}
2013-12-13 22:02:47 +01:00
int container_get_leader ( const char * machine , pid_t * pid ) {
_cleanup_free_ char * s = NULL , * class = NULL ;
const char * p ;
pid_t leader ;
int r ;
assert ( machine ) ;
assert ( pid ) ;
p = strappenda ( " /run/systemd/machines/ " , machine ) ;
r = parse_env_file ( p , NEWLINE , " LEADER " , & s , " CLASS " , & class , NULL ) ;
if ( r = = - ENOENT )
return - EHOSTDOWN ;
if ( r < 0 )
return r ;
if ( ! s )
return - EIO ;
if ( ! streq_ptr ( class , " container " ) )
return - EIO ;
r = parse_pid ( s , & leader ) ;
if ( r < 0 )
return r ;
if ( leader < = 1 )
return - EIO ;
* pid = leader ;
return 0 ;
}
2013-12-17 01:03:09 +01:00
int namespace_open ( pid_t pid , int * pidns_fd , int * mntns_fd , int * root_fd ) {
_cleanup_close_ int pidnsfd = - 1 , mntnsfd = - 1 ;
const char * pidns , * mntns , * root ;
2013-12-13 22:02:47 +01:00
int rfd ;
assert ( pid > = 0 ) ;
2013-12-17 01:03:09 +01:00
assert ( pidns_fd ) ;
assert ( mntns_fd ) ;
2013-12-13 22:02:47 +01:00
assert ( root_fd ) ;
2013-12-17 01:03:09 +01:00
mntns = procfs_file_alloca ( pid , " ns/mnt " ) ;
mntnsfd = open ( mntns , O_RDONLY | O_NOCTTY | O_CLOEXEC ) ;
if ( mntnsfd < 0 )
return - errno ;
pidns = procfs_file_alloca ( pid , " ns/pid " ) ;
pidnsfd = open ( pidns , O_RDONLY | O_NOCTTY | O_CLOEXEC ) ;
if ( pidnsfd < 0 )
2013-12-13 22:02:47 +01:00
return - errno ;
root = procfs_file_alloca ( pid , " root " ) ;
rfd = open ( root , O_RDONLY | O_NOCTTY | O_CLOEXEC | O_DIRECTORY ) ;
if ( rfd < 0 )
return - errno ;
2013-12-17 01:03:09 +01:00
* pidns_fd = pidnsfd ;
* mntns_fd = mntnsfd ;
2013-12-13 22:02:47 +01:00
* root_fd = rfd ;
2013-12-17 01:03:09 +01:00
pidnsfd = - 1 ;
mntnsfd = - 1 ;
2013-12-13 22:02:47 +01:00
return 0 ;
}
2013-12-17 01:03:09 +01:00
int namespace_enter ( int pidns_fd , int mntns_fd , int root_fd ) {
assert ( pidns_fd > = 0 ) ;
assert ( mntns_fd > = 0 ) ;
2013-12-13 22:02:47 +01:00
assert ( root_fd > = 0 ) ;
2013-12-17 01:03:09 +01:00
if ( setns ( pidns_fd , CLONE_NEWPID ) < 0 )
return - errno ;
if ( setns ( mntns_fd , CLONE_NEWNS ) < 0 )
2013-12-13 22:02:47 +01:00
return - errno ;
if ( fchdir ( root_fd ) < 0 )
return - errno ;
if ( chroot ( " . " ) < 0 )
return - errno ;
2013-12-14 05:04:49 +01:00
if ( setresgid ( 0 , 0 , 0 ) < 0 )
return - errno ;
if ( setresuid ( 0 , 0 , 0 ) < 0 )
return - errno ;
2013-12-13 22:02:47 +01:00
return 0 ;
}
2013-12-18 04:19:20 +01:00
2014-02-17 18:28:53 +01:00
bool pid_is_unwaited ( pid_t pid ) {
/* Checks whether a PID is still valid at all, including a zombie */
2013-12-18 04:19:20 +01:00
if ( pid < = 0 )
return false ;
if ( kill ( pid , 0 ) > = 0 )
return true ;
return errno ! = ESRCH ;
}
2013-12-24 15:53:04 +01:00
2014-02-17 18:28:53 +01:00
bool pid_is_alive ( pid_t pid ) {
int r ;
/* Checks whether a PID is still valid and not a zombie */
if ( pid < = 0 )
return false ;
r = get_process_state ( pid ) ;
if ( r = = - ENOENT | | r = = ' Z ' )
return false ;
return true ;
}
2013-12-24 15:53:04 +01:00
int getpeercred ( int fd , struct ucred * ucred ) {
socklen_t n = sizeof ( struct ucred ) ;
struct ucred u ;
int r ;
assert ( fd > = 0 ) ;
assert ( ucred ) ;
r = getsockopt ( fd , SOL_SOCKET , SO_PEERCRED , & u , & n ) ;
if ( r < 0 )
return - errno ;
if ( n ! = sizeof ( struct ucred ) )
return - EIO ;
/* Check if the data is actually useful and not suppressed due
* to namespacing issues */
if ( u . pid < = 0 )
return - ENODATA ;
* ucred = u ;
return 0 ;
}
int getpeersec ( int fd , char * * ret ) {
socklen_t n = 64 ;
char * s ;
int r ;
assert ( fd > = 0 ) ;
assert ( ret ) ;
s = new0 ( char , n ) ;
if ( ! s )
return - ENOMEM ;
r = getsockopt ( fd , SOL_SOCKET , SO_PEERSEC , s , & n ) ;
if ( r < 0 ) {
free ( s ) ;
if ( errno ! = ERANGE )
return - errno ;
s = new0 ( char , n ) ;
if ( ! s )
return - ENOMEM ;
r = getsockopt ( fd , SOL_SOCKET , SO_PEERSEC , s , & n ) ;
if ( r < 0 ) {
free ( s ) ;
return - errno ;
}
}
2013-12-24 16:21:59 +01:00
if ( isempty ( s ) ) {
free ( s ) ;
return - ENOTSUP ;
}
2013-12-24 15:53:04 +01:00
* ret = s ;
return 0 ;
}
2014-01-26 02:48:01 +01:00
2014-01-29 00:25:31 +01:00
/* This is much like like mkostemp() but is subject to umask(). */
2014-01-26 05:35:28 +01:00
int mkostemp_safe ( char * pattern , int flags ) {
2014-01-28 13:47:35 +01:00
_cleanup_umask_ mode_t u ;
2014-01-29 00:25:31 +01:00
int fd ;
2014-01-26 05:35:28 +01:00
2014-01-28 13:08:34 +01:00
assert ( pattern ) ;
2014-01-26 05:35:28 +01:00
2014-01-28 13:47:35 +01:00
u = umask ( 077 ) ;
2014-01-29 00:25:31 +01:00
fd = mkostemp ( pattern , flags ) ;
if ( fd < 0 )
return - errno ;
2014-01-26 05:35:28 +01:00
2014-01-29 00:25:31 +01:00
return fd ;
2014-01-26 05:35:28 +01:00
}
2014-01-26 02:48:01 +01:00
int open_tmpfile ( const char * path , int flags ) {
char * p ;
2014-01-28 13:09:14 +01:00
int fd ;
assert ( path ) ;
2014-01-26 02:48:01 +01:00
# ifdef O_TMPFILE
2014-01-28 13:26:48 +01:00
/* Try O_TMPFILE first, if it is supported */
fd = open ( path , flags | O_TMPFILE , S_IRUSR | S_IWUSR ) ;
2014-01-26 02:48:01 +01:00
if ( fd > = 0 )
return fd ;
# endif
2014-01-28 13:26:48 +01:00
/* Fall back to unguessable name + unlinking */
2014-01-26 02:48:01 +01:00
p = strappenda ( path , " /systemd-tmp-XXXXXX " ) ;
2014-01-28 13:09:14 +01:00
fd = mkostemp_safe ( p , flags ) ;
2014-01-26 02:48:01 +01:00
if ( fd < 0 )
2014-01-26 05:35:28 +01:00
return fd ;
2014-01-26 02:48:01 +01:00
unlink ( p ) ;
return fd ;
}
2014-02-03 12:52:16 +01:00
int fd_warn_permissions ( const char * path , int fd ) {
struct stat st ;
if ( fstat ( fd , & st ) < 0 )
return - errno ;
if ( st . st_mode & 0111 )
log_warning ( " Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway. " , path ) ;
if ( st . st_mode & 0002 )
log_warning ( " Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway. " , path ) ;
if ( getpid ( ) = = 1 & & ( st . st_mode & 0044 ) ! = 0044 )
log_warning ( " Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway. " , path ) ;
return 0 ;
}
2014-02-18 23:35:19 +01:00
2014-02-19 02:15:24 +01:00
unsigned long personality_from_string ( const char * p ) {
2014-02-18 23:35:19 +01:00
/* Parse a personality specifier. We introduce our own
* identifiers that indicate specific ABIs , rather than just
* hints regarding the register size , since we want to keep
* things open for multiple locally supported ABIs for the
* same register size . We try to reuse the ABI identifiers
* used by libseccomp . */
# if defined(__x86_64__)
if ( streq ( p , " x86 " ) )
return PER_LINUX32 ;
if ( streq ( p , " x86-64 " ) )
return PER_LINUX ;
# elif defined(__i386__)
if ( streq ( p , " x86 " ) )
return PER_LINUX ;
# endif
/* personality(7) documents that 0xffffffffUL is used for
* querying the current personality , hence let ' s use that here
* as error indicator . */
return 0xffffffffUL ;
}
2014-02-19 02:15:24 +01:00
const char * personality_to_string ( unsigned long p ) {
# if defined(__x86_64__)
if ( p = = PER_LINUX32 )
return " x86 " ;
if ( p = = PER_LINUX )
return " x86-64 " ;
# elif defined(__i386__)
if ( p = = PER_LINUX )
return " x86 " ;
# endif
return NULL ;
}
2014-03-04 19:20:21 +01:00
uint64_t physical_memory ( void ) {
long mem ;
/* We return this as uint64_t in case we are running as 32bit
* process on a 64 bit kernel with huge amounts of memory */
mem = sysconf ( _SC_PHYS_PAGES ) ;
assert ( mem > 0 ) ;
return ( uint64_t ) mem * ( uint64_t ) page_size ( ) ;
}