]>
git.proxmox.com Git - mirror_lxcfs.git/blob - pam/pam_cgfs.c
3 * Copyright © 2016 Canonical, Inc
4 * Author: Serge Hallyn <serge.hallyn@ubuntu.com>
5 * Author: Christian Brauner <christian.brauner@ubuntu.com>
7 * When a user logs in, this pam module will create cgroups which the user may
8 * administer. It handles both pure cgroupfs v1 and pure cgroupfs v2, as well as
9 * mixed mounts, where some controllers are mounted in a standard cgroupfs v1
10 * hierarchy location (/sys/fs/cgroup/<controller>) and others are in the
11 * cgroupfs v2 hierarchy.
12 * Writeable cgroups are either created for all controllers or, if specified,
13 * for any controllers listed on the command line.
14 * The cgroup created will be "user/$user/0" for the first session,
15 * "user/$user/1" for the second, etc.
17 * Systems with a systemd init system are treated specially, both with respect
18 * to cgroupfs v1 and cgroupfs v2. For both, cgroupfs v1 and cgroupfs v2, We
19 * check whether systemd already placed us in a cgroup it created:
21 * user.slice/user-uid.slice/session-n.scope
23 * by checking whether uid == our uid. If it did, we simply chown the last
24 * part (session-n.scope). If it did not we create a cgroup as outlined above
25 * (user/$user/n) and chown it to our uid.
26 * The same holds for cgroupfs v2 where this assumptions becomes crucial:
27 * We __have to__ be placed in our under the cgroup systemd created for us on
28 * login, otherwise things like starting an xserver or similar will not work.
30 * All requested cgroups must be mounted under /sys/fs/cgroup/$controller,
31 * no messing around with finding mountpoints.
33 * See COPYING file for details.
49 #include <linux/unistd.h>
50 #include <sys/mount.h>
51 #include <sys/param.h>
53 #include <sys/types.h>
56 #define PAM_SM_SESSION
57 #include <security/_pam_macros.h>
58 #include <security/pam_modules.h>
62 #ifndef CGROUP_SUPER_MAGIC
63 #define CGROUP_SUPER_MAGIC 0x27e0eb
66 #ifndef CGROUP2_SUPER_MAGIC
67 #define CGROUP2_SUPER_MAGIC 0x63677270
70 /* Taken over modified from the kernel sources. */
71 #define NBITS 32 /* bits in uint32_t */
72 #define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
73 #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBITS)
75 static enum cg_mount_mode
{
80 CGROUP_UNINITIALIZED
= 3,
81 } cg_mount_mode
= CGROUP_UNINITIALIZED
;
83 /* Common helper functions. Most of these have been taken from LXC. */
84 static void append_line(char **dest
, size_t oldlen
, char *new, size_t newlen
);
85 static int append_null_to_list(void ***list
);
86 static void batch_realloc(char **mem
, size_t oldlen
, size_t newlen
);
87 static inline void clear_bit(unsigned bit
, uint32_t *bitarr
)
89 bitarr
[bit
/ NBITS
] &= ~(1 << (bit
% NBITS
));
91 static char *copy_to_eol(char *s
);
92 static bool file_exists(const char *f
);
93 static void free_string_list(char **list
);
94 static char *get_mountpoint(char *line
);
95 static bool get_uid_gid(const char *user
, uid_t
*uid
, gid_t
*gid
);
96 static int handle_login(const char *user
, uid_t uid
, gid_t gid
);
97 static inline bool is_set(unsigned bit
, uint32_t *bitarr
)
99 return (bitarr
[bit
/ NBITS
] & (1 << (bit
% NBITS
))) != 0;
101 /* __typeof__ should be safe to use with all compilers. */
102 typedef __typeof__(((struct statfs
*)NULL
)->f_type
) fs_type_magic
;
103 static bool has_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
);
104 static bool is_lxcfs(const char *line
);
105 static bool is_cgv1(char *line
);
106 static bool is_cgv2(char *line
);
107 static bool mkdir_p(const char *root
, char *path
);
108 static void *must_alloc(size_t sz
);
109 static void must_add_to_list(char ***clist
, char *entry
);
110 static void must_append_controller(char **klist
, char **nlist
, char ***clist
,
112 static void must_append_string(char ***list
, char *entry
);
113 static char *must_copy_string(const char *entry
);
114 static char *must_make_path(const char *first
, ...) __attribute__((sentinel
));
115 static void *must_realloc(void *orig
, size_t sz
);
116 static void mysyslog(int err
, const char *format
, ...) __attribute__((sentinel
));
117 static char *read_file(char *fnam
);
118 static int read_from_file(const char *filename
, void* buf
, size_t count
);
119 static int recursive_rmdir(char *dirname
);
120 static inline void set_bit(unsigned bit
, uint32_t *bitarr
)
122 bitarr
[bit
/ NBITS
] |= (1 << (bit
% NBITS
));
124 static bool string_in_list(char **list
, const char *entry
);
125 static char *string_join(const char *sep
, const char **parts
, bool use_as_prefix
);
126 static void trim(char *s
);
127 static bool write_int(char *path
, int v
);
128 static ssize_t
write_nointr(int fd
, const void* buf
, size_t count
);
129 static int write_to_file(const char *filename
, const void *buf
, size_t count
,
132 /* cgroupfs prototypes. */
133 static bool cg_belongs_to_uid_gid(const char *path
, uid_t uid
, gid_t gid
);
134 static uint32_t *cg_cpumask(char *buf
, size_t nbits
);
135 static bool cg_copy_parent_file(char *path
, char *file
);
136 static char *cg_cpumask_to_cpulist(uint32_t *bitarr
, size_t nbits
);
137 static bool cg_enter(const char *cgroup
);
138 static void cg_escape(void);
139 static bool cg_filter_and_set_cpus(char *path
, bool am_initialized
);
140 static ssize_t
cg_get_max_cpus(char *cpulist
);
141 static int cg_get_version_of_mntpt(const char *path
);
142 static bool cg_init(uid_t uid
, gid_t gid
);
143 static void cg_mark_to_make_rw(char **list
);
144 static void cg_prune_empty_cgroups(const char *user
);
145 static bool cg_systemd_created_user_slice(const char *base_cgroup
,
146 const char *init_cgroup
,
147 const char *in
, uid_t uid
);
148 static bool cg_systemd_chown_existing_cgroup(const char *mountpoint
,
149 const char *base_cgroup
, uid_t uid
,
151 bool systemd_user_slice
);
152 static bool cg_systemd_under_user_slice_1(const char *in
, uid_t uid
);
153 static bool cg_systemd_under_user_slice_2(const char *base_cgroup
,
154 const char *init_cgroup
, uid_t uid
);
155 static void cg_systemd_prune_init_scope(char *cg
);
156 static bool is_lxcfs(const char *line
);
158 /* cgroupfs v1 prototypes. */
159 struct cgv1_hierarchy
{
165 bool create_rw_cgroup
;
166 bool systemd_user_slice
;
169 static struct cgv1_hierarchy
**cgv1_hierarchies
;
171 static void cgv1_add_controller(char **clist
, char *mountpoint
,
172 char *base_cgroup
, char *init_cgroup
);
173 static bool cgv1_controller_in_clist(char *cgline
, char *c
);
174 static bool cgv1_controller_lists_intersect(char **l1
, char **l2
);
175 static bool cgv1_controller_list_is_dup(struct cgv1_hierarchy
**hlist
,
177 static bool cgv1_create(const char *cgroup
, uid_t uid
, gid_t gid
,
179 static bool cgv1_create_one(struct cgv1_hierarchy
*h
, const char *cgroup
,
180 uid_t uid
, gid_t gid
, bool *existed
);
181 static bool cgv1_enter(const char *cgroup
);
182 static void cgv1_escape(void);
183 static bool cgv1_get_controllers(char ***klist
, char ***nlist
);
184 static char *cgv1_get_current_cgroup(char *basecginfo
, char *controller
);
185 static char **cgv1_get_proc_mountinfo_controllers(char **klist
, char **nlist
,
187 static bool cgv1_handle_cpuset_hierarchy(struct cgv1_hierarchy
*h
,
189 static bool cgv1_handle_root_cpuset_hierarchy(struct cgv1_hierarchy
*h
);
190 static bool cgv1_init(uid_t uid
, gid_t gid
);
191 static void cgv1_mark_to_make_rw(char **clist
);
192 static char *cgv1_must_prefix_named(char *entry
);
193 static bool cgv1_prune_empty_cgroups(const char *user
);
194 static bool cgv1_remove_one(struct cgv1_hierarchy
*h
, const char *cgroup
);
195 static bool is_cgv1(char *line
);
197 /* cgroupfs v2 prototypes. */
198 struct cgv2_hierarchy
{
204 bool create_rw_cgroup
;
205 bool systemd_user_slice
;
208 /* Actually this should only be a single hierarchy. But for the sake of
209 * parallelism and because the layout of the cgroupfs v2 is still somewhat
210 * changing, we'll leave it as an array of structs.
212 static struct cgv2_hierarchy
**cgv2_hierarchies
;
214 static void cgv2_add_controller(char **clist
, char *mountpoint
,
215 char *base_cgroup
, char *init_cgroup
,
216 bool systemd_user_slice
);
217 static bool cgv2_create(const char *cgroup
, uid_t uid
, gid_t gid
,
219 static bool cgv2_enter(const char *cgroup
);
220 static void cgv2_escape(void);
221 static char *cgv2_get_current_cgroup(int pid
);
222 static bool cgv2_init(uid_t uid
, gid_t gid
);
223 static void cgv2_mark_to_make_rw(char **clist
);
224 static bool cgv2_prune_empty_cgroups(const char *user
);
225 static bool cgv2_remove(const char *cgroup
);
226 static bool is_cgv2(char *line
);
228 /* Common helper functions. Most of these have been taken from LXC. */
229 static void mysyslog(int err
, const char *format
, ...)
233 va_start(args
, format
);
234 openlog("PAM-CGFS", LOG_CONS
| LOG_PID
, LOG_AUTH
);
235 vsyslog(err
, format
, args
);
240 /* realloc() pointer; do not fail. */
241 static void *must_realloc(void *orig
, size_t sz
)
246 ret
= realloc(orig
, sz
);
252 /* realloc() pointer in batch sizes; do not fail. */
253 #define BATCH_SIZE 50
254 static void batch_realloc(char **mem
, size_t oldlen
, size_t newlen
)
256 int newbatches
= (newlen
/ BATCH_SIZE
) + 1;
257 int oldbatches
= (oldlen
/ BATCH_SIZE
) + 1;
259 if (!*mem
|| newbatches
> oldbatches
)
260 *mem
= must_realloc(*mem
, newbatches
* BATCH_SIZE
);
263 /* Append lines as is to pointer; do not fail. */
264 static void append_line(char **dest
, size_t oldlen
, char *new, size_t newlen
)
266 size_t full
= oldlen
+ newlen
;
268 batch_realloc(dest
, oldlen
, full
+ 1);
270 memcpy(*dest
+ oldlen
, new, newlen
+ 1);
273 /* Read in whole file and return allocated pointer. */
274 static char *read_file(char *fnam
)
278 char *line
= NULL
, *buf
= NULL
;
279 size_t len
= 0, fulllen
= 0;
281 f
= fopen(fnam
, "r");
285 while ((linelen
= getline(&line
, &len
, f
)) != -1) {
286 append_line(&buf
, fulllen
, line
, linelen
);
296 /* Given a pointer to a null-terminated array of pointers, realloc to add one
297 * entry, and point the new entry to NULL. Do not fail. Return the index to the
298 * second-to-last entry - that is, the one which is now available for use
299 * (keeping the list null-terminated).
301 static int append_null_to_list(void ***list
)
306 for (; (*list
)[newentry
]; newentry
++) {
310 *list
= must_realloc(*list
, (newentry
+ 2) * sizeof(void **));
311 (*list
)[newentry
+ 1] = NULL
;
316 /* Make allocated copy of string; do not fail. */
317 static char *must_copy_string(const char *entry
)
331 /* Append new entry to null-terminated array of pointer; make sure that array of
332 * pointers will still be null-terminated.
334 static void must_append_string(char ***list
, char *entry
)
339 newentry
= append_null_to_list((void ***)list
);
340 copy
= must_copy_string(entry
);
341 (*list
)[newentry
] = copy
;
344 /* Remove newlines from string. */
345 static void trim(char *s
)
347 size_t len
= strlen(s
);
349 while ((len
> 0) && s
[len
- 1] == '\n')
353 /* Allocate pointer; do not fail. */
354 static void *must_alloc(size_t sz
)
356 return must_realloc(NULL
, sz
);
359 /* Make allocated copy of string. End of string is taken to be '\n'. */
360 static char *copy_to_eol(char *s
)
362 char *newline
, *sret
;
365 newline
= strchr(s
, '\n');
370 sret
= must_alloc(len
+ 1);
371 memcpy(sret
, s
, len
);
377 /* Check if given entry under /proc/<pid>/mountinfo is a fuse.lxcfs mount. */
378 static bool is_lxcfs(const char *line
)
380 char *p
= strstr(line
, " - ");
384 return strncmp(p
, " - fuse.lxcfs ", 14) == 0;
387 /* Check if given entry under /proc/<pid>/mountinfo is a cgroupfs v1 mount. */
388 static bool is_cgv1(char *line
)
390 char *p
= strstr(line
, " - ");
394 return strncmp(p
, " - cgroup ", 10) == 0;
397 /* Check if given entry under /proc/<pid>/mountinfo is a cgroupfs v2 mount. */
398 static bool is_cgv2(char *line
)
400 char *p
= strstr(line
, " - ");
404 return strncmp(p
, " - cgroup2 ", 11) == 0;
407 /* Given a null-terminated array of strings, check whether @entry is one of the
410 static bool string_in_list(char **list
, const char *entry
)
414 for (it
= list
; it
&& *it
; it
++)
415 if (strcmp(*it
, entry
) == 0)
422 * Creates a null-terminated array of strings, made by splitting the entries in
423 * @str on each @sep. Caller is responsible for calling free_string_list.
425 static char **make_string_list(const char *str
, const char *sep
)
428 char *saveptr
= NULL
;
431 copy
= must_copy_string(str
);
433 for (tok
= strtok_r(copy
, sep
, &saveptr
); tok
;
434 tok
= strtok_r(NULL
, sep
, &saveptr
))
435 must_add_to_list(&clist
, tok
);
442 /* Gets the length of a null-terminated array of strings. */
443 static size_t string_list_length(char **list
)
448 for (it
= list
; it
&& *it
; it
++)
454 /* Free null-terminated array of strings. */
455 static void free_string_list(char **list
)
459 for (it
= list
; it
&& *it
; it
++)
464 /* Concatenate all passed-in strings into one path. Do not fail. If any piece
465 * is not prefixed with '/', add a '/'. Does not remove duplicate '///' from the
468 static char *must_make_path(const char *first
, ...)
474 full_len
= strlen(first
);
476 dest
= must_copy_string(first
);
478 va_start(args
, first
);
479 while ((cur
= va_arg(args
, char *)) != NULL
) {
480 full_len
+= strlen(cur
);
485 dest
= must_realloc(dest
, full_len
+ 1);
497 /* Write single integer to file. */
498 static bool write_int(char *path
, int v
)
503 f
= fopen(path
, "w");
507 if (fprintf(f
, "%d\n", v
) < 0)
516 /* Check if a given file exists. */
517 static bool file_exists(const char *f
)
521 return stat(f
, &statbuf
) == 0;
524 /* Create directory and (if necessary) its parents. */
525 static bool mkdir_p(const char *root
, char *path
)
529 if (strlen(path
) < strlen(root
))
532 if (strlen(path
) == strlen(root
))
535 b
= path
+ strlen(root
) + 1;
537 while (*b
&& (*b
== '/'))
543 while (*e
&& *e
!= '/')
550 if (file_exists(path
))
553 if (mkdir(path
, 0755) < 0) {
554 lxcfs_debug("Failed to create %s: %s.\n", path
, strerror(errno
));
569 /* Recursively remove directory and its parents. */
570 static int recursive_rmdir(char *dirname
)
572 struct dirent
*direntp
;
576 dir
= opendir(dirname
);
580 while ((direntp
= readdir(dir
))) {
587 if (!strcmp(direntp
->d_name
, ".") ||
588 !strcmp(direntp
->d_name
, ".."))
591 pathname
= must_make_path(dirname
, direntp
->d_name
, NULL
);
593 if (lstat(pathname
, &st
)) {
595 lxcfs_debug("Failed to stat %s.\n", pathname
);
600 if (!S_ISDIR(st
.st_mode
))
603 if (recursive_rmdir(pathname
) < 0)
609 if (rmdir(dirname
) < 0) {
611 lxcfs_debug("Failed to delete %s: %s.\n", dirname
, strerror(errno
));
615 if (closedir(dir
) < 0) {
617 lxcfs_debug("Failed to delete %s: %s.\n", dirname
, strerror(errno
));
624 /* Add new entry to null-terminated array of pointers. Make sure array is still
627 static void must_add_to_list(char ***clist
, char *entry
)
631 newentry
= append_null_to_list((void ***)clist
);
632 (*clist
)[newentry
] = must_copy_string(entry
);
635 /* Get mountpoint from a /proc/<pid>/mountinfo line. */
636 static char *get_mountpoint(char *line
)
644 for (i
= 0; i
< 4; i
++) {
656 sret
= must_alloc(len
+ 1);
657 memcpy(sret
, p
, len
);
663 /* Create list of cgroupfs v1 controller found under /proc/self/cgroup. Skips
664 * the 0::/some/path cgroupfs v2 hierarchy listed. Splits controllers into
665 * kernel controllers (@klist) and named controllers (@nlist).
667 static bool cgv1_get_controllers(char ***klist
, char ***nlist
)
673 f
= fopen("/proc/self/cgroup", "r");
677 while (getline(&line
, &len
, f
) != -1) {
679 char *saveptr
= NULL
;
681 p
= strchr(line
, ':');
691 /* Skip the v2 hierarchy. */
695 for (tok
= strtok_r(p
, ",", &saveptr
); tok
;
696 tok
= strtok_r(NULL
, ",", &saveptr
)) {
697 if (strncmp(tok
, "name=", 5) == 0)
698 must_append_string(nlist
, tok
);
700 must_append_string(klist
, tok
);
710 /* Get list of controllers for cgroupfs v2 hierarchy by looking at
711 * cgroup.controllers and/or cgroup.subtree_control of a given (parent) cgroup.
712 static bool cgv2_get_controllers(char ***klist)
718 /* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */
719 static char *cgv2_get_current_cgroup(int pid
)
723 char *current_cgroup
;
725 /* The largest integer that can fit into long int is 2^64. This is a
726 * 20-digit number. */
727 #define __PIDLEN /* /proc */ 5 + /* /pid-to-str */ 21 + /* /cgroup */ 7 + /* \0 */ 1
730 ret
= snprintf(path
, __PIDLEN
, "/proc/%d/cgroup", pid
);
731 if (ret
< 0 || ret
>= __PIDLEN
)
734 cgroups_v2
= read_file(path
);
738 current_cgroup
= strstr(cgroups_v2
, "0::/");
742 current_cgroup
= current_cgroup
+ 3;
743 copy
= copy_to_eol(current_cgroup
);
755 /* Given two null-terminated lists of strings, return true if any string is in
758 static bool cgv1_controller_lists_intersect(char **l1
, char **l2
)
765 for (it
= l1
; it
&& *it
; it
++)
766 if (string_in_list(l2
, *it
))
772 /* For a null-terminated list of controllers @clist, return true if any of those
773 * controllers is already listed the null-terminated list of hierarchies @hlist.
774 * Realistically, if one is present, all must be present.
776 static bool cgv1_controller_list_is_dup(struct cgv1_hierarchy
**hlist
, char **clist
)
778 struct cgv1_hierarchy
**it
;
780 for (it
= hlist
; it
&& *it
; it
++)
781 if ((*it
)->controllers
)
782 if (cgv1_controller_lists_intersect((*it
)->controllers
, clist
))
788 /* Set boolean to mark controllers under which we are supposed create a
791 static void cgv1_mark_to_make_rw(char **clist
)
793 struct cgv1_hierarchy
**it
;
795 for (it
= cgv1_hierarchies
; it
&& *it
; it
++)
796 if ((*it
)->controllers
)
797 if (cgv1_controller_lists_intersect((*it
)->controllers
, clist
) ||
798 string_in_list(clist
, "all"))
799 (*it
)->create_rw_cgroup
= true;
802 /* Set boolean to mark whether we are supposed to create a writeable cgroup in
803 * the cgroupfs v2 hierarchy.
805 static void cgv2_mark_to_make_rw(char **clist
)
807 if (string_in_list(clist
, "unified") || string_in_list(clist
, "all"))
808 if (cgv2_hierarchies
)
809 (*cgv2_hierarchies
)->create_rw_cgroup
= true;
812 /* Wrapper around cgv{1,2}_mark_to_make_rw(). */
813 static void cg_mark_to_make_rw(char **clist
)
815 cgv1_mark_to_make_rw(clist
);
816 cgv2_mark_to_make_rw(clist
);
819 /* Prefix any named controllers with "name=", e.g. "name=systemd". */
820 static char *cgv1_must_prefix_named(char *entry
)
827 s
= must_alloc(len
+ 6);
829 ret
= snprintf(s
, len
+ 6, "name=%s", entry
);
830 if (ret
< 0 || (size_t)ret
>= (len
+ 6))
836 /* Append kernel controller in @klist or named controller in @nlist to @clist */
837 static void must_append_controller(char **klist
, char **nlist
, char ***clist
, char *entry
)
842 if (string_in_list(klist
, entry
) && string_in_list(nlist
, entry
))
845 newentry
= append_null_to_list((void ***)clist
);
847 if (strncmp(entry
, "name=", 5) == 0)
848 copy
= must_copy_string(entry
);
849 else if (string_in_list(klist
, entry
))
850 copy
= must_copy_string(entry
);
852 copy
= cgv1_must_prefix_named(entry
);
854 (*clist
)[newentry
] = copy
;
857 /* Get the controllers from a mountinfo line. There are other ways we could get
858 * this info. For lxcfs, field 3 is /cgroup/controller-list. For cgroupfs, we
859 * could parse the mount options. But we simply assume that the mountpoint must
860 * be /sys/fs/cgroup/controller-list
862 static char **cgv1_get_proc_mountinfo_controllers(char **klist
, char **nlist
, char *line
)
866 char *saveptr
= NULL
;
871 for (i
= 0; i
< 4; i
++) {
880 if (strncmp(p
, "/sys/fs/cgroup/", 15) != 0)
890 for (tok
= strtok_r(p
, ",", &saveptr
); tok
;
891 tok
= strtok_r(NULL
, ",", &saveptr
))
892 must_append_controller(klist
, nlist
, &aret
, tok
);
897 /* Check if a cgroupfs v2 controller is present in the string @cgline. */
898 static bool cgv1_controller_in_clist(char *cgline
, char *c
)
901 char *tok
, *eol
, *tmp
;
902 char *saveptr
= NULL
;
904 eol
= strchr(cgline
, ':');
909 tmp
= alloca(len
+ 1);
910 memcpy(tmp
, cgline
, len
);
913 for (tok
= strtok_r(tmp
, ",", &saveptr
); tok
;
914 tok
= strtok_r(NULL
, ",", &saveptr
)) {
915 if (strcmp(tok
, c
) == 0)
921 /* Get current cgroup from the /proc/<pid>/cgroup file passed in via @basecginfo
922 * of a given cgv1 controller passed in via @controller.
924 static char *cgv1_get_current_cgroup(char *basecginfo
, char *controller
)
936 if (cgv1_controller_in_clist(p
, controller
)) {
942 return copy_to_eol(p
);
954 /* Remove /init.scope from string @cg. This will mostly affect systemd-based
957 #define INIT_SCOPE "/init.scope"
958 static void cg_systemd_prune_init_scope(char *cg
)
965 point
= cg
+ strlen(cg
) - strlen(INIT_SCOPE
);
969 if (strcmp(point
, INIT_SCOPE
) == 0) {
977 /* Add new info about a mounted cgroupfs v1 hierarchy. Includes the controllers
978 * mounted into that hierarchy (e.g. cpu,cpuacct), the mountpoint of that
979 * hierarchy (/sys/fs/cgroup/<controller>, the base cgroup of the current
980 * process gathered from /proc/self/cgroup, and the init cgroup of PID1 gathered
981 * from /proc/1/cgroup.
983 static void cgv1_add_controller(char **clist
, char *mountpoint
, char *base_cgroup
, char *init_cgroup
)
985 struct cgv1_hierarchy
*new;
988 new = must_alloc(sizeof(*new));
989 new->controllers
= clist
;
990 new->mountpoint
= mountpoint
;
991 new->base_cgroup
= base_cgroup
;
992 new->fullcgpath
= NULL
;
993 new->create_rw_cgroup
= false;
994 new->init_cgroup
= init_cgroup
;
995 new->systemd_user_slice
= false;
997 newentry
= append_null_to_list((void ***)&cgv1_hierarchies
);
998 cgv1_hierarchies
[newentry
] = new;
1001 /* Add new info about the mounted cgroupfs v2 hierarchy. Can (but doesn't
1002 * currently) include the controllers mounted into the hierarchy (e.g. memory,
1003 * pids, blkio), the mountpoint of that hierarchy (Should usually be
1004 * /sys/fs/cgroup but some init systems seems to think it might be a good idea
1005 * to also mount empty cgroupfs v2 hierarchies at /sys/fs/cgroup/systemd.), the
1006 * base cgroup of the current process gathered from /proc/self/cgroup, and the
1007 * init cgroup of PID1 gathered from /proc/1/cgroup.
1009 static void cgv2_add_controller(char **clist
, char *mountpoint
, char *base_cgroup
, char *init_cgroup
, bool systemd_user_slice
)
1011 struct cgv2_hierarchy
*new;
1014 new = must_alloc(sizeof(*new));
1015 new->controllers
= clist
;
1016 new->mountpoint
= mountpoint
;
1017 new->base_cgroup
= base_cgroup
;
1018 new->fullcgpath
= NULL
;
1019 new->create_rw_cgroup
= false;
1020 new->init_cgroup
= init_cgroup
;
1021 new->systemd_user_slice
= systemd_user_slice
;
1023 newentry
= append_null_to_list((void ***)&cgv2_hierarchies
);
1024 cgv2_hierarchies
[newentry
] = new;
1027 /* In Ubuntu 14.04, the paths created for us were
1028 * '/user/$uid.user/$something.session' This can be merged better with
1029 * systemd_created_slice_for_us(), but keeping it separate makes it easier to
1030 * reason about the correctness.
1032 static bool cg_systemd_under_user_slice_1(const char *in
, uid_t uid
)
1040 copy
= must_copy_string(in
);
1041 if (strlen(copy
) < strlen("/user/1.user/1.session"))
1043 p
= copy
+ strlen(copy
) - 1;
1045 /* skip any trailing '/' (shouldn't be any, but be sure) */
1046 while (p
>= copy
&& *p
== '/')
1051 /* Get last path element */
1052 while (p
>= copy
&& *p
!= '/')
1056 /* make sure it is something.session */
1057 len
= strlen(p
+ 1);
1058 if (len
< strlen("1.session") ||
1059 strncmp(p
+ 1 + len
- 8, ".session", 8) != 0)
1062 /* ok last path piece checks out, now check the second to last */
1064 while (p
>= copy
&& *(--p
) != '/')
1066 if (sscanf(p
+ 1, "%d.user/", &id
) != 1)
1079 /* So long as our path relative to init starts with /user.slice/user-$uid.slice,
1080 * assume it belongs to $uid and chown it
1082 static bool cg_systemd_under_user_slice_2(const char *base_cgroup
,
1083 const char *init_cgroup
, uid_t uid
)
1087 size_t curlen
, initlen
;
1089 curlen
= strlen(base_cgroup
);
1090 initlen
= strlen(init_cgroup
);
1091 if (curlen
<= initlen
)
1094 if (strncmp(base_cgroup
, init_cgroup
, initlen
) != 0)
1097 ret
= snprintf(buf
, 100, "/user.slice/user-%d.slice/", (int)uid
);
1098 if (ret
< 0 || ret
>= 100)
1102 initlen
= 0; // skip the '/'
1104 return strncmp(base_cgroup
+ initlen
, buf
, strlen(buf
)) == 0;
1107 /* The systemd-created path is: user-$uid.slice/session-c$session.scope. If that
1108 * is not the end of our systemd path, then we're not part of the PAM call that
1109 * created that path.
1111 * The last piece is chowned to $uid, the user- part not.
1112 * Note: If the user creates paths that look like what we're looking for to
1114 * - they fool us, we create new cgroups, and they get auto-logged-out.
1115 * - they fool a root sudo, systemd cgroup is not changed but chowned, and they
1116 * lose ownership of their cgroups
1118 static bool cg_systemd_created_user_slice(const char *base_cgroup
,
1119 const char *init_cgroup
,
1120 const char *in
, uid_t uid
)
1128 copy
= must_copy_string(in
);
1130 /* An old version of systemd has already created a cgroup for us. */
1131 if (cg_systemd_under_user_slice_1(in
, uid
))
1134 /* A new version of systemd has already created a cgroup for us. */
1135 if (cg_systemd_under_user_slice_2(base_cgroup
, init_cgroup
, uid
))
1138 if (strlen(copy
) < strlen("/user-0.slice/session-0.scope"))
1141 p
= copy
+ strlen(copy
) - 1;
1142 /* Skip any trailing '/' (shouldn't be any, but be sure). */
1143 while (p
>= copy
&& *p
== '/')
1149 /* Get last path element */
1150 while (p
>= copy
&& *p
!= '/')
1156 /* Make sure it is session-something.scope. */
1157 len
= strlen(p
+ 1);
1158 if (strncmp(p
+ 1, "session-", strlen("session-")) != 0 ||
1159 strncmp(p
+ 1 + len
- 6, ".scope", 6) != 0)
1162 /* Ok last path piece checks out, now check the second to last. */
1164 while (p
>= copy
&& *(--p
) != '/')
1167 if (sscanf(p
+ 1, "user-%d.slice/", &id
) != 1)
1180 /* Chown existing cgroup that systemd has already created for us. */
1181 static bool cg_systemd_chown_existing_cgroup(const char *mountpoint
,
1182 const char *base_cgroup
, uid_t uid
,
1183 gid_t gid
, bool systemd_user_slice
)
1187 if (!systemd_user_slice
)
1190 path
= must_make_path(mountpoint
, base_cgroup
, NULL
);
1192 /* A cgroup within name=systemd has already been created. So we only
1195 if (chown(path
, uid
, gid
) < 0)
1196 mysyslog(LOG_WARNING
, "Failed to chown %s to %d:%d: %s.\n",
1197 path
, (int)uid
, (int)gid
, strerror(errno
), NULL
);
1198 lxcfs_debug("Chowned %s to %d:%d.\n", path
, (int)uid
, (int)gid
);
1204 /* Detect and store information about cgroupfs v1 hierarchies. */
1205 static bool cgv1_init(uid_t uid
, gid_t gid
)
1208 struct cgv1_hierarchy
**it
;
1211 char **klist
= NULL
, **nlist
= NULL
;
1214 basecginfo
= read_file("/proc/self/cgroup");
1218 f
= fopen("/proc/self/mountinfo", "r");
1224 cgv1_get_controllers(&klist
, &nlist
);
1226 while (getline(&line
, &len
, f
) != -1) {
1227 char **controller_list
= NULL
;
1228 char *mountpoint
, *base_cgroup
;
1230 if (is_lxcfs(line
) || !is_cgv1(line
))
1233 controller_list
= cgv1_get_proc_mountinfo_controllers(klist
, nlist
, line
);
1234 if (!controller_list
)
1237 if (cgv1_controller_list_is_dup(cgv1_hierarchies
,
1239 free(controller_list
);
1243 mountpoint
= get_mountpoint(line
);
1245 free_string_list(controller_list
);
1249 base_cgroup
= cgv1_get_current_cgroup(basecginfo
, controller_list
[0]);
1251 free_string_list(controller_list
);
1256 lxcfs_debug("Detected cgroupfs v1 controller \"%s\" with "
1257 "mountpoint \"%s\" and cgroup \"%s\".\n",
1258 controller_list
[0], mountpoint
, base_cgroup
);
1259 cgv1_add_controller(controller_list
, mountpoint
, base_cgroup
,
1262 free_string_list(klist
);
1263 free_string_list(nlist
);
1268 /* Retrieve init cgroup path for all controllers. */
1269 basecginfo
= read_file("/proc/1/cgroup");
1273 for (it
= cgv1_hierarchies
; it
&& *it
; it
++) {
1274 if ((*it
)->controllers
) {
1275 char *init_cgroup
, *user_slice
;
1276 /* We've already stored the controller and received its
1277 * current cgroup. If we now fail to retrieve its init
1278 * cgroup, we should probably fail.
1280 init_cgroup
= cgv1_get_current_cgroup(basecginfo
, (*it
)->controllers
[0]);
1285 cg_systemd_prune_init_scope(init_cgroup
);
1286 (*it
)->init_cgroup
= init_cgroup
;
1287 lxcfs_debug("cgroupfs v1 controller \"%s\" has init "
1289 (*(*it
)->controllers
), init_cgroup
);
1290 /* Check whether systemd has already created a cgroup
1293 user_slice
= must_make_path((*it
)->mountpoint
, (*it
)->base_cgroup
, NULL
);
1294 if (cg_systemd_created_user_slice((*it
)->base_cgroup
, (*it
)->init_cgroup
, user_slice
, uid
))
1295 (*it
)->systemd_user_slice
= true;
1303 /* __typeof__ should be safe to use with all compilers. */
1304 typedef __typeof__(((struct statfs
*)NULL
)->f_type
) fs_type_magic
;
1305 /* Check whether given mountpoint has mount type specified via @magic_val. */
1306 static bool has_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
1308 return (fs
->f_type
== (fs_type_magic
)magic_val
);
1311 /* Check whether @path is a cgroupfs v1 or cgroupfs v2 mount. Returns -1 if
1312 * statfs fails. If @path is null /sys/fs/cgroup is checked.
1314 static int cg_get_version_of_mntpt(const char *path
)
1320 ret
= statfs(path
, &sb
);
1322 ret
= statfs("/sys/fs/cgroup", &sb
);
1327 if (has_fs_type(&sb
, CGROUP_SUPER_MAGIC
))
1329 else if (has_fs_type(&sb
, CGROUP2_SUPER_MAGIC
))
1335 /* Detect and store information about the cgroupfs v2 hierarchy. Currently only
1336 * deals with the empty v2 hierachy as we do not retrieve enabled controllers.
1338 static bool cgv2_init(uid_t uid
, gid_t gid
)
1342 char *current_cgroup
= NULL
, *init_cgroup
= NULL
;
1347 current_cgroup
= cgv2_get_current_cgroup(getpid());
1348 if (!current_cgroup
) {
1349 /* No v2 hierarchy present. We're done. */
1354 init_cgroup
= cgv2_get_current_cgroup(1);
1356 /* If we're here and didn't fail already above, then something's
1357 * certainly wrong, so error this time.
1361 cg_systemd_prune_init_scope(init_cgroup
);
1363 /* Check if the v2 hierarchy is mounted at its standard location.
1364 * If so we can skip the rest of the work here. Although the unified
1365 * hierarchy can be mounted multiple times, each of those mountpoints
1366 * will expose identical information.
1368 if (cg_get_version_of_mntpt("/sys/fs/cgroup") == 2) {
1370 bool has_user_slice
= false;
1372 mountpoint
= must_copy_string("/sys/fs/cgroup");
1376 user_slice
= must_make_path(mountpoint
, current_cgroup
, NULL
);
1377 if (cg_systemd_created_user_slice(current_cgroup
, init_cgroup
, user_slice
, uid
))
1378 has_user_slice
= true;
1381 cgv2_add_controller(NULL
, mountpoint
, current_cgroup
, init_cgroup
, has_user_slice
);
1387 f
= fopen("/proc/self/mountinfo", "r");
1391 /* we support simple cgroup mounts and lxcfs mounts */
1392 while (getline(&line
, &len
, f
) != -1) {
1394 bool has_user_slice
= false;
1398 mountpoint
= get_mountpoint(line
);
1402 user_slice
= must_make_path(mountpoint
, current_cgroup
, NULL
);
1403 if (cg_systemd_created_user_slice(current_cgroup
, init_cgroup
, user_slice
, uid
))
1404 has_user_slice
= true;
1407 cgv2_add_controller(NULL
, mountpoint
, current_cgroup
, init_cgroup
, has_user_slice
);
1408 /* Although the unified hierarchy can be mounted multiple times,
1409 * each of those mountpoints will expose identical information.
1410 * So let the first mountpoint we find, win.
1416 lxcfs_debug("Detected cgroupfs v2 hierarchy at mountpoint \"%s\" with "
1417 "current cgroup \"%s\" and init cgroup \"%s\".\n",
1418 mountpoint
, current_cgroup
, init_cgroup
);
1428 /* Detect and store information about mounted cgroupfs v1 hierarchies and the
1429 * cgroupfs v2 hierarchy.
1430 * Detect whether we are on a pure cgroupfs v1, cgroupfs v2, or mixed system,
1431 * where some controllers are mounted into their standard cgroupfs v1 locations
1432 * (/sys/fs/cgroup/<controller>) and others are mounted into the cgroupfs v2
1433 * hierarchy (/sys/fs/cgroup).
1435 static bool cg_init(uid_t uid
, gid_t gid
)
1437 if (!cgv1_init(uid
, gid
))
1440 if (!cgv2_init(uid
, gid
))
1443 if (cgv1_hierarchies
&& cgv2_hierarchies
) {
1444 cg_mount_mode
= CGROUP_MIXED
;
1445 lxcfs_debug("%s\n", "Detected cgroupfs v1 and v2 hierarchies.");
1446 } else if (cgv1_hierarchies
&& !cgv2_hierarchies
) {
1447 cg_mount_mode
= CGROUP_PURE_V1
;
1448 lxcfs_debug("%s\n", "Detected cgroupfs v1 hierarchies.");
1449 } else if (cgv2_hierarchies
&& !cgv1_hierarchies
) {
1450 cg_mount_mode
= CGROUP_PURE_V2
;
1451 lxcfs_debug("%s\n", "Detected cgroupfs v2 hierarchies.");
1453 cg_mount_mode
= CGROUP_UNKNOWN
;
1454 mysyslog(LOG_ERR
, "Could not detect cgroupfs hierarchy.\n", NULL
);
1457 if (cg_mount_mode
== CGROUP_UNKNOWN
)
1463 /* Try to move/migrate us into @cgroup in a cgroupfs v1 hierarchy. */
1464 static bool cgv1_enter(const char *cgroup
)
1466 struct cgv1_hierarchy
**it
;
1468 for (it
= cgv1_hierarchies
; it
&& *it
; it
++) {
1470 bool entered
= false;
1472 if (!(*it
)->controllers
|| !(*it
)->mountpoint
||
1473 !(*it
)->init_cgroup
|| !(*it
)->create_rw_cgroup
)
1476 for (controller
= (*it
)->controllers
; controller
&& *controller
;
1480 /* We've already been placed in a user slice, so we
1481 * don't need to enter the cgroup again.
1483 if ((*it
)->systemd_user_slice
) {
1488 path
= must_make_path((*it
)->mountpoint
,
1493 if (!file_exists(path
)) {
1495 path
= must_make_path((*it
)->mountpoint
,
1501 lxcfs_debug("Attempting to enter cgroupfs v1 hierarchy in \"%s\" cgroup.\n", path
);
1502 entered
= write_int(path
, (int)getpid());
1507 lxcfs_debug("Failed to enter cgroupfs v1 hierarchy in \"%s\" cgroup.\n", path
);
1517 /* Try to move/migrate us into @cgroup in the cgroupfs v2 hierarchy. */
1518 static bool cgv2_enter(const char *cgroup
)
1520 struct cgv2_hierarchy
*v2
;
1522 bool entered
= false;
1524 if (!cgv2_hierarchies
)
1527 v2
= *cgv2_hierarchies
;
1529 if (!v2
->mountpoint
|| !v2
->base_cgroup
)
1532 if (!v2
->create_rw_cgroup
|| v2
->systemd_user_slice
)
1535 path
= must_make_path(v2
->mountpoint
, v2
->base_cgroup
, cgroup
, "/cgroup.procs", NULL
);
1536 lxcfs_debug("Attempting to enter cgroupfs v2 hierarchy in cgroup \"%s\".\n", path
);
1537 entered
= write_int(path
, (int)getpid());
1539 lxcfs_debug("Failed to enter cgroupfs v2 hierarchy in cgroup \"%s\".\n", path
);
1549 /* Wrapper around cgv{1,2}_enter(). */
1550 static bool cg_enter(const char *cgroup
)
1552 if (!cgv1_enter(cgroup
)) {
1553 mysyslog(LOG_WARNING
, "cgroupfs v1: Failed to enter cgroups.\n", NULL
);
1557 if (!cgv2_enter(cgroup
)) {
1558 mysyslog(LOG_WARNING
, "cgroupfs v2: Failed to enter cgroups.\n", NULL
);
1565 /* Escape to root cgroup in all detected cgroupfs v1 hierarchies. */
1566 static void cgv1_escape(void)
1568 struct cgv1_hierarchy
**it
;
1570 /* In case systemd hasn't already placed us in a user slice for the
1571 * cpuset v1 controller we will reside in the root cgroup. This means
1572 * that cgroup.clone_children will not have been initialized for us so
1575 for (it
= cgv1_hierarchies
; it
&& *it
; it
++)
1576 if (!cgv1_handle_root_cpuset_hierarchy(*it
))
1577 mysyslog(LOG_WARNING
, "cgroupfs v1: Failed to initialize cpuset.\n", NULL
);
1579 if (!cgv1_enter("/"))
1580 mysyslog(LOG_WARNING
, "cgroupfs v1: Failed to escape to init's cgroup.\n", NULL
);
1583 /* Escape to root cgroup in the cgroupfs v2 hierarchy. */
1584 static void cgv2_escape(void)
1586 if (!cgv2_enter("/"))
1587 mysyslog(LOG_WARNING
, "cgroupfs v2: Failed to escape to init's cgroup.\n", NULL
);
1590 /* Wrapper around cgv{1,2}_escape(). */
1591 static void cg_escape(void)
1597 /* Get uid and gid for @user. */
1598 static bool get_uid_gid(const char *user
, uid_t
*uid
, gid_t
*gid
)
1600 struct passwd
*pwent
;
1602 pwent
= getpwnam(user
);
1606 *uid
= pwent
->pw_uid
;
1607 *gid
= pwent
->pw_gid
;
1612 /* Check if cgroup belongs to our uid and gid. If so, reuse it. */
1613 static bool cg_belongs_to_uid_gid(const char *path
, uid_t uid
, gid_t gid
)
1615 struct stat statbuf
;
1617 if (stat(path
, &statbuf
) < 0)
1620 if (!(statbuf
.st_uid
== uid
) || !(statbuf
.st_gid
== gid
))
1626 /* Create cpumask from cpulist aka turn:
1634 static uint32_t *cg_cpumask(char *buf
, size_t nbits
)
1637 char *saveptr
= NULL
;
1638 size_t arrlen
= BITS_TO_LONGS(nbits
);
1639 uint32_t *bitarr
= calloc(arrlen
, sizeof(uint32_t));
1643 for (; (token
= strtok_r(buf
, ",", &saveptr
)); buf
= NULL
) {
1645 unsigned start
= strtoul(token
, NULL
, 0);
1646 unsigned end
= start
;
1648 char *range
= strchr(token
, '-');
1650 end
= strtoul(range
+ 1, NULL
, 0);
1651 if (!(start
<= end
)) {
1661 while (start
<= end
)
1662 set_bit(start
++, bitarr
);
1668 static char *string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
1672 size_t sep_len
= strlen(sep
);
1673 size_t result_len
= use_as_prefix
* sep_len
;
1678 /* calculate new string length */
1679 for (p
= (char **)parts
; *p
; p
++)
1680 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
1682 result
= calloc(result_len
+ 1, sizeof(char));
1687 strcpy(result
, sep
);
1688 for (p
= (char **)parts
; *p
; p
++) {
1689 if (p
> (char **)parts
)
1690 strcat(result
, sep
);
1697 /* The largest integer that can fit into long int is 2^64. This is a
1700 #define __IN_TO_STR_LEN 21
1701 /* Turn cpumask into simple, comma-separated cpulist. */
1702 static char *cg_cpumask_to_cpulist(uint32_t *bitarr
, size_t nbits
)
1706 char numstr
[__IN_TO_STR_LEN
] = {0};
1707 char **cpulist
= NULL
;
1709 for (i
= 0; i
<= nbits
; i
++) {
1710 if (is_set(i
, bitarr
)) {
1711 ret
= snprintf(numstr
, __IN_TO_STR_LEN
, "%zu", i
);
1712 if (ret
< 0 || (size_t)ret
>= __IN_TO_STR_LEN
) {
1713 free_string_list(cpulist
);
1716 must_append_string(&cpulist
, numstr
);
1719 return string_join(",", (const char **)cpulist
, false);
1722 static ssize_t
cg_get_max_cpus(char *cpulist
)
1725 char *maxcpus
= cpulist
;
1728 c1
= strrchr(maxcpus
, ',');
1732 c2
= strrchr(maxcpus
, '-');
1741 /* If the above logic is correct, c1 should always hold a valid string
1746 cpus
= strtoul(c1
, NULL
, 0);
1753 static ssize_t
write_nointr(int fd
, const void* buf
, size_t count
)
1757 ret
= write(fd
, buf
, count
);
1758 if (ret
< 0 && errno
== EINTR
)
1763 static int write_to_file(const char *filename
, const void* buf
, size_t count
, bool add_newline
)
1765 int fd
, saved_errno
;
1768 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, 0666);
1771 ret
= write_nointr(fd
, buf
, count
);
1774 if ((size_t)ret
!= count
)
1777 ret
= write_nointr(fd
, "\n", 1);
1785 saved_errno
= errno
;
1787 errno
= saved_errno
;
1791 #define __ISOL_CPUS "/sys/devices/system/cpu/isolated"
1792 static bool cg_filter_and_set_cpus(char *path
, bool am_initialized
)
1794 char *lastslash
, *fpath
, oldv
;
1798 ssize_t maxposs
= 0, maxisol
= 0;
1799 char *cpulist
= NULL
, *posscpus
= NULL
, *isolcpus
= NULL
;
1800 uint32_t *possmask
= NULL
, *isolmask
= NULL
;
1801 bool bret
= false, flipped_bit
= false;
1803 lastslash
= strrchr(path
, '/');
1804 if (!lastslash
) { // bug... this shouldn't be possible
1805 lxcfs_debug("Invalid path: %s.\n", path
);
1810 fpath
= must_make_path(path
, "cpuset.cpus", NULL
);
1811 posscpus
= read_file(fpath
);
1813 lxcfs_debug("Could not read file: %s.\n", fpath
);
1817 /* Get maximum number of cpus found in possible cpuset. */
1818 maxposs
= cg_get_max_cpus(posscpus
);
1822 if (!file_exists(__ISOL_CPUS
)) {
1823 /* This system doesn't expose isolated cpus. */
1824 lxcfs_debug("%s", "Path: "__ISOL_CPUS
" to read isolated cpus from does not exist.\n");
1826 /* No isolated cpus but we weren't already initialized by
1827 * someone. We should simply copy the parents cpuset.cpus
1830 if (!am_initialized
) {
1831 lxcfs_debug("%s", "Copying cpuset of parent cgroup.\n");
1834 /* No isolated cpus but we were already initialized by someone.
1835 * Nothing more to do for us.
1840 isolcpus
= read_file(__ISOL_CPUS
);
1842 lxcfs_debug("%s", "Could not read file "__ISOL_CPUS
"\n");
1845 if (!isdigit(isolcpus
[0])) {
1846 lxcfs_debug("%s", "No isolated cpus detected.\n");
1848 /* No isolated cpus but we weren't already initialized by
1849 * someone. We should simply copy the parents cpuset.cpus
1852 if (!am_initialized
) {
1853 lxcfs_debug("%s", "Copying cpuset of parent cgroup.\n");
1856 /* No isolated cpus but we were already initialized by someone.
1857 * Nothing more to do for us.
1862 /* Get maximum number of cpus found in isolated cpuset. */
1863 maxisol
= cg_get_max_cpus(isolcpus
);
1867 if (maxposs
< maxisol
)
1871 possmask
= cg_cpumask(posscpus
, maxposs
);
1873 lxcfs_debug("%s", "Could not create cpumask for all possible cpus.\n");
1877 isolmask
= cg_cpumask(isolcpus
, maxposs
);
1879 lxcfs_debug("%s", "Could not create cpumask for all isolated cpus.\n");
1883 for (i
= 0; i
<= maxposs
; i
++) {
1884 if (is_set(i
, isolmask
) && is_set(i
, possmask
)) {
1886 clear_bit(i
, possmask
);
1891 lxcfs_debug("%s", "No isolated cpus present in cpuset.\n");
1894 lxcfs_debug("%s", "Removed isolated cpus from cpuset.\n");
1896 cpulist
= cg_cpumask_to_cpulist(possmask
, maxposs
);
1898 lxcfs_debug("%s", "Could not create cpu list.\n");
1904 fpath
= must_make_path(path
, "cpuset.cpus", NULL
);
1905 ret
= write_to_file(fpath
, cpulist
, strlen(cpulist
), false);
1907 lxcfs_debug("Could not write cpu list to: %s.\n", fpath
);
1920 if (posscpus
!= cpulist
)
1928 int read_from_file(const char *filename
, void* buf
, size_t count
)
1930 int fd
= -1, saved_errno
;
1933 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
1937 if (!buf
|| !count
) {
1940 while ((ret
= read(fd
, buf2
, 100)) > 0)
1945 memset(buf
, 0, count
);
1946 ret
= read(fd
, buf
, count
);
1950 lxcfs_debug("read %s: %s", filename
, strerror(errno
));
1952 saved_errno
= errno
;
1954 errno
= saved_errno
;
1958 /* Copy contents of parent(@path)/@file to @path/@file */
1959 static bool cg_copy_parent_file(char *path
, char *file
)
1961 char *lastslash
, *value
= NULL
, *fpath
, oldv
;
1965 lastslash
= strrchr(path
, '/');
1966 if (!lastslash
) { // bug... this shouldn't be possible
1967 lxcfs_debug("cgfsng:copy_parent_file: bad path %s", path
);
1972 fpath
= must_make_path(path
, file
, NULL
);
1973 len
= read_from_file(fpath
, NULL
, 0);
1976 value
= must_alloc(len
+ 1);
1977 if (read_from_file(fpath
, value
, len
) != len
)
1981 fpath
= must_make_path(path
, file
, NULL
);
1982 ret
= write_to_file(fpath
, value
, len
, false);
1984 lxcfs_debug("Unable to write %s to %s", value
, fpath
);
1990 lxcfs_debug("Error reading '%s'", fpath
);
1996 /* In case systemd hasn't already placed us in a user slice for the cpuset v1
1997 * controller we will reside in the root cgroup. This means that
1998 * cgroup.clone_children will not have been initialized for us so we need to do
2001 static bool cgv1_handle_root_cpuset_hierarchy(struct cgv1_hierarchy
*h
)
2003 char *clonechildrenpath
, v
;
2005 if (!string_in_list(h
->controllers
, "cpuset"))
2008 clonechildrenpath
= must_make_path(h
->mountpoint
, "cgroup.clone_children", NULL
);
2010 if (read_from_file(clonechildrenpath
, &v
, 1) < 0) {
2011 lxcfs_debug("Failed to read '%s'", clonechildrenpath
);
2012 free(clonechildrenpath
);
2016 if (v
== '1') { /* already set for us by someone else */
2017 free(clonechildrenpath
);
2021 if (write_to_file(clonechildrenpath
, "1", 1, false) < 0) {
2022 /* Set clone_children so children inherit our settings */
2023 lxcfs_debug("Failed to write 1 to %s", clonechildrenpath
);
2024 free(clonechildrenpath
);
2027 free(clonechildrenpath
);
2032 * Initialize the cpuset hierarchy in first directory of @gname and
2033 * set cgroup.clone_children so that children inherit settings.
2034 * Since the h->base_path is populated by init or ourselves, we know
2035 * it is already initialized.
2037 static bool cgv1_handle_cpuset_hierarchy(struct cgv1_hierarchy
*h
,
2040 char *cgpath
, *clonechildrenpath
, v
, *slash
;
2042 if (!string_in_list(h
->controllers
, "cpuset"))
2047 slash
= strchr(cgroup
, '/');
2051 cgpath
= must_make_path(h
->mountpoint
, h
->base_cgroup
, cgroup
, NULL
);
2054 if (mkdir(cgpath
, 0755) < 0 && errno
!= EEXIST
) {
2055 lxcfs_debug("Failed to create '%s'", cgpath
);
2059 clonechildrenpath
= must_make_path(cgpath
, "cgroup.clone_children", NULL
);
2060 if (!file_exists(clonechildrenpath
)) { /* unified hierarchy doesn't have clone_children */
2061 free(clonechildrenpath
);
2065 if (read_from_file(clonechildrenpath
, &v
, 1) < 0) {
2066 lxcfs_debug("Failed to read '%s'", clonechildrenpath
);
2067 free(clonechildrenpath
);
2072 /* Make sure any isolated cpus are removed from cpuset.cpus. */
2073 if (!cg_filter_and_set_cpus(cgpath
, v
== '1')) {
2074 lxcfs_debug("%s", "Failed to remove isolated cpus.\n");
2075 free(clonechildrenpath
);
2080 if (v
== '1') { /* already set for us by someone else */
2081 lxcfs_debug("%s", "\"cgroup.clone_children\" was already set to \"1\".\n");
2082 free(clonechildrenpath
);
2087 /* copy parent's settings */
2088 if (!cg_copy_parent_file(cgpath
, "cpuset.mems")) {
2089 lxcfs_debug("%s", "Failed to copy \"cpuset.mems\" settings.\n");
2091 free(clonechildrenpath
);
2096 if (write_to_file(clonechildrenpath
, "1", 1, false) < 0) {
2097 /* Set clone_children so children inherit our settings */
2098 lxcfs_debug("Failed to write 1 to %s", clonechildrenpath
);
2099 free(clonechildrenpath
);
2102 free(clonechildrenpath
);
2106 /* Create and chown @cgroup for all given controllers in a cgroupfs v1 hierarchy
2107 * (For example, create @cgroup for the cpu and cpuacct controller mounted into
2108 * /sys/fs/cgroup/cpu,cpuacct). Check if the path already exists and report back
2109 * to the caller in @existed.
2111 #define __PAM_CGFS_USER "/user/"
2112 #define __PAM_CGFS_USER_LEN 6
2113 static bool cgv1_create_one(struct cgv1_hierarchy
*h
, const char *cgroup
, uid_t uid
, gid_t gid
, bool *existed
)
2115 char *clean_base_cgroup
, *path
;
2117 struct cgv1_hierarchy
*it
;
2118 bool created
= false;
2122 for (controller
= it
->controllers
; controller
&& *controller
;
2124 if (!cgv1_handle_cpuset_hierarchy(it
, cgroup
))
2127 /* If systemd has already created a cgroup for us, keep using
2130 if (cg_systemd_chown_existing_cgroup(it
->mountpoint
,
2131 it
->base_cgroup
, uid
, gid
,
2132 it
->systemd_user_slice
)) {
2136 /* We need to make sure that we do not create an endless chain
2137 * of sub-cgroups. So we check if we have already logged in
2138 * somehow (sudo -i, su, etc.) and have created a
2139 * /user/PAM_user/idx cgroup. If so, we skip that part. For most
2140 * cgroups this is unnecessary since we use the init_cgroup
2141 * anyway, but for controllers which have an existing systemd
2142 * cgroup that does not match the current uid, this is pretty
2145 if (strncmp(it
->base_cgroup
, __PAM_CGFS_USER
, __PAM_CGFS_USER_LEN
) == 0) {
2146 free(it
->base_cgroup
);
2147 it
->base_cgroup
= must_copy_string("/");
2150 strstr(it
->base_cgroup
, __PAM_CGFS_USER
);
2151 if (clean_base_cgroup
)
2152 *clean_base_cgroup
= '\0';
2155 path
= must_make_path(it
->mountpoint
, it
->init_cgroup
, cgroup
, NULL
);
2156 lxcfs_debug("Constructing path: %s.\n", path
);
2157 if (file_exists(path
)) {
2158 bool our_cg
= cg_belongs_to_uid_gid(path
, uid
, gid
);
2159 lxcfs_debug("%s existed and does %shave our uid: %d and gid: %d.\n", path
, our_cg
? "" : "not ", uid
, gid
);
2167 created
= mkdir_p(it
->mountpoint
, path
);
2172 if (chown(path
, uid
, gid
) < 0)
2173 mysyslog(LOG_WARNING
,
2174 "Failed to chown %s to %d:%d: %s.\n", path
,
2175 (int)uid
, (int)gid
, strerror(errno
), NULL
);
2176 lxcfs_debug("Chowned %s to %d:%d.\n", path
, (int)uid
, (int)gid
);
2184 /* Try to remove @cgroup for all given controllers in a cgroupfs v1 hierarchy
2185 * (For example, try to remove @cgroup for the cpu and cpuacct controller
2186 * mounted into /sys/fs/cgroup/cpu,cpuacct). Ignores failures.
2188 static bool cgv1_remove_one(struct cgv1_hierarchy
*h
, const char *cgroup
)
2193 /* Better safe than sorry. */
2194 if (!h
->controllers
)
2197 /* Cgroups created by systemd for us which we re-use won't be removed
2198 * here, since we're using init_cgroup + cgroup as path instead of
2199 * base_cgroup + cgroup.
2201 path
= must_make_path(h
->mountpoint
, h
->init_cgroup
, cgroup
, NULL
);
2202 (void)recursive_rmdir(path
);
2208 /* Try to remove @cgroup the cgroupfs v2 hierarchy. */
2209 static bool cgv2_remove(const char *cgroup
)
2211 struct cgv2_hierarchy
*v2
;
2214 if (!cgv2_hierarchies
)
2217 v2
= *cgv2_hierarchies
;
2219 /* If we reused an already existing cgroup, don't bother trying to
2220 * remove (a potentially wrong)/the path.
2221 * Cgroups created by systemd for us which we re-use would be removed
2222 * here, since we're using base_cgroup + cgroup as path.
2224 if (v2
->systemd_user_slice
)
2227 path
= must_make_path(v2
->mountpoint
, v2
->base_cgroup
, cgroup
, NULL
);
2228 (void)recursive_rmdir(path
);
2234 /* Create @cgroup in all detected cgroupfs v1 hierarchy. If the creation fails
2235 * for any cgroupfs v1 hierarchy, remove all we have created so far. Report
2236 * back, to the caller if the creation failed due to @cgroup already existing
2239 static bool cgv1_create(const char *cgroup
, uid_t uid
, gid_t gid
, bool *existed
)
2241 struct cgv1_hierarchy
**it
, **rev_it
;
2242 bool all_created
= true;
2244 for (it
= cgv1_hierarchies
; it
&& *it
; it
++) {
2245 if (!(*it
)->controllers
|| !(*it
)->mountpoint
||
2246 !(*it
)->init_cgroup
|| !(*it
)->create_rw_cgroup
)
2249 if (!cgv1_create_one(*it
, cgroup
, uid
, gid
, existed
)) {
2250 all_created
= false;
2258 for (rev_it
= cgv1_hierarchies
; rev_it
&& *rev_it
&& (*rev_it
!= *it
);
2260 cgv1_remove_one(*rev_it
, cgroup
);
2265 /* Create @cgroup in the cgroupfs v2 hierarchy. Report back, to the caller if
2266 * the creation failed due to @cgroup already existing via @existed.
2268 static bool cgv2_create(const char *cgroup
, uid_t uid
, gid_t gid
, bool *existed
)
2270 char *clean_base_cgroup
;
2272 struct cgv2_hierarchy
*v2
;
2273 bool our_cg
= false, created
= false;
2277 if (!cgv2_hierarchies
|| !(*cgv2_hierarchies
)->create_rw_cgroup
)
2280 v2
= *cgv2_hierarchies
;
2282 /* We can't be placed under init's cgroup for the v2 hierarchy. We need
2283 * to be placed under our current cgroup.
2285 if (cg_systemd_chown_existing_cgroup(v2
->mountpoint
, v2
->base_cgroup
,
2286 uid
, gid
, v2
->systemd_user_slice
))
2287 goto chown_cgroup_procs_file
;
2289 /* We need to make sure that we do not create an endless chain of
2290 * sub-cgroups. So we check if we have already logged in somehow (sudo
2291 * -i, su, etc.) and have created a /user/PAM_user/idx cgroup. If so, we
2294 if (strncmp(v2
->base_cgroup
, __PAM_CGFS_USER
, __PAM_CGFS_USER_LEN
) == 0) {
2295 free(v2
->base_cgroup
);
2296 v2
->base_cgroup
= must_copy_string("/");
2298 clean_base_cgroup
= strstr(v2
->base_cgroup
, __PAM_CGFS_USER
);
2299 if (clean_base_cgroup
)
2300 *clean_base_cgroup
= '\0';
2303 path
= must_make_path(v2
->mountpoint
, v2
->base_cgroup
, cgroup
, NULL
);
2304 lxcfs_debug("Constructing path \"%s\".\n", path
);
2305 if (file_exists(path
)) {
2306 our_cg
= cg_belongs_to_uid_gid(path
, uid
, gid
);
2308 "%s existed and does %shave our uid: %d and gid: %d.\n",
2309 path
, our_cg
? "" : "not ", uid
, gid
);
2313 goto chown_cgroup_procs_file
;
2320 created
= mkdir_p(v2
->mountpoint
, path
);
2326 /* chown cgroup to user */
2327 if (chown(path
, uid
, gid
) < 0)
2328 mysyslog(LOG_WARNING
, "Failed to chown %s to %d:%d: %s.\n",
2329 path
, (int)uid
, (int)gid
, strerror(errno
), NULL
);
2331 lxcfs_debug("Chowned %s to %d:%d.\n", path
, (int)uid
, (int)gid
);
2334 chown_cgroup_procs_file
:
2335 /* chown cgroup.procs to user */
2336 if (v2
->systemd_user_slice
)
2337 path
= must_make_path(v2
->mountpoint
, v2
->base_cgroup
,
2338 "/cgroup.procs", NULL
);
2340 path
= must_make_path(v2
->mountpoint
, v2
->base_cgroup
, cgroup
,
2341 "/cgroup.procs", NULL
);
2342 if (chown(path
, uid
, gid
) < 0)
2343 mysyslog(LOG_WARNING
, "Failed to chown %s to %d:%d: %s.\n",
2344 path
, (int)uid
, (int)gid
, strerror(errno
), NULL
);
2346 lxcfs_debug("Chowned %s to %d:%d.\n", path
, (int)uid
, (int)gid
);
2352 /* Create writeable cgroups for @user at login. Details can be found in the
2353 * preamble/license at the top of this file.
2355 static int handle_login(const char *user
, uid_t uid
, gid_t gid
)
2359 char cg
[MAXPATHLEN
];
2364 ret
= snprintf(cg
, MAXPATHLEN
, "/user/%s/%d", user
, idx
);
2365 if (ret
< 0 || ret
>= MAXPATHLEN
) {
2366 mysyslog(LOG_ERR
, "Username too long.\n", NULL
);
2367 return PAM_SESSION_ERR
;
2371 if (!cgv2_create(cg
, uid
, gid
, &existed
)) {
2377 mysyslog(LOG_ERR
, "Failed to create a cgroup for user %s.\n", user
, NULL
);
2378 return PAM_SESSION_ERR
;
2382 if (!cgv1_create(cg
, uid
, gid
, &existed
)) {
2388 mysyslog(LOG_ERR
, "Failed to create a cgroup for user %s.\n", user
, NULL
);
2389 return PAM_SESSION_ERR
;
2392 if (!cg_enter(cg
)) {
2393 mysyslog( LOG_ERR
, "Failed to enter user cgroup %s for user %s.\n", cg
, user
, NULL
);
2394 return PAM_SESSION_ERR
;
2402 /* Try to prune cgroups we created and that now are empty from all cgroupfs v1
2405 static bool cgv1_prune_empty_cgroups(const char *user
)
2407 bool controller_removed
= true;
2408 bool all_removed
= true;
2409 struct cgv1_hierarchy
**it
;
2411 for (it
= cgv1_hierarchies
; it
&& *it
; it
++) {
2413 char *path_base
, *path_init
;
2416 if (!(*it
)->controllers
|| !(*it
)->mountpoint
||
2417 !(*it
)->init_cgroup
|| !(*it
)->create_rw_cgroup
)
2420 for (controller
= (*it
)->controllers
; controller
&& *controller
;
2422 bool path_base_rm
, path_init_rm
;
2424 path_base
= must_make_path((*it
)->mountpoint
, (*it
)->base_cgroup
, "/user", user
, NULL
);
2425 lxcfs_debug("cgroupfs v1: Trying to prune \"%s\".\n", path_base
);
2426 ret
= recursive_rmdir(path_base
);
2427 if (ret
== -ENOENT
|| ret
>= 0)
2428 path_base_rm
= true;
2430 path_base_rm
= false;
2433 path_init
= must_make_path((*it
)->mountpoint
, (*it
)->init_cgroup
, "/user", user
, NULL
);
2434 lxcfs_debug("cgroupfs v1: Trying to prune \"%s\".\n", path_init
);
2435 ret
= recursive_rmdir(path_init
);
2436 if (ret
== -ENOENT
|| ret
>= 0)
2437 path_init_rm
= true;
2439 path_init_rm
= false;
2442 if (!path_base_rm
&& !path_init_rm
) {
2443 controller_removed
= false;
2447 controller_removed
= true;
2450 if (!controller_removed
)
2451 all_removed
= false;
2457 /* Try to prune cgroup we created and that now is empty from the cgroupfs v2
2460 static bool cgv2_prune_empty_cgroups(const char *user
)
2463 struct cgv2_hierarchy
*v2
;
2464 char *path_base
, *path_init
;
2465 bool path_base_rm
, path_init_rm
;
2467 if (!cgv2_hierarchies
)
2470 v2
= *cgv2_hierarchies
;
2472 path_base
= must_make_path(v2
->mountpoint
, v2
->base_cgroup
, "/user", user
, NULL
);
2473 lxcfs_debug("cgroupfs v2: Trying to prune \"%s\".\n", path_base
);
2474 ret
= recursive_rmdir(path_base
);
2475 if (ret
== -ENOENT
|| ret
>= 0)
2476 path_base_rm
= true;
2478 path_base_rm
= false;
2481 path_init
= must_make_path(v2
->mountpoint
, v2
->init_cgroup
, "/user", user
, NULL
);
2482 lxcfs_debug("cgroupfs v2: Trying to prune \"%s\".\n", path_init
);
2483 ret
= recursive_rmdir(path_init
);
2484 if (ret
== -ENOENT
|| ret
>= 0)
2485 path_init_rm
= true;
2487 path_init_rm
= false;
2490 if (!path_base_rm
&& !path_init_rm
)
2496 /* Wrapper around cgv{1,2}_prune_empty_cgroups(). */
2497 static void cg_prune_empty_cgroups(const char *user
)
2499 (void)cgv1_prune_empty_cgroups(user
);
2500 (void)cgv2_prune_empty_cgroups(user
);
2503 /* Free allocated information for detected cgroupfs v1 hierarchies. */
2504 static void cgv1_free_hierarchies(void)
2506 struct cgv1_hierarchy
**it
;
2508 if (!cgv1_hierarchies
)
2511 for (it
= cgv1_hierarchies
; it
&& *it
; it
++) {
2512 if ((*it
)->controllers
) {
2514 for (tmp
= (*it
)->controllers
; tmp
&& *tmp
; tmp
++)
2517 free((*it
)->controllers
);
2519 free((*it
)->mountpoint
);
2520 free((*it
)->base_cgroup
);
2521 free((*it
)->fullcgpath
);
2522 free((*it
)->init_cgroup
);
2524 free(cgv1_hierarchies
);
2527 /* Free allocated information for the detected cgroupfs v2 hierarchy. */
2528 static void cgv2_free_hierarchies(void)
2530 struct cgv2_hierarchy
**it
;
2532 if (!cgv2_hierarchies
)
2535 for (it
= cgv2_hierarchies
; it
&& *it
; it
++) {
2536 if ((*it
)->controllers
) {
2538 for (tmp
= (*it
)->controllers
; tmp
&& *tmp
; tmp
++)
2541 free((*it
)->controllers
);
2543 free((*it
)->mountpoint
);
2544 free((*it
)->base_cgroup
);
2545 free((*it
)->fullcgpath
);
2546 free((*it
)->init_cgroup
);
2548 free(cgv2_hierarchies
);
2551 /* Wrapper around cgv{1,2}_free_hierarchies(). */
2552 static void cg_exit(void)
2554 cgv1_free_hierarchies();
2555 cgv2_free_hierarchies();
2558 int pam_sm_open_session(pam_handle_t
*pamh
, int flags
, int argc
,
2564 const char *PAM_user
= NULL
;
2566 ret
= pam_get_user(pamh
, &PAM_user
, NULL
);
2567 if (ret
!= PAM_SUCCESS
) {
2568 mysyslog(LOG_ERR
, "PAM-CGFS: couldn't get user\n", NULL
);
2569 return PAM_SESSION_ERR
;
2572 if (!get_uid_gid(PAM_user
, &uid
, &gid
)) {
2573 mysyslog(LOG_ERR
, "Failed to get uid and gid for %s.\n", PAM_user
, NULL
);
2574 return PAM_SESSION_ERR
;
2577 if (!cg_init(uid
, gid
)) {
2578 mysyslog(LOG_ERR
, "Failed to get list of controllers\n", NULL
);
2579 return PAM_SESSION_ERR
;
2582 /* Try to prune cgroups, that are actually empty but were still marked
2583 * as busy by the kernel so we couldn't remove them on session close.
2585 cg_prune_empty_cgroups(PAM_user
);
2587 if (cg_mount_mode
== CGROUP_UNKNOWN
)
2588 return PAM_SESSION_ERR
;
2590 if (argc
> 1 && !strcmp(argv
[0], "-c")) {
2591 char **clist
= make_string_list(argv
[1], ",");
2594 * We don't allow using "all" and other controllers explicitly because
2595 * that simply doesn't make any sense.
2597 if (string_list_length(clist
) > 1 && string_in_list(clist
, "all")) {
2598 mysyslog(LOG_ERR
, "Invalid -c option, cannot specify individual controllers alongside 'all'.\n", NULL
);
2599 free_string_list(clist
);
2600 return PAM_SESSION_ERR
;
2603 cg_mark_to_make_rw(clist
);
2604 free_string_list(clist
);
2607 return handle_login(PAM_user
, uid
, gid
);
2610 int pam_sm_close_session(pam_handle_t
*pamh
, int flags
, int argc
,
2616 const char *PAM_user
= NULL
;
2618 ret
= pam_get_user(pamh
, &PAM_user
, NULL
);
2619 if (ret
!= PAM_SUCCESS
) {
2620 mysyslog(LOG_ERR
, "PAM-CGFS: couldn't get user\n", NULL
);
2621 return PAM_SESSION_ERR
;
2624 if (!get_uid_gid(PAM_user
, &uid
, &gid
)) {
2625 mysyslog(LOG_ERR
, "Failed to get uid and gid for %s.\n", PAM_user
, NULL
);
2626 return PAM_SESSION_ERR
;
2629 if (cg_mount_mode
== CGROUP_UNINITIALIZED
) {
2630 if (!cg_init(uid
, gid
))
2631 mysyslog(LOG_ERR
, "Failed to get list of controllers\n", NULL
);
2633 if (argc
> 1 && !strcmp(argv
[0], "-c")) {
2634 char **clist
= make_string_list(argv
[1], ",");
2637 * We don't allow using "all" and other controllers explicitly because
2638 * that simply doesn't make any sense.
2640 if (string_list_length(clist
) > 1 && string_in_list(clist
, "all")) {
2641 mysyslog(LOG_ERR
, "Invalid -c option, cannot specify individual controllers alongside 'all'.\n", NULL
);
2642 free_string_list(clist
);
2643 return PAM_SESSION_ERR
;
2646 cg_mark_to_make_rw(clist
);
2647 free_string_list(clist
);
2651 cg_prune_empty_cgroups(PAM_user
);