]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/user-runtime-dir.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "main-func.h"
13 #include "mount-util.h"
14 #include "path-util.h"
16 #include "selinux-util.h"
17 #include "smack-util.h"
18 #include "stdio-util.h"
19 #include "string-util.h"
21 #include "user-util.h"
23 static int acquire_runtime_dir_size ( uint64_t * ret
) {
24 _cleanup_ ( sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
25 _cleanup_ ( sd_bus_unrefp
) sd_bus
* bus
= NULL
;
28 r
= sd_bus_default_system (& bus
);
30 return log_error_errno ( r
, "Failed to connect to system bus: %m" );
32 r
= sd_bus_get_property_trivial ( bus
, "org.freedesktop.login1" , "/org/freedesktop/login1" , "org.freedesktop.login1.Manager" , "RuntimeDirectorySize" , & error
, 't' , ret
);
34 return log_error_errno ( r
, "Failed to acquire runtime directory size: %s" , bus_error_message (& error
, r
));
39 static int user_mkdir_runtime_path (
40 const char * runtime_path
,
43 uint64_t runtime_dir_size
) {
48 assert ( path_is_absolute ( runtime_path
));
49 assert ( uid_is_valid ( uid
));
50 assert ( gid_is_valid ( gid
));
52 r
= mkdir_safe_label ( "/run/user" , 0755 , 0 , 0 , MKDIR_WARN_MODE
);
54 return log_error_errno ( r
, "Failed to create /run/user: %m" );
56 if ( path_is_mount_point ( runtime_path
, NULL
, 0 ) >= 0 )
57 log_debug ( "%s is already a mount point" , runtime_path
);
59 char options
[ sizeof ( "mode=0700,uid=,gid=,size=,smackfsroot=*" )
60 + DECIMAL_STR_MAX ( uid_t
)
61 + DECIMAL_STR_MAX ( gid_t
)
62 + DECIMAL_STR_MAX ( uint64_t )];
65 "mode=0700,uid=" UID_FMT
",gid=" GID_FMT
",size=%" PRIu64
"%s" ,
66 uid
, gid
, runtime_dir_size
,
67 mac_smack_use () ? ",smackfsroot=*" : "" );
69 ( void ) mkdir_label ( runtime_path
, 0700 );
71 r
= mount ( "tmpfs" , runtime_path
, "tmpfs" , MS_NODEV
| MS_NOSUID
, options
);
73 if (! IN_SET ( errno
, EPERM
, EACCES
)) {
74 r
= log_error_errno ( errno
, "Failed to mount per-user tmpfs directory %s: %m" , runtime_path
);
78 log_debug_errno ( errno
, "Failed to mount per-user tmpfs directory %s. \n "
79 "Assuming containerized execution, ignoring: %m" , runtime_path
);
81 r
= chmod_and_chown ( runtime_path
, 0700 , uid
, gid
);
83 log_error_errno ( r
, "Failed to change ownership and mode of \" %s \" : %m" , runtime_path
);
88 r
= label_fix ( runtime_path
, 0 );
90 log_warning_errno ( r
, "Failed to fix label of \" %s \" , ignoring: %m" , runtime_path
);
96 /* Try to clean up, but ignore errors */
97 ( void ) rmdir ( runtime_path
);
101 static int user_remove_runtime_path ( const char * runtime_path
) {
104 assert ( runtime_path
);
105 assert ( path_is_absolute ( runtime_path
));
107 r
= rm_rf ( runtime_path
, 0 );
109 log_debug_errno ( r
, "Failed to remove runtime directory %s (before unmounting), ignoring: %m" , runtime_path
);
111 /* Ignore cases where the directory isn't mounted, as that's quite possible, if we lacked the permissions to
113 r
= umount2 ( runtime_path
, MNT_DETACH
);
114 if ( r
< 0 && ! IN_SET ( errno
, EINVAL
, ENOENT
))
115 log_debug_errno ( errno
, "Failed to unmount user runtime directory %s, ignoring: %m" , runtime_path
);
117 r
= rm_rf ( runtime_path
, REMOVE_ROOT
);
118 if ( r
< 0 && r
!= - ENOENT
)
119 return log_error_errno ( r
, "Failed to remove runtime directory %s (after unmounting): %m" , runtime_path
);
124 static int do_mount ( const char * user
) {
125 char runtime_path
[ sizeof ( "/run/user" ) + DECIMAL_STR_MAX ( uid_t
)];
126 uint64_t runtime_dir_size
;
131 r
= get_user_creds (& user
, & uid
, & gid
, NULL
, NULL
, 0 );
133 return log_error_errno ( r
,
134 r
== - ESRCH
? "No such user \" %s \" " :
135 r
== - ENOMSG
? "UID \" %s \" is invalid or has an invalid main group"
136 : "Failed to look up user \" %s \" : %m" ,
139 r
= acquire_runtime_dir_size (& runtime_dir_size
);
143 xsprintf ( runtime_path
, "/run/user/" UID_FMT
, uid
);
145 log_debug ( "Will mount %s owned by " UID_FMT
":" GID_FMT
, runtime_path
, uid
, gid
);
146 return user_mkdir_runtime_path ( runtime_path
, uid
, gid
, runtime_dir_size
);
149 static int do_umount ( const char * user
) {
150 char runtime_path
[ sizeof ( "/run/user" ) + DECIMAL_STR_MAX ( uid_t
)];
154 /* The user may be already removed. So, first try to parse the string by parse_uid(),
155 * and if it fails, fallback to get_user_creds().*/
156 if ( parse_uid ( user
, & uid
) < 0 ) {
157 r
= get_user_creds (& user
, & uid
, NULL
, NULL
, NULL
, 0 );
159 return log_error_errno ( r
,
160 r
== - ESRCH
? "No such user \" %s \" " :
161 r
== - ENOMSG
? "UID \" %s \" is invalid or has an invalid main group"
162 : "Failed to look up user \" %s \" : %m" ,
166 xsprintf ( runtime_path
, "/run/user/" UID_FMT
, uid
);
168 log_debug ( "Will remove %s" , runtime_path
);
169 return user_remove_runtime_path ( runtime_path
);
172 static int run ( int argc
, char * argv
[]) {
175 log_parse_environment ();
179 return log_error_errno ( SYNTHETIC_ERRNO ( EINVAL
),
180 "This program takes two arguments." );
181 if (! STR_IN_SET ( argv
[ 1 ], "start" , "stop" ))
182 return log_error_errno ( SYNTHETIC_ERRNO ( EINVAL
),
183 "First argument must be either \" start \" or \" stop \" ." );
185 r
= mac_selinux_init ();
187 return log_error_errno ( r
, "Could not initialize labelling: %m \n " );
191 if ( streq ( argv
[ 1 ], "start" ))
192 return do_mount ( argv
[ 2 ]);
193 if ( streq ( argv
[ 1 ], "stop" ))
194 return do_umount ( argv
[ 2 ]);
195 assert_not_reached ( "Unknown verb!" );
198 DEFINE_MAIN_FUNCTION ( run
);