]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utmp.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/inotify.h>
42 lxc_log_define(lxc_utmp
, lxc
);
44 static int utmp_handler(int fd
, void *data
, struct lxc_epoll_descr
*descr
)
46 struct inotify_event ie
;
48 struct lxc_handler
*handler
= (struct lxc_handler
*)data
;
49 struct lxc_conf
*conf
= handler
->conf
;
50 char prevrun_level
= 'N', currun_level
= 'N';
52 char path
[MAXPATHLEN
];
54 if (read(fd
, &ie
, sizeof(ie
)) < 0) {
55 SYSERROR("failed to read utmp notification");
59 if (snprintf(path
, MAXPATHLEN
, "%s/var/run/utmp", conf
->rootfs
) >
61 ERROR("path is too long");
65 if (utmpxname(path
)) {
66 SYSERROR("failed to 'utmpxname'");
72 while ((utmpx
= getutxent())) {
74 if (utmpx
->ut_type
== RUN_LVL
) {
75 prevrun_level
= utmpx
->ut_pid
/ 256;
76 currun_level
= utmpx
->ut_pid
% 256;
80 ntasks
= lxc_cgroup_nrtasks(handler
->name
);
82 ERROR("failed to get the number of tasks");
86 if (ntasks
== 1 && prevrun_level
== '3') {
88 DEBUG("run level is %c/%c", prevrun_level
, currun_level
);
89 DEBUG("there is %d tasks remaining", ntasks
);
91 if (currun_level
== '0') {
92 INFO("container has shutdown");
93 kill(handler
->pid
, SIGKILL
);
96 if (currun_level
== '6') {
97 INFO("container has rebooted");
99 kill(handler
->pid
, SIGKILL
);
110 int lxc_utmp_mainloop_add(struct lxc_epoll_descr
*descr
,
111 struct lxc_handler
*handler
)
113 struct lxc_conf
*conf
= handler
->conf
;
114 char path
[MAXPATHLEN
];
120 if (snprintf(path
, MAXPATHLEN
, "%s/var/run/utmp", conf
->rootfs
) >
122 ERROR("path is too long");
126 if (access(path
, F_OK
)) {
127 WARN("'%s' not found", path
);
133 SYSERROR("failed to inotify_init");
137 if (fcntl(fd
, F_SETFD
, FD_CLOEXEC
)) {
138 SYSERROR("failed to set inotify fd to close-on-exec");
143 wd
= inotify_add_watch(fd
, path
, IN_MODIFY
);
145 SYSERROR("failed to add watch for '%s'", path
);
150 if (lxc_mainloop_add_handler(descr
, fd
, utmp_handler
, handler
)) {
151 SYSERROR("failed to add mainloop");