chiark / gitweb /
label: fix database check
[elogind.git] / src / 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_is_vtconsole(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_is_vtconsole(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         Session *old_active;
328
329         assert(s);
330
331         if (s->vtnr < 0)
332                 return -ENOTSUP;
333
334         if (!s->seat)
335                 return -ENOTSUP;
336
337         if (s->seat->active == s)
338                 return 0;
339
340         assert(seat_is_vtconsole(s->seat));
341
342         r = chvt(s->vtnr);
343         if (r < 0)
344                 return r;
345
346         old_active = s->seat->active;
347         s->seat->active = s;
348
349         return seat_apply_acls(s->seat, old_active);
350 }
351
352 static int session_link_x11_socket(Session *s) {
353         char *t, *f, *c;
354         size_t k;
355
356         assert(s);
357         assert(s->user);
358         assert(s->user->runtime_path);
359
360         if (s->user->display)
361                 return 0;
362
363         if (!s->display || !display_is_local(s->display))
364                 return 0;
365
366         k = strspn(s->display+1, "0123456789");
367         f = new(char, sizeof("/tmp/.X11-unix/X") + k);
368         if (!f) {
369                 log_error("Out of memory");
370                 return -ENOMEM;
371         }
372
373         c = stpcpy(f, "/tmp/.X11-unix/X");
374         memcpy(c, s->display+1, k);
375         c[k] = 0;
376
377         if (access(f, F_OK) < 0) {
378                 log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f);
379                 free(f);
380                 return -ENOENT;
381         }
382
383         t = strappend(s->user->runtime_path, "/X11/display");
384         if (!t) {
385                 log_error("Out of memory");
386                 free(f);
387                 return -ENOMEM;
388         }
389
390         mkdir_parents(t, 0755);
391
392         if (link(f, t) < 0) {
393                 if (errno == EEXIST) {
394                         unlink(t);
395
396                         if (link(f, t) >= 0)
397                                 goto done;
398                 }
399
400                 if (symlink(f, t) < 0) {
401
402                         if (errno == EEXIST) {
403                                 unlink(t);
404
405                                 if (symlink(f, t) >= 0)
406                                         goto done;
407                         }
408
409                         log_error("Failed to link %s to %s: %m", f, t);
410                         free(f);
411                         free(t);
412                         return -errno;
413                 }
414         }
415
416 done:
417         log_info("Linked %s to %s.", f, t);
418         free(f);
419         free(t);
420
421         s->user->display = s;
422
423         return 0;
424 }
425
426 static int session_create_one_group(Session *s, const char *controller, const char *path) {
427         int r;
428
429         assert(s);
430         assert(controller);
431         assert(path);
432
433         if (s->leader > 0) {
434                 r = cg_create_and_attach(controller, path, s->leader);
435                 if (r < 0)
436                         r = cg_create(controller, path);
437         } else
438                 r = cg_create(controller, path);
439
440         if (r < 0)
441                 return r;
442
443         r = cg_set_task_access(controller, path, 0644, s->user->uid, s->user->gid);
444         if (r >= 0)
445                 r = cg_set_group_access(controller, path, 0755, s->user->uid, s->user->gid);
446
447         return r;
448 }
449
450 static int session_create_cgroup(Session *s) {
451         char **k;
452         char *p;
453         int r;
454
455         assert(s);
456         assert(s->user);
457         assert(s->user->cgroup_path);
458
459         if (!s->cgroup_path) {
460                 if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) {
461                         log_error("Out of memory");
462                         return -ENOMEM;
463                 }
464         } else
465                 p = s->cgroup_path;
466
467         r = session_create_one_group(s, SYSTEMD_CGROUP_CONTROLLER, p);
468         if (r < 0) {
469                 log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r));
470                 free(p);
471                 s->cgroup_path = NULL;
472                 return r;
473         }
474
475         s->cgroup_path = p;
476
477         STRV_FOREACH(k, s->controllers) {
478
479                 if (strv_contains(s->reset_controllers, *k))
480                         continue;
481
482                 r = session_create_one_group(s, *k, p);
483                 if (r < 0)
484                         log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
485         }
486
487         STRV_FOREACH(k, s->manager->controllers) {
488
489                 if (strv_contains(s->reset_controllers, *k) ||
490                     strv_contains(s->manager->reset_controllers, *k) ||
491                     strv_contains(s->controllers, *k))
492                         continue;
493
494                 r = session_create_one_group(s, *k, p);
495                 if (r < 0)
496                         log_warning("Failed to create %s:%s: %s", *k, p, strerror(-r));
497         }
498
499         if (s->leader > 0) {
500
501                 STRV_FOREACH(k, s->reset_controllers) {
502                         r = cg_attach(*k, "/", s->leader);
503                         if (r < 0)
504                                 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
505
506                 }
507
508                 STRV_FOREACH(k, s->manager->reset_controllers) {
509
510                         if (strv_contains(s->reset_controllers, *k) ||
511                             strv_contains(s->controllers, *k))
512                                 continue;
513
514                         r = cg_attach(*k, "/", s->leader);
515                         if (r < 0)
516                                 log_warning("Failed to reset controller %s: %s", *k, strerror(-r));
517
518                 }
519         }
520
521         hashmap_put(s->manager->cgroups, s->cgroup_path, s);
522
523         return 0;
524 }
525
526 int session_start(Session *s) {
527         int r;
528
529         assert(s);
530         assert(s->user);
531
532         if (s->started)
533                 return 0;
534
535         r = user_start(s->user);
536         if (r < 0)
537                 return r;
538
539         log_full(s->display || s->tty ? LOG_INFO : LOG_DEBUG,
540                  "New session %s of user %s.", s->id, s->user->name);
541
542         /* Create cgroup */
543         r = session_create_cgroup(s);
544         if (r < 0)
545                 return r;
546
547         /* Create X11 symlink */
548         session_link_x11_socket(s);
549
550         dual_timestamp_get(&s->timestamp);
551
552         if (s->seat)
553                 seat_read_active_vt(s->seat);
554
555         s->started = true;
556
557         /* Save session data */
558         session_save(s);
559         user_save(s->user);
560
561         session_send_signal(s, true);
562
563         if (s->seat) {
564                 seat_save(s->seat);
565
566                 if (s->seat->active == s)
567                         seat_send_changed(s->seat, "Sessions\0ActiveSession\0");
568                 else
569                         seat_send_changed(s->seat, "Sessions\0");
570         }
571
572         user_send_changed(s->user, "Sessions\0");
573
574         return 0;
575 }
576
577 static bool session_shall_kill(Session *s) {
578         assert(s);
579
580         if (!s->kill_processes)
581                 return false;
582
583         if (strv_contains(s->manager->kill_exclude_users, s->user->name))
584                 return false;
585
586         if (strv_isempty(s->manager->kill_only_users))
587                 return true;
588
589         return strv_contains(s->manager->kill_only_users, s->user->name);
590 }
591
592 static int session_terminate_cgroup(Session *s) {
593         int r;
594         char **k;
595
596         assert(s);
597
598         if (!s->cgroup_path)
599                 return 0;
600
601         cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
602
603         if (session_shall_kill(s)) {
604
605                 r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
606                 if (r < 0)
607                         log_error("Failed to kill session cgroup: %s", strerror(-r));
608
609         } else {
610                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true);
611                 if (r < 0)
612                         log_error("Failed to check session cgroup: %s", strerror(-r));
613                 else if (r > 0) {
614                         r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path);
615                         if (r < 0)
616                                 log_error("Failed to delete session cgroup: %s", strerror(-r));
617                 } else
618                         r = -EBUSY;
619         }
620
621         STRV_FOREACH(k, s->user->manager->controllers)
622                 cg_trim(*k, s->cgroup_path, true);
623
624         hashmap_remove(s->manager->cgroups, s->cgroup_path);
625
626         free(s->cgroup_path);
627         s->cgroup_path = NULL;
628
629         return r;
630 }
631
632 static int session_unlink_x11_socket(Session *s) {
633         char *t;
634         int r;
635
636         assert(s);
637         assert(s->user);
638
639         if (s->user->display != s)
640                 return 0;
641
642         s->user->display = NULL;
643
644         t = strappend(s->user->runtime_path, "/X11/display");
645         if (!t) {
646                 log_error("Out of memory");
647                 return -ENOMEM;
648         }
649
650         r = unlink(t);
651         free(t);
652
653         return r < 0 ? -errno : 0;
654 }
655
656 int session_stop(Session *s) {
657         int r = 0, k;
658
659         assert(s);
660
661         if (s->started)
662                 log_full(s->display || s->tty ? LOG_INFO : LOG_DEBUG,
663                          "Removed session %s.", s->id);
664
665         /* Kill cgroup */
666         k = session_terminate_cgroup(s);
667         if (k < 0)
668                 r = k;
669
670         /* Remove X11 symlink */
671         session_unlink_x11_socket(s);
672
673         unlink(s->state_file);
674         session_add_to_gc_queue(s);
675         user_add_to_gc_queue(s->user);
676
677         if (s->started)
678                 session_send_signal(s, false);
679
680         if (s->seat) {
681                 if (s->seat->active == s)
682                         seat_set_active(s->seat, NULL);
683
684                 seat_send_changed(s->seat, "Sessions\0");
685         }
686
687         user_send_changed(s->user, "Sessions\0");
688
689         s->started = false;
690
691         return r;
692 }
693
694 bool session_is_active(Session *s) {
695         assert(s);
696
697         if (!s->seat)
698                 return true;
699
700         return s->seat->active == s;
701 }
702
703 int session_get_idle_hint(Session *s, dual_timestamp *t) {
704         char *p;
705         struct stat st;
706         usec_t u, n;
707         bool b;
708         int k;
709
710         assert(s);
711
712         if (s->idle_hint) {
713                 if (t)
714                         *t = s->idle_hint_timestamp;
715
716                 return s->idle_hint;
717         }
718
719         if (isempty(s->tty))
720                 goto dont_know;
721
722         if (s->tty[0] != '/') {
723                 p = strappend("/dev/", s->tty);
724                 if (!p)
725                         return -ENOMEM;
726         } else
727                 p = NULL;
728
729         if (!startswith(p ? p : s->tty, "/dev/")) {
730                 free(p);
731                 goto dont_know;
732         }
733
734         k = lstat(p ? p : s->tty, &st);
735         free(p);
736
737         if (k < 0)
738                 goto dont_know;
739
740         u = timespec_load(&st.st_atim);
741         n = now(CLOCK_REALTIME);
742         b = u + IDLE_THRESHOLD_USEC < n;
743
744         if (t)
745                 dual_timestamp_from_realtime(t, u + b ? IDLE_THRESHOLD_USEC : 0);
746
747         return b;
748
749 dont_know:
750         if (t)
751                 *t = s->idle_hint_timestamp;
752
753         return 0;
754 }
755
756 void session_set_idle_hint(Session *s, bool b) {
757         assert(s);
758
759         if (s->idle_hint == b)
760                 return;
761
762         s->idle_hint = b;
763         dual_timestamp_get(&s->idle_hint_timestamp);
764
765         session_send_changed(s,
766                              "IdleHint\0"
767                              "IdleSinceHint\0"
768                              "IdleSinceHintMonotonic\0");
769
770         if (s->seat)
771                 seat_send_changed(s->seat,
772                                   "IdleHint\0"
773                                   "IdleSinceHint\0"
774                                   "IdleSinceHintMonotonic\0");
775
776         user_send_changed(s->user,
777                           "IdleHint\0"
778                           "IdleSinceHint\0"
779                           "IdleSinceHintMonotonic\0");
780
781         manager_send_changed(s->manager,
782                              "IdleHint\0"
783                              "IdleSinceHint\0"
784                              "IdleSinceHintMonotonic\0");
785 }
786
787 int session_create_fifo(Session *s) {
788         int r;
789
790         assert(s);
791
792         /* Create FIFO */
793         if (!s->fifo_path) {
794                 r = safe_mkdir("/run/systemd/sessions", 0755, 0, 0);
795                 if (r < 0)
796                         return r;
797
798                 if (asprintf(&s->fifo_path, "/run/systemd/sessions/%s.ref", s->id) < 0)
799                         return -ENOMEM;
800
801                 if (mkfifo(s->fifo_path, 0600) < 0 && errno != EEXIST)
802                         return -errno;
803         }
804
805         /* Open reading side */
806         if (s->fifo_fd < 0) {
807                 struct epoll_event ev;
808
809                 s->fifo_fd = open(s->fifo_path, O_RDONLY|O_CLOEXEC|O_NDELAY);
810                 if (s->fifo_fd < 0)
811                         return -errno;
812
813                 r = hashmap_put(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1), s);
814                 if (r < 0)
815                         return r;
816
817                 zero(ev);
818                 ev.events = 0;
819                 ev.data.u32 = FD_FIFO_BASE + s->fifo_fd;
820
821                 if (epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_ADD, s->fifo_fd, &ev) < 0)
822                         return -errno;
823         }
824
825         /* Open writing side */
826         r = open(s->fifo_path, O_WRONLY|O_CLOEXEC|O_NDELAY);
827         if (r < 0)
828                 return -errno;
829
830         return r;
831 }
832
833 void session_remove_fifo(Session *s) {
834         assert(s);
835
836         if (s->fifo_fd >= 0) {
837                 assert_se(hashmap_remove(s->manager->fifo_fds, INT_TO_PTR(s->fifo_fd + 1)) == s);
838                 assert_se(epoll_ctl(s->manager->epoll_fd, EPOLL_CTL_DEL, s->fifo_fd, NULL) == 0);
839                 close_nointr_nofail(s->fifo_fd);
840                 s->fifo_fd = -1;
841         }
842
843         if (s->fifo_path) {
844                 unlink(s->fifo_path);
845                 free(s->fifo_path);
846                 s->fifo_path = NULL;
847         }
848 }
849
850 int session_check_gc(Session *s, bool drop_not_started) {
851         int r;
852
853         assert(s);
854
855         if (drop_not_started && !s->started)
856                 return 0;
857
858         if (s->fifo_fd >= 0) {
859
860                 r = pipe_eof(s->fifo_fd);
861                 if (r < 0)
862                         return r;
863
864                 if (r == 0)
865                         return 1;
866         }
867
868         if (s->cgroup_path) {
869
870                 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false);
871                 if (r < 0)
872                         return r;
873
874                 if (r <= 0)
875                         return 1;
876         }
877
878         return 0;
879 }
880
881 void session_add_to_gc_queue(Session *s) {
882         assert(s);
883
884         if (s->in_gc_queue)
885                 return;
886
887         LIST_PREPEND(Session, gc_queue, s->manager->session_gc_queue, s);
888         s->in_gc_queue = true;
889 }
890
891 int session_kill(Session *s, KillWho who, int signo) {
892         int r = 0;
893         Set *pid_set = NULL;
894
895         assert(s);
896
897         if (!s->cgroup_path)
898                 return -ESRCH;
899
900         if (s->leader <= 0 && who == KILL_LEADER)
901                 return -ESRCH;
902
903         if (s->leader > 0)
904                 if (kill(s->leader, signo) < 0)
905                         r = -errno;
906
907         if (who == KILL_ALL) {
908                 int q;
909
910                 pid_set = set_new(trivial_hash_func, trivial_compare_func);
911                 if (!pid_set)
912                         return -ENOMEM;
913
914                 if (s->leader > 0) {
915                         q = set_put(pid_set, LONG_TO_PTR(s->leader));
916                         if (q < 0)
917                                 r = q;
918                 }
919
920                 q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, signo, false, true, false, pid_set);
921                 if (q < 0)
922                         if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
923                                 r = q;
924         }
925
926         if (pid_set)
927                 set_free(pid_set);
928
929         return r;
930 }
931
932 static const char* const session_type_table[_SESSION_TYPE_MAX] = {
933         [SESSION_TTY] = "tty",
934         [SESSION_X11] = "x11",
935         [SESSION_UNSPECIFIED] = "unspecified"
936 };
937
938 DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType);
939
940 static const char* const kill_who_table[_KILL_WHO_MAX] = {
941         [KILL_LEADER] = "leader",
942         [KILL_ALL] = "all"
943 };
944
945 DEFINE_STRING_TABLE_LOOKUP(kill_who, KillWho);