chiark / gitweb /
logind: simplify session_activate() a bit
[elogind.git] / src / login / logind-session.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/epoll.h>
26 #include <fcntl.h>
27
28 #include "logind-session.h"
29 #include "strv.h"
30 #include "util.h"
31 #include "cgroup-util.h"
32
33 #define IDLE_THRESHOLD_USEC (5*USEC_PER_MINUTE)
34
35 Session* session_new(Manager *m, User *u, const char *id) {
36         Session *s;
37
38         assert(m);
39         assert(id);
40
41         s = new0(Session, 1);
42         if (!s)
43                 return NULL;
44
45         s->state_file = strappend("/run/systemd/sessions/", id);
46         if (!s->state_file) {
47                 free(s);
48                 return NULL;
49         }
50
51         s->id = file_name_from_path(s->state_file);
52
53         if (hashmap_put(m->sessions, s->id, s) < 0) {
54                 free(s->id);
55                 free(s);
56                 return NULL;
57         }
58
59         s->manager = m;
60         s->fifo_fd = -1;
61         s->user = u;
62
63         LIST_PREPEND(Session, sessions_by_user, u->sessions, s);
64
65         return s;
66 }
67
68 void session_free(Session *s) {
69         assert(s);
70
71         if (s->in_gc_queue)
72                 LIST_REMOVE(Session, gc_queue, s->manager->session_gc_queue, s);
73
74         if (s->user) {
75                 LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s);
76
77                 if (s->user->display == s)
78                         s->user->display = NULL;
79         }
80
81         if (s->seat) {
82                 if (s->seat->active == s)
83                         s->seat->active = NULL;
84
85                 LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s);
86         }
87
88         if (s->cgroup_path)
89                 hashmap_remove(s->manager->cgroups, s->cgroup_path);
90
91         free(s->cgroup_path);
92         strv_free(s->controllers);
93
94         free(s->tty);
95         free(s->display);
96         free(s->remote_host);
97         free(s->remote_user);
98         free(s->service);
99
100         hashmap_remove(s->manager->sessions, s->id);
101
102         session_remove_fifo(s);
103
104         free(s->state_file);
105         free(s);
106 }
107
108 int session_save(Session *s) {
109         FILE *f;
110         int r = 0;
111         char *temp_path;
112
113         assert(s);
114
115         if (!s->started)
116                 return 0;
117
118         r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
119         if (r < 0)
120                 goto finish;
121
122         r = fopen_temporary(s->state_file, &f, &temp_path);
123         if (r < 0)
124                 goto finish;
125
126         assert(s->user);
127
128         fchmod(fileno(f), 0644);
129
130         fprintf(f,
131                 "# This is private data. Do not parse.\n"
132                 "UID=%lu\n"
133                 "USER=%s\n"
134                 "ACTIVE=%i\n"
135                 "REMOTE=%i\n"
136                 "KILL_PROCESSES=%i\n",
137                 (unsigned long) s->user->uid,
138                 s->user->name,
139                 session_is_active(s),
140                 s->remote,
141                 s->kill_processes);
142
143         if (s->type >= 0)
144                 fprintf(f,
145                         "TYPE=%s\n",
146                         session_type_to_string(s->type));
147
148         if (s->cgroup_path)
149                 fprintf(f,
150                         "CGROUP=%s\n",
151                         s->cgroup_path);
152
153         if (s->fifo_path)
154                 fprintf(f,
155                         "FIFO=%s\n",
156                         s->fifo_path);
157
158         if (s->seat)
159                 fprintf(f,
160                         "SEAT=%s\n",
161                         s->seat->id);
162
163         if (s->tty)
164                 fprintf(f,
165                         "TTY=%s\n",
166                         s->tty);
167
168         if (s->display)
169                 fprintf(f,
170                         "DISPLAY=%s\n",
171                         s->display);
172
173         if (s->remote_host)
174                 fprintf(f,
175                         "REMOTE_HOST=%s\n",
176                         s->remote_host);
177
178         if (s->remote_user)
179                 fprintf(f,
180                         "REMOTE_USER=%s\n",
181                         s->remote_user);
182
183         if (s->service)
184                 fprintf(f,
185                         "SERVICE=%s\n",
186                         s->service);
187
188         if (s->seat && seat_can_multi_session(s->seat))
189                 fprintf(f,
190                         "VTNR=%i\n",
191                         s->vtnr);
192
193         if (s->leader > 0)
194                 fprintf(f,
195                         "LEADER=%lu\n",
196                         (unsigned long) s->leader);
197
198         if (s->audit_id > 0)
199                 fprintf(f,
200                         "AUDIT=%llu\n",
201                         (unsigned long long) s->audit_id);
202
203         fflush(f);
204
205         if (ferror(f) || rename(temp_path, s->state_file) < 0) {
206                 r = -errno;
207                 unlink(s->state_file);
208                 unlink(temp_path);
209         }
210
211         fclose(f);
212         free(temp_path);
213
214 finish:
215         if (r < 0)
216                 log_error("Failed to save session data for %s: %s", s->id, strerror(-r));
217
218         return r;
219 }
220
221 int session_load(Session *s) {
222         char *remote = NULL,
223                 *kill_processes = NULL,
224                 *seat = NULL,
225                 *vtnr = NULL,
226                 *leader = NULL,
227                 *audit_id = NULL,
228                 *type = NULL;
229
230         int k, r;
231
232         assert(s);
233
234         r = parse_env_file(s->state_file, NEWLINE,
235                            "REMOTE",         &remote,
236                            "KILL_PROCESSES", &kill_processes,
237                            "CGROUP",         &s->cgroup_path,
238                            "FIFO",           &s->fifo_path,
239                            "SEAT",           &seat,
240                            "TTY",            &s->tty,
241                            "DISPLAY",        &s->display,
242                            "REMOTE_HOST",    &s->remote_host,
243                            "REMOTE_USER",    &s->remote_user,
244                            "SERVICE",        &s->service,
245                            "VTNR",           &vtnr,
246                            "LEADER",         &leader,
247                            "TYPE",           &type,
248                            NULL);
249
250         if (r < 0)
251                 goto finish;
252
253         if (remote) {
254                 k = parse_boolean(remote);
255                 if (k >= 0)
256                         s->remote = k;
257         }
258
259         if (kill_processes) {
260                 k = parse_boolean(kill_processes);
261                 if (k >= 0)
262                         s->kill_processes = k;
263         }
264
265         if (seat && !s->seat) {
266                 Seat *o;
267
268                 o = hashmap_get(s->manager->seats, seat);
269                 if (o)
270                         seat_attach_session(o, s);
271         }
272
273         if (vtnr && s->seat && seat_can_multi_session(s->seat)) {
274                 int v;
275
276                 k = safe_atoi(vtnr, &v);
277                 if (k >= 0 && v >= 1)
278                         s->vtnr = v;
279         }
280
281         if (leader) {
282                 pid_t pid;
283
284                 k = parse_pid(leader, &pid);
285                 if (k >= 0 && pid >= 1) {
286                         s->leader = pid;
287
288                         audit_session_from_pid(pid, &s->audit_id);
289                 }
290         }
291
292         if (type) {
293                 SessionType t;
294
295                 t = session_type_from_string(type);
296                 if (t >= 0)
297                         s->type = t;
298         }
299
300         if (s->fifo_path) {
301                 int fd;
302
303                 /* If we open an unopened pipe for reading we will not
304                    get an EOF. to trigger an EOF we hence open it for
305                    reading, but close it right-away which then will
306                    trigger the EOF. */
307
308                 fd = session_create_fifo(s);
309                 if (fd >= 0)
310                         close_nointr_nofail(fd);
311         }
312
313
314 finish:
315         free(remote);
316         free(kill_processes);
317         free(seat);
318         free(vtnr);
319         free(leader);
320         free(audit_id);
321
322         return r;
323 }
324
325 int session_activate(Session *s) {
326         int r;
327
328         assert(s);
329
330         if (s->vtnr < 0)
331                 return -ENOTSUP;
332
333         if (!s->seat)
334                 return -ENOTSUP;
335
336         if (s->seat->active == s)
337                 return 0;
338
339         assert(seat_is_vtconsole(s->seat));
340
341         r = chvt(s->vtnr);
342         if (r < 0)
343                 return r;
344
345         return seat_set_active(s->seat, s);
346 }
347
348 static int session_link_x11_socket(Session *s) {
349         char *t, *f, *c;
350         size_t k;
351
352         assert(s);
353         assert(s->user);
354         assert(s->user->runtime_path);
355
356         if (s->user->display)
357                 return 0;
358
359         if (!s->display || !display_is_local(s->display))
360                 return 0;
361
362         k = strspn(s->display+1, "0123456789");
363         f = new(char, sizeof("/tmp/.X11-unix/X") + k);
364         if (!f) {
365                 log_error("Out of memory");
366                 return -ENOMEM;
367         }
368
369         c = stpcpy(f, "/tmp/.X11-unix/X");
370         memcpy(c, s->display+1, k);
371         c[k] = 0;
372
373         if (access(f, F_OK) < 0) {
374                 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
375                 free(f);
376                 return -ENOENT;
377         }
378
379         t = strappend(s->user->runtime_path, "/X11/display");
380         if (!t) {
381                 log_error("Out of memory");
382                 free(f);
383                 return -ENOMEM;
384         }
385
386         mkdir_parents(t, 0755);
387
388         if (link(f, t) < 0) {
389                 if (errno == EEXIST) {
390                         unlink(t);
391
392                         if (link(f, t) >= 0)
393                                 goto done;
394                 }
395
396                 if (symlink(f, t) < 0) {
397
398                         if (errno == EEXIST) {
399                                 unlink(t);
400
401                                 if (symlink(f, t) >= 0)
402                                         goto done;
403                         }
404
405                         log_error("Failed to link %s to %s: %m", f, t);
406                         free(f);
407                         free(t);
408                         return -errno;
409                 }
410         }
411
412 done:
413         log_info("Linked %s to %s.", f, t);
414         free(f);
415         free(t);
416
417         s->user->display = s;
418
419         return 0;
420 }
421
422 static int session_create_one_group(Session *s, const char *controller, const char *path) {
423         int r;
424
425         assert(s);
426         assert(controller);
427         assert(path);
428
429         if (s->leader > 0) {
430                 r = cg_create_and_attach(controller, path, s->leader);
431                 if (r < 0)
432                         r = cg_create(controller, path);
433         } else
434                 r = cg_create(controller, path);
435
436         if (r < 0)
437                 return r;
438
439         r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
440         if (r >= 0)
441                 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
442
443         return r;
444 }
445
446 static int session_create_cgroup(Session *s) {
447         char **k;
448         char *p;
449         int r;
450
451         assert(s);
452         assert(s->user);
453         assert(s->user->cgroup_path);
454
455         if (!s->cgroup_path) {
456                 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
457                         log_error("Out of memory");
458                         return -ENOMEM;
459                 }
460         } else
461                 p = s->cgroup_path;
462
463         r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
464         if (r < 0) {
465                 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
466                 free(p);
467                 s->cgroup_path = NULL;
468                 return r;
469         }
470
471         s->cgroup_path = p;
472
473         STRV_FOREACH(k, s->controllers) {
474
475                 if (strv_contains(s->reset_controllers, *k))
476                         continue;
477
478                 r = session_create_one_group(s, *k, p);
479                 if (r < 0)
480                         log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
481         }
482
483         STRV_FOREACH(k, s->manager->controllers) {
484
485                 if (strv_contains(s->reset_controllers, *k) ||
486                     strv_contains(s->manager->reset_controllers, *k) ||
487                     strv_contains(s->controllers, *k))
488                         continue;
489
490                 r = session_create_one_group(s, *k, p);
491                 if (r < 0)
492                         log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
493         }
494
495         if (s->leader > 0) {
496
497                 STRV_FOREACH(k, s->reset_controllers) {
498                         r = cg_attach(*k, "/", s->leader);
499                         if (r < 0)
500                                 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
501
502                 }
503
504                 STRV_FOREACH(k, s->manager->reset_controllers) {
505
506                         if (strv_contains(s->reset_controllers, *k) ||
507                             strv_contains(s->controllers, *k))
508                                 continue;
509
510                         r = cg_attach(*k, "/", s->leader);
511                         if (r < 0)
512                                 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
513
514                 }
515         }
516
517         hashmap_put(s->manager->cgroups, s->cgroup_path, s);
518
519         return 0;
520 }
521
522 int session_start(Session *s) {
523         int r;
524
525         assert(s);
526         assert(s->user);
527
528         if (s->started)
529                 return 0;
530
531         r = user_start(s->user);
532         if (r < 0)
533                 return r;
534
535         log_full(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
536                  "New session %s of user %s.", s->id, s->user->name);
537
538         /* Create cgroup */
539         r = session_create_cgroup(s);
540         if (r < 0)
541                 return r;
542
543         /* Create X11 symlink */
544         session_link_x11_socket(s);
545
546         dual_timestamp_get(&s->timestamp);
547
548         if (s->seat)
549                 seat_read_active_vt(s->seat);
550
551         s->started = true;
552
553         /* Save session data */
554         session_save(s);
555         user_save(s->user);
556
557         session_send_signal(s, true);
558
559         if (s->seat) {
560                 seat_save(s->seat);
561
562                 if (s->seat->active == s)
563                         seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
564                 else
565                         seat_send_changed(s->seat, "Sessions\0");
566         }
567
568         user_send_changed(s->user, "Sessions\0");
569
570         return 0;
571 }
572
573 static bool session_shall_kill(Session *s) {
574         assert(s);
575
576         if (!s->kill_processes)
577                 return false;
578
579         if (strv_contains(s->manager->kill_exclude_users, s->user->name))
580                 return false;
581
582         if (strv_isempty(s->manager->kill_only_users))
583                 return true;
584
585         return strv_contains(s->manager->kill_only_users, s->user->name);
586 }
587
588 static int session_terminate_cgroup(Session *s) {
589         int r;
590         char **k;
591
592         assert(s);
593
594         if (!s->cgroup_path)
595                 return 0;
596
597         cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
598
599         if (session_shall_kill(s)) {
600
601                 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
602                 if (r < 0)
603                         log_error("Failed to kill session cgroup: %s", strerror(-r));
604
605         } else {
606                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
607                 if (r < 0)
608                         log_error("Failed to check session cgroup: %s", strerror(-r));
609                 else if (r > 0) {
610                         r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
611                         if (r < 0)
612                                 log_error("Failed to delete session cgroup: %s", strerror(-r));
613                 } else
614                         r = -EBUSY;
615         }
616
617         STRV_FOREACH(k, s->user->manager->controllers)
618                 cg_trim(*k, s->cgroup_path, true);
619
620         hashmap_remove(s->manager->cgroups, s->cgroup_path);
621
622         free(s->cgroup_path);
623         s->cgroup_path = NULL;
624
625         return r;
626 }
627
628 static int session_unlink_x11_socket(Session *s) {
629         char *t;
630         int r;
631
632         assert(s);
633         assert(s->user);
634
635         if (s->user->display != s)
636                 return 0;
637
638         s->user->display = NULL;
639
640         t = strappend(s->user->runtime_path, "/X11/display");
641         if (!t) {
642                 log_error("Out of memory");
643                 return -ENOMEM;
644         }
645
646         r = unlink(t);
647         free(t);
648
649         return r < 0 ? -errno : 0;
650 }
651
652 int session_stop(Session *s) {
653         int r = 0, k;
654
655         assert(s);
656
657         if (s->started)
658                 log_full(s->type == SESSION_TTY || s->type == SESSION_X11 ? LOG_INFO : LOG_DEBUG,
659                          "Removed session %s.", s->id);
660
661         /* Kill cgroup */
662         k = session_terminate_cgroup(s);
663         if (k < 0)
664                 r = k;
665
666         /* Remove X11 symlink */
667         session_unlink_x11_socket(s);
668
669         unlink(s->state_file);
670         session_add_to_gc_queue(s);
671         user_add_to_gc_queue(s->user);
672
673         if (s->started)
674                 session_send_signal(s, false);
675
676         if (s->seat) {
677                 if (s->seat->active == s)
678                         seat_set_active(s->seat, NULL);
679
680                 seat_send_changed(s->seat, "Sessions\0");
681         }
682
683         user_send_changed(s->user, "Sessions\0");
684
685         s->started = false;
686
687         return r;
688 }
689
690 bool session_is_active(Session *s) {
691         assert(s);
692
693         if (!s->seat)
694                 return true;
695
696         return s->seat->active == s;
697 }
698
699 int session_get_idle_hint(Session *s, dual_timestamp *t) {
700         char *p;
701         struct stat st;
702         usec_t u, n;
703         bool b;
704         int k;
705
706         assert(s);
707
708         if (s->idle_hint) {
709                 if (t)
710                         *t = s->idle_hint_timestamp;
711
712                 return s->idle_hint;
713         }
714
715         if (isempty(s->tty))
716                 goto dont_know;
717
718         if (s->tty[0] != '/') {
719                 p = strappend("/dev/", s->tty);
720                 if (!p)
721                         return -ENOMEM;
722         } else
723                 p = NULL;
724
725         if (!startswith(p ? p : s->tty, "/dev/")) {
726                 free(p);
727                 goto dont_know;
728         }
729
730         k = lstat(p ? p : s->tty, &st);
731         free(p);
732
733         if (k < 0)
734                 goto dont_know;
735
736         u = timespec_load(&st.st_atim);
737         n = now(CLOCK_REALTIME);
738         b = u + IDLE_THRESHOLD_USEC < n;
739
740         if (t)
741                 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
742
743         return b;
744
745 dont_know:
746         if (t)
747                 *t = s->idle_hint_timestamp;
748
749         return 0;
750 }
751
752 void session_set_idle_hint(Session *s, bool b) {
753         assert(s);
754
755         if (s->idle_hint == b)
756                 return;
757
758         s->idle_hint = b;
759         dual_timestamp_get(&s->idle_hint_timestamp);
760
761         session_send_changed(s,
762                              "IdleHint\0"
763                              "IdleSinceHint\0"
764                              "IdleSinceHintMonotonic\0");
765
766         if (s->seat)
767                 seat_send_changed(s->seat,
768                                   "IdleHint\0"
769                                   "IdleSinceHint\0"
770                                   "IdleSinceHintMonotonic\0");
771
772         user_send_changed(s->user,
773                           "IdleHint\0"
774                           "IdleSinceHint\0"
775                           "IdleSinceHintMonotonic\0");
776
777         manager_send_changed(s->manager,
778                              "IdleHint\0"
779                              "IdleSinceHint\0"
780                              "IdleSinceHintMonotonic\0");
781 }
782
783 int session_create_fifo(Session *s) {
784         int r;
785
786         assert(s);
787
788         /* Create FIFO */
789         if (!s->fifo_path) {
790                 r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
791                 if (r < 0)
792                         return r;
793
794                 if (asprintf(&s->fifo_path, "/run/systemd/sessions/%s.ref", s->id) < 0)
795                         return -ENOMEM;
796
797                 if (mkfifo(s->fifo_path, 0600) < 0 && errno != EEXIST)
798                         return -errno;
799         }
800
801         /* Open reading side */
802         if (s->fifo_fd < 0) {
803                 struct epoll_event ev;
804
805                 s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
806                 if (s->fifo_fd < 0)
807                         return -errno;
808
809                 r = hashmap_put(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1), s);
810                 if (r < 0)
811                         return r;
812
813                 zero(ev);
814                 ev.events = 0;
815                 ev.data.u32 = FD_FIFO_BASE + s->fifo_fd;
816
817                 if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, s->fifo_fd, &ev) < 0)
818                         return -errno;
819         }
820
821         /* Open writing side */
822         r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY);
823         if (r < 0)
824                 return -errno;
825
826         return r;
827 }
828
829 void session_remove_fifo(Session *s) {
830         assert(s);
831
832         if (s->fifo_fd >= 0) {
833                 assert_se(hashmap_remove(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1)) == s);
834                 assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->fifo_fd, NULL) == 0);
835                 close_nointr_nofail(s->fifo_fd);
836                 s->fifo_fd = -1;
837         }
838
839         if (s->fifo_path) {
840                 unlink(s->fifo_path);
841                 free(s->fifo_path);
842                 s->fifo_path = NULL;
843         }
844 }
845
846 int session_check_gc(Session *s, bool drop_not_started) {
847         int r;
848
849         assert(s);
850
851         if (drop_not_started && !s->started)
852                 return 0;
853
854         if (s->fifo_fd >= 0) {
855
856                 r = pipe_eof(s->fifo_fd);
857                 if (r < 0)
858                         return r;
859
860                 if (r == 0)
861                         return 1;
862         }
863
864         if (s->cgroup_path) {
865
866                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
867                 if (r < 0)
868                         return r;
869
870                 if (r <= 0)
871                         return 1;
872         }
873
874         return 0;
875 }
876
877 void session_add_to_gc_queue(Session *s) {
878         assert(s);
879
880         if (s->in_gc_queue)
881                 return;
882
883         LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
884         s->in_gc_queue = true;
885 }
886
887 int session_kill(Session *s, KillWho who, int signo) {
888         int r = 0;
889         Set *pid_set = NULL;
890
891         assert(s);
892
893         if (!s->cgroup_path)
894                 return -ESRCH;
895
896         if (s->leader <= 0 && who == KILL_LEADER)
897                 return -ESRCH;
898
899         if (s->leader > 0)
900                 if (kill(s->leader, signo) < 0)
901                         r = -errno;
902
903         if (who == KILL_ALL) {
904                 int q;
905
906                 pid_set = set_new(trivial_hash_func, trivial_compare_func);
907                 if (!pid_set)
908                         return -ENOMEM;
909
910                 if (s->leader > 0) {
911                         q = set_put(pid_set, LONG_TO_PTR(s->leader));
912                         if (q < 0)
913                                 r = q;
914                 }
915
916                 q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, signo, false, true, false, pid_set);
917                 if (q < 0)
918                         if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
919                                 r = q;
920         }
921
922         if (pid_set)
923                 set_free(pid_set);
924
925         return r;
926 }
927
928 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
929         [SESSION_TTY] = "tty",
930         [SESSION_X11] = "x11",
931         [SESSION_UNSPECIFIED] = "unspecified"
932 };
933
934 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);
935
936 static const char* const kill_who_table[_KILL_WHO_MAX] = {
937         [KILL_LEADER] = "leader",
938         [KILL_ALL] = "all"
939 };
940
941 DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);