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