1717#include <unistd.h>
1818
1919#include "cgroup_util.h"
20- #include "../clone3/clone3_selftests.h"
20+ #include "../../ clone3/clone3_selftests.h"
2121
2222/* Returns read len on success, or -errno on failure. */
23- static ssize_t read_text (const char * path , char * buf , size_t max_len )
23+ ssize_t read_text (const char * path , char * buf , size_t max_len )
2424{
2525 ssize_t len ;
2626 int fd ;
@@ -39,7 +39,7 @@ static ssize_t read_text(const char *path, char *buf, size_t max_len)
3939}
4040
4141/* Returns written len on success, or -errno on failure. */
42- static ssize_t write_text (const char * path , char * buf , ssize_t len )
42+ ssize_t write_text (const char * path , char * buf , ssize_t len )
4343{
4444 int fd ;
4545
@@ -217,7 +217,8 @@ int cg_write_numeric(const char *cgroup, const char *control, long value)
217217 return cg_write (cgroup , control , buf );
218218}
219219
220- int cg_find_unified_root (char * root , size_t len , bool * nsdelegate )
220+ static int cg_find_root (char * root , size_t len , const char * controller ,
221+ bool * nsdelegate )
221222{
222223 char buf [10 * PAGE_SIZE ];
223224 char * fs , * mount , * type , * options ;
@@ -236,18 +237,37 @@ int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
236237 options = strtok (NULL , delim );
237238 strtok (NULL , delim );
238239 strtok (NULL , delim );
239-
240- if (strcmp (type , "cgroup2" ) == 0 ) {
241- strncpy (root , mount , len );
242- if (nsdelegate )
243- * nsdelegate = !!strstr (options , "nsdelegate" );
244- return 0 ;
240+ if (strcmp (type , "cgroup" ) == 0 ) {
241+ if (!controller || !strstr (options , controller ))
242+ continue ;
243+ } else if (strcmp (type , "cgroup2" ) == 0 ) {
244+ if (controller &&
245+ cg_read_strstr (mount , "cgroup.controllers" , controller ))
246+ continue ;
247+ } else {
248+ continue ;
245249 }
250+ strncpy (root , mount , len );
251+
252+ if (nsdelegate )
253+ * nsdelegate = !!strstr (options , "nsdelegate" );
254+ return 0 ;
255+
246256 }
247257
248258 return -1 ;
249259}
250260
261+ int cg_find_controller_root (char * root , size_t len , const char * controller )
262+ {
263+ return cg_find_root (root , len , controller , NULL );
264+ }
265+
266+ int cg_find_unified_root (char * root , size_t len , bool * nsdelegate )
267+ {
268+ return cg_find_root (root , len , NULL , nsdelegate );
269+ }
270+
251271int cg_create (const char * cgroup )
252272{
253273 return mkdir (cgroup , 0755 );
@@ -488,84 +508,6 @@ int cg_run_nowait(const char *cgroup,
488508 return pid ;
489509}
490510
491- int get_temp_fd (void )
492- {
493- return open ("." , O_TMPFILE | O_RDWR | O_EXCL );
494- }
495-
496- int alloc_pagecache (int fd , size_t size )
497- {
498- char buf [PAGE_SIZE ];
499- struct stat st ;
500- int i ;
501-
502- if (fstat (fd , & st ))
503- goto cleanup ;
504-
505- size += st .st_size ;
506-
507- if (ftruncate (fd , size ))
508- goto cleanup ;
509-
510- for (i = 0 ; i < size ; i += sizeof (buf ))
511- read (fd , buf , sizeof (buf ));
512-
513- return 0 ;
514-
515- cleanup :
516- return -1 ;
517- }
518-
519- int alloc_anon (const char * cgroup , void * arg )
520- {
521- size_t size = (unsigned long )arg ;
522- char * buf , * ptr ;
523-
524- buf = malloc (size );
525- for (ptr = buf ; ptr < buf + size ; ptr += PAGE_SIZE )
526- * ptr = 0 ;
527-
528- free (buf );
529- return 0 ;
530- }
531-
532- int is_swap_enabled (void )
533- {
534- char buf [PAGE_SIZE ];
535- const char delim [] = "\n" ;
536- int cnt = 0 ;
537- char * line ;
538-
539- if (read_text ("/proc/swaps" , buf , sizeof (buf )) <= 0 )
540- return -1 ;
541-
542- for (line = strtok (buf , delim ); line ; line = strtok (NULL , delim ))
543- cnt ++ ;
544-
545- return cnt > 1 ;
546- }
547-
548- int set_oom_adj_score (int pid , int score )
549- {
550- char path [PATH_MAX ];
551- int fd , len ;
552-
553- sprintf (path , "/proc/%d/oom_score_adj" , pid );
554-
555- fd = open (path , O_WRONLY | O_APPEND );
556- if (fd < 0 )
557- return fd ;
558-
559- len = dprintf (fd , "%d" , score );
560- if (len < 0 ) {
561- close (fd );
562- return len ;
563- }
564-
565- close (fd );
566- return 0 ;
567- }
568-
569511int proc_mount_contains (const char * option )
570512{
571513 char buf [4 * PAGE_SIZE ];
0 commit comments