4444
4545#define RELEASE_USEC (20*USEC_PER_SEC)
4646
47- static void session_remove_fifo (Session * s );
4847static void session_restore_vt (Session * s );
4948
5049int session_new (Manager * m , const char * id , Session * * ret ) {
@@ -66,7 +65,6 @@ int session_new(Manager *m, const char *id, Session **ret) {
6665 .manager = m ,
6766 .id = strdup (id ),
6867 .state_file = path_join ("/run/systemd/sessions/" , id ),
69- .fifo_fd = - EBADF ,
7068 .vtfd = - EBADF ,
7169 .audit_id = AUDIT_SESSION_INVALID ,
7270 .tty_validity = _TTY_VALIDITY_INVALID ,
@@ -107,11 +105,9 @@ static int session_watch_pidfd(Session *s) {
107105 assert (s );
108106 assert (s -> manager );
109107 assert (pidref_is_set (& s -> leader ));
108+ assert (s -> leader .fd >= 0 );
110109 assert (!s -> leader_pidfd_event_source );
111110
112- if (s -> leader .fd < 0 )
113- return 0 ;
114-
115111 r = sd_event_add_io (s -> manager -> event , & s -> leader_pidfd_event_source , s -> leader .fd , EPOLLIN , session_dispatch_leader_pidfd , s );
116112 if (r < 0 )
117113 return r ;
@@ -209,12 +205,7 @@ Session* session_free(Session *s) {
209205
210206 hashmap_remove (s -> manager -> sessions , s -> id );
211207
212- sd_event_source_unref (s -> fifo_event_source );
213- safe_close (s -> fifo_fd );
214-
215- /* Note that we remove neither the state file nor the fifo path here, since we want both to survive
216- * daemon restarts */
217- free (s -> fifo_path );
208+ /* Note that we don't remove the state file here, since it's supposed to survive daemon restarts */
218209 free (s -> state_file );
219210 free (s -> id );
220211
@@ -237,6 +228,7 @@ int session_set_leader_consume(Session *s, PidRef _leader) {
237228
238229 assert (s );
239230 assert (pidref_is_set (& pidref ));
231+ assert (pidref .fd >= 0 );
240232
241233 if (pidref_equal (& s -> leader , & pidref ))
242234 return 0 ;
@@ -332,9 +324,6 @@ int session_save(Session *s) {
332324 if (s -> scope_job )
333325 fprintf (f , "SCOPE_JOB=%s\n" , s -> scope_job );
334326
335- if (s -> fifo_path )
336- fprintf (f , "FIFO=%s\n" , s -> fifo_path );
337-
338327 if (s -> seat )
339328 fprintf (f , "SEAT=%s\n" , s -> seat -> id );
340329
@@ -486,7 +475,8 @@ int session_load(Session *s) {
486475 * controller = NULL ,
487476 * active = NULL ,
488477 * devices = NULL ,
489- * is_display = NULL ;
478+ * is_display = NULL ,
479+ * fifo_path = NULL ; /* compat only, not used */
490480
491481 int k , r ;
492482
@@ -496,7 +486,7 @@ int session_load(Session *s) {
496486 "REMOTE" , & remote ,
497487 "SCOPE" , & s -> scope ,
498488 "SCOPE_JOB" , & s -> scope_job ,
499- "FIFO" , & s -> fifo_path ,
489+ "FIFO" , & fifo_path ,
500490 "SEAT" , & seat ,
501491 "TTY" , & s -> tty ,
502492 "TTY_VALIDITY" , & tty_validity ,
@@ -615,19 +605,10 @@ int session_load(Session *s) {
615605 if (streq_ptr (state , "closing" ))
616606 s -> stopping = true;
617607
618- if (s -> fifo_path ) {
619- int fd ;
620-
621- /* If we open an unopened pipe for reading we will not
622- get an EOF. to trigger an EOF we hence open it for
623- writing, but close it right away which then will
624- trigger the EOF. This will happen immediately if no
625- other process has the FIFO open for writing, i. e.
626- when the session died before logind (re)started. */
627-
628- fd = session_create_fifo (s );
629- safe_close (fd );
630- }
608+ /* logind before v258 used a fifo for session close notification. Since v258 we fully employ
609+ * pidfd for the job, hence just unlink the legacy fifo. */
610+ if (fifo_path )
611+ (void ) unlink (fifo_path );
631612
632613 if (realtime )
633614 (void ) deserialize_usec (realtime , & s -> timestamp .realtime );
@@ -681,13 +662,19 @@ int session_load(Session *s) {
681662 _cleanup_ (pidref_done ) PidRef p = PIDREF_NULL ;
682663
683664 r = pidref_set_pid (& p , s -> deserialized_pid );
684- if (r >= 0 )
685- r = session_set_leader_consume (s , TAKE_PIDREF (p ));
686665 if (r < 0 )
687- log_warning_errno (r , "Failed to set leader PID for session '%s': %m" , s -> id );
666+ return log_error_errno (r , "Failed to deserialize leader PID for session '%s': %m" , s -> id );
667+ if (p .fd < 0 )
668+ return log_error_errno (SYNTHETIC_ERRNO (ENOTRECOVERABLE ),
669+ "Failed to acquire pidfd for session leader '" PID_FMT "', refusing." ,
670+ p .pid );
671+
672+ r = session_set_leader_consume (s , TAKE_PIDREF (p ));
673+ if (r < 0 )
674+ return log_error_errno (r , "Failed to set leader PID for session '%s': %m" , s -> id );
688675 }
689676
690- return r ;
677+ return 0 ;
691678}
692679
693680int session_activate (Session * s ) {
@@ -968,9 +955,6 @@ int session_stop(Session *s, bool force) {
968955 if (s -> seat )
969956 seat_evict_position (s -> seat , s );
970957
971- /* We are going down, don't care about FIFOs anymore */
972- session_remove_fifo (s );
973-
974958 /* Kill cgroup */
975959 r = session_stop_scope (s , force );
976960
@@ -1264,71 +1248,6 @@ int session_set_tty(Session *s, const char *tty) {
12641248 return 1 ;
12651249}
12661250
1267- static int session_dispatch_fifo (sd_event_source * es , int fd , uint32_t revents , void * userdata ) {
1268- Session * s = ASSERT_PTR (userdata );
1269-
1270- assert (s -> fifo_fd == fd );
1271-
1272- /* EOF on the FIFO means the session died abnormally. */
1273-
1274- session_remove_fifo (s );
1275- session_stop (s , /* force = */ false);
1276-
1277- session_add_to_gc_queue (s );
1278-
1279- return 1 ;
1280- }
1281-
1282- int session_create_fifo (Session * s ) {
1283- int r ;
1284-
1285- assert (s );
1286-
1287- /* Create FIFO */
1288- if (!s -> fifo_path ) {
1289- r = mkdir_safe_label ("/run/systemd/sessions" , 0755 , 0 , 0 , MKDIR_WARN_MODE );
1290- if (r < 0 )
1291- return r ;
1292-
1293- s -> fifo_path = strjoin ("/run/systemd/sessions/" , s -> id , ".ref" );
1294- if (!s -> fifo_path )
1295- return - ENOMEM ;
1296-
1297- if (mkfifo (s -> fifo_path , 0600 ) < 0 && errno != EEXIST )
1298- return - errno ;
1299- }
1300-
1301- /* Open reading side */
1302- if (s -> fifo_fd < 0 ) {
1303- s -> fifo_fd = open (s -> fifo_path , O_RDONLY |O_CLOEXEC |O_NONBLOCK );
1304- if (s -> fifo_fd < 0 )
1305- return - errno ;
1306- }
1307-
1308- if (!s -> fifo_event_source ) {
1309- r = sd_event_add_io (s -> manager -> event , & s -> fifo_event_source , s -> fifo_fd , 0 , session_dispatch_fifo , s );
1310- if (r < 0 )
1311- return r ;
1312-
1313- /* Let's make sure we noticed dead sessions before we process new bus requests (which might
1314- * create new sessions). */
1315- r = sd_event_source_set_priority (s -> fifo_event_source , SD_EVENT_PRIORITY_NORMAL - 10 );
1316- if (r < 0 )
1317- return r ;
1318- }
1319-
1320- /* Open writing side */
1321- return RET_NERRNO (open (s -> fifo_path , O_WRONLY |O_CLOEXEC |O_NONBLOCK ));
1322- }
1323-
1324- static void session_remove_fifo (Session * s ) {
1325- assert (s );
1326-
1327- s -> fifo_event_source = sd_event_source_unref (s -> fifo_event_source );
1328- s -> fifo_fd = safe_close (s -> fifo_fd );
1329- s -> fifo_path = unlink_and_free (s -> fifo_path );
1330- }
1331-
13321251bool session_may_gc (Session * s , bool drop_not_started ) {
13331252 int r ;
13341253
@@ -1350,9 +1269,6 @@ bool session_may_gc(Session *s, bool drop_not_started) {
13501269 if (r > 0 )
13511270 return false;
13521271
1353- if (s -> fifo_fd >= 0 && pipe_eof (s -> fifo_fd ) <= 0 )
1354- return false;
1355-
13561272 if (s -> scope_job ) {
13571273 _cleanup_ (sd_bus_error_free ) sd_bus_error error = SD_BUS_ERROR_NULL ;
13581274
@@ -1393,7 +1309,7 @@ SessionState session_get_state(Session *s) {
13931309 if (s -> stopping || s -> timer_event_source )
13941310 return SESSION_CLOSING ;
13951311
1396- if (s -> scope_job || ( !pidref_is_set (& s -> leader ) && s -> fifo_fd < 0 ))
1312+ if (s -> scope_job || !pidref_is_set (& s -> leader ))
13971313 return SESSION_OPENING ;
13981314
13991315 if (session_is_active (s ))
0 commit comments