2017-11-18 19:09:20 +03:00
/* SPDX-License-Identifier: LGPL-2.1+ */
2013-10-23 04:39:18 +04:00
/***
This file is part of systemd .
Copyright 2013 Zbigniew Jędrzejewski - Szmek
* * */
2013-11-27 23:23:18 +04:00
# include <sys/socket.h>
2013-10-23 04:39:18 +04:00
2015-10-27 05:01:06 +03:00
# include "alloc-util.h"
2015-10-25 15:14:12 +03:00
# include "fd-util.h"
2013-10-23 04:39:18 +04:00
# include "namespace.h"
2015-04-10 20:10:00 +03:00
# include "process-util.h"
2015-10-24 23:58:24 +03:00
# include "string-util.h"
# include "util.h"
2013-10-23 04:39:18 +04:00
static void test_tmpdir ( const char * id , const char * A , const char * B ) {
_cleanup_free_ char * a , * b ;
2013-11-27 23:23:18 +04:00
struct stat x , y ;
char * c , * d ;
2013-10-23 04:39:18 +04:00
2013-11-27 23:23:18 +04:00
assert_se ( setup_tmp_dirs ( id , & a , & b ) = = 0 ) ;
assert_se ( startswith ( a , A ) ) ;
assert_se ( startswith ( b , B ) ) ;
2013-10-23 04:39:18 +04:00
2013-11-27 23:23:18 +04:00
assert_se ( stat ( a , & x ) > = 0 ) ;
assert_se ( stat ( b , & y ) > = 0 ) ;
2013-10-23 04:39:18 +04:00
2013-11-27 23:23:18 +04:00
assert_se ( S_ISDIR ( x . st_mode ) ) ;
assert_se ( S_ISDIR ( y . st_mode ) ) ;
2013-10-23 04:39:18 +04:00
2013-11-27 23:23:18 +04:00
assert_se ( ( x . st_mode & 01777 ) = = 0700 ) ;
assert_se ( ( y . st_mode & 01777 ) = = 0700 ) ;
2015-02-03 04:05:59 +03:00
c = strjoina ( a , " /tmp " ) ;
d = strjoina ( b , " /tmp " ) ;
2013-11-27 23:23:18 +04:00
assert_se ( stat ( c , & x ) > = 0 ) ;
assert_se ( stat ( d , & y ) > = 0 ) ;
assert_se ( S_ISDIR ( x . st_mode ) ) ;
assert_se ( S_ISDIR ( y . st_mode ) ) ;
assert_se ( ( x . st_mode & 01777 ) = = 01777 ) ;
assert_se ( ( y . st_mode & 01777 ) = = 01777 ) ;
assert_se ( rmdir ( c ) > = 0 ) ;
assert_se ( rmdir ( d ) > = 0 ) ;
assert_se ( rmdir ( a ) > = 0 ) ;
assert_se ( rmdir ( b ) > = 0 ) ;
}
static void test_netns ( void ) {
2014-03-24 06:22:44 +04:00
_cleanup_close_pair_ int s [ 2 ] = { - 1 , - 1 } ;
2013-11-27 23:23:18 +04:00
pid_t pid1 , pid2 , pid3 ;
int r , n = 0 ;
siginfo_t si ;
2016-04-11 02:41:04 +03:00
if ( geteuid ( ) > 0 ) {
log_info ( " Skipping test: not root " ) ;
exit ( EXIT_TEST_SKIP ) ;
}
2013-11-27 23:23:18 +04:00
assert_se ( socketpair ( AF_UNIX , SOCK_DGRAM , 0 , s ) > = 0 ) ;
pid1 = fork ( ) ;
assert_se ( pid1 > = 0 ) ;
if ( pid1 = = 0 ) {
r = setup_netns ( s ) ;
assert_se ( r > = 0 ) ;
_exit ( r ) ;
}
pid2 = fork ( ) ;
assert_se ( pid2 > = 0 ) ;
if ( pid2 = = 0 ) {
r = setup_netns ( s ) ;
assert_se ( r > = 0 ) ;
exit ( r ) ;
}
pid3 = fork ( ) ;
assert_se ( pid3 > = 0 ) ;
if ( pid3 = = 0 ) {
r = setup_netns ( s ) ;
assert_se ( r > = 0 ) ;
exit ( r ) ;
}
r = wait_for_terminate ( pid1 , & si ) ;
assert_se ( r > = 0 ) ;
assert_se ( si . si_code = = CLD_EXITED ) ;
n + = si . si_status ;
r = wait_for_terminate ( pid2 , & si ) ;
assert_se ( r > = 0 ) ;
assert_se ( si . si_code = = CLD_EXITED ) ;
n + = si . si_status ;
r = wait_for_terminate ( pid3 , & si ) ;
assert_se ( r > = 0 ) ;
assert_se ( si . si_code = = CLD_EXITED ) ;
n + = si . si_status ;
assert_se ( n = = 1 ) ;
2013-10-23 04:39:18 +04:00
}
int main ( int argc , char * argv [ ] ) {
2013-12-13 05:05:04 +04:00
sd_id128_t bid ;
char boot_id [ SD_ID128_STRING_MAX ] ;
_cleanup_free_ char * x = NULL , * y = NULL , * z = NULL , * zz = NULL ;
2013-10-23 04:39:18 +04:00
2016-04-11 02:41:04 +03:00
log_parse_environment ( ) ;
log_open ( ) ;
2013-12-13 05:05:04 +04:00
assert_se ( sd_id128_get_boot ( & bid ) > = 0 ) ;
sd_id128_to_string ( bid , boot_id ) ;
2016-10-23 18:43:27 +03:00
x = strjoin ( " /tmp/systemd-private- " , boot_id , " -abcd.service- " ) ;
y = strjoin ( " /var/tmp/systemd-private- " , boot_id , " -abcd.service- " ) ;
2013-12-13 05:05:04 +04:00
assert_se ( x & & y ) ;
test_tmpdir ( " abcd.service " , x , y ) ;
2016-10-23 18:43:27 +03:00
z = strjoin ( " /tmp/systemd-private- " , boot_id , " -sys-devices-pci0000:00-0000:00:1a.0-usb3-3 \\ x2d1-3 \\ x2d1:1.0-bluetooth-hci0.device- " ) ;
zz = strjoin ( " /var/tmp/systemd-private- " , boot_id , " -sys-devices-pci0000:00-0000:00:1a.0-usb3-3 \\ x2d1-3 \\ x2d1:1.0-bluetooth-hci0.device- " ) ;
2013-12-13 05:05:04 +04:00
assert_se ( z & & zz ) ;
test_tmpdir ( " sys-devices-pci0000:00-0000:00:1a.0-usb3-3 \\ x2d1-3 \\ x2d1:1.0-bluetooth-hci0.device " , z , zz ) ;
2013-10-23 04:39:18 +04:00
2013-11-27 23:23:18 +04:00
test_netns ( ) ;
2013-10-23 04:39:18 +04:00
return 0 ;
}