Skip to content

Commit d2ae0ac

Browse files
authored
Merge pull request #2094 from giuseppe/procfd-refactor
Refactor mount operations to use procfd and new mount API
2 parents 6cd45af + 8a5e0e9 commit d2ae0ac

12 files changed

Lines changed: 1383 additions & 305 deletions

File tree

src/libcrun/cgroup-utils.c

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -210,35 +210,13 @@ libcrun_get_cgroup_mode (libcrun_error_t *err)
210210
return cgroup_mode;
211211
}
212212

213-
int
214-
libcrun_get_cgroup_process (pid_t pid, char **path, bool absolute, libcrun_error_t *err)
213+
static int
214+
get_cgroup_process_from_content (char *content, int cgroup_mode, char **path, bool absolute, libcrun_error_t *err)
215215
{
216-
cleanup_free char *content = NULL;
217-
char proc_cgroup_file[64];
218216
char *cg_path = NULL;
219-
size_t content_size;
220217
char *controller;
221218
char *saveptr;
222-
int cgroup_mode;
223219
bool has_data;
224-
int ret;
225-
226-
cgroup_mode = libcrun_get_cgroup_mode (err);
227-
if (UNLIKELY (cgroup_mode < 0))
228-
return cgroup_mode;
229-
230-
if (pid == 0)
231-
strcpy (proc_cgroup_file, PROC_SELF_CGROUP);
232-
else
233-
{
234-
int len = snprintf (proc_cgroup_file, sizeof (proc_cgroup_file), "/proc/%d/cgroup", pid);
235-
if (UNLIKELY (len >= (int) sizeof (proc_cgroup_file)))
236-
return crun_make_error (err, 0, "internal error: static buffer too small");
237-
}
238-
239-
ret = read_all_file (proc_cgroup_file, &content, &content_size, err);
240-
if (UNLIKELY (ret < 0))
241-
return ret;
242220

243221
for (has_data = read_proc_cgroup (content, &saveptr, NULL, &controller, &cg_path);
244222
has_data;
@@ -266,6 +244,54 @@ libcrun_get_cgroup_process (pid_t pid, char **path, bool absolute, libcrun_error
266244
return 0;
267245
}
268246

247+
int
248+
libcrun_get_cgroup_process_at (int dirfd, char **path, bool absolute, libcrun_error_t *err)
249+
{
250+
cleanup_free char *content = NULL;
251+
size_t content_size;
252+
int cgroup_mode;
253+
int ret;
254+
255+
cgroup_mode = libcrun_get_cgroup_mode (err);
256+
if (UNLIKELY (cgroup_mode < 0))
257+
return cgroup_mode;
258+
259+
ret = read_all_file_at (dirfd, SELF_CGROUP, &content, &content_size, err);
260+
if (UNLIKELY (ret < 0))
261+
return ret;
262+
263+
return get_cgroup_process_from_content (content, cgroup_mode, path, absolute, err);
264+
}
265+
266+
int
267+
libcrun_get_cgroup_process (pid_t pid, char **path, bool absolute, libcrun_error_t *err)
268+
{
269+
cleanup_free char *content = NULL;
270+
char proc_cgroup_file[64];
271+
size_t content_size;
272+
int cgroup_mode;
273+
int ret;
274+
275+
cgroup_mode = libcrun_get_cgroup_mode (err);
276+
if (UNLIKELY (cgroup_mode < 0))
277+
return cgroup_mode;
278+
279+
if (pid == 0)
280+
strcpy (proc_cgroup_file, PROC_SELF_CGROUP);
281+
else
282+
{
283+
int len = snprintf (proc_cgroup_file, sizeof (proc_cgroup_file), "/proc/%d/cgroup", pid);
284+
if (UNLIKELY (len >= (int) sizeof (proc_cgroup_file)))
285+
return crun_make_error (err, 0, "internal error: static buffer too small");
286+
}
287+
288+
ret = read_all_file (proc_cgroup_file, &content, &content_size, err);
289+
if (UNLIKELY (ret < 0))
290+
return ret;
291+
292+
return get_cgroup_process_from_content (content, cgroup_mode, path, absolute, err);
293+
}
294+
269295
static int
270296
read_pids_cgroup (int dfd, bool recurse, pid_t **pids, size_t *n_pids, size_t *allocated, libcrun_error_t *err)
271297
{

src/libcrun/cgroup-utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ int libcrun_cgroups_create_symlinks (int dirfd, libcrun_error_t *err);
2828

2929
int libcrun_get_cgroup_process (pid_t pid, char **path, bool absolute, libcrun_error_t *err);
3030

31+
int libcrun_get_cgroup_process_at (int dirfd, char **path, bool absolute, libcrun_error_t *err);
32+
3133
int libcrun_get_cgroup_mode (libcrun_error_t *err);
3234

3335
int libcrun_get_cgroup_dirfd (struct libcrun_cgroup_status *status, const char *sub_cgroup, libcrun_error_t *err);

src/libcrun/cgroup.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626
# define CGROUP_ROOT "/sys/fs/cgroup"
2727
#endif
2828

29+
#ifndef SELF_CGROUP
30+
# define SELF_CGROUP "self/cgroup"
31+
#endif
32+
2933
#ifndef PROC_SELF_CGROUP
30-
# define PROC_SELF_CGROUP "/proc/self/cgroup"
34+
# define PROC_SELF_CGROUP "/proc/" SELF_CGROUP
3135
#endif
3236

3337
enum

src/libcrun/container.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,11 +1341,6 @@ container_init_setup (void *args, pid_t own_pid, char *notify_socket,
13411341
if (UNLIKELY (ret < 0))
13421342
return ret;
13431343

1344-
/* sync 2 and 3 are sent as part of libcrun_set_mounts. */
1345-
ret = libcrun_set_mounts (entrypoint_args, container, rootfs, send_sync_cb, &sync_socket, err);
1346-
if (UNLIKELY (ret < 0))
1347-
return ret;
1348-
13491344
if (def->hooks && def->hooks->create_container_len)
13501345
{
13511346
libcrun_error_t tmp_err = NULL;
@@ -1359,6 +1354,15 @@ container_init_setup (void *args, pid_t own_pid, char *notify_socket,
13591354
return ret;
13601355
}
13611356

1357+
ret = libcrun_do_pivot_root (container, entrypoint_args->context->no_pivot, &rootfs, err);
1358+
if (UNLIKELY (ret < 0))
1359+
return ret;
1360+
1361+
/* sync 2 and 3 are sent as part of libcrun_set_mounts. */
1362+
ret = libcrun_set_mounts (entrypoint_args, container, rootfs, send_sync_cb, &sync_socket, err);
1363+
if (UNLIKELY (ret < 0))
1364+
return ret;
1365+
13621366
ret = libcrun_finalize_mounts (entrypoint_args, container, rootfs, err);
13631367
if (UNLIKELY (ret < 0))
13641368
return ret;
@@ -1378,13 +1382,6 @@ container_init_setup (void *args, pid_t own_pid, char *notify_socket,
13781382
if (UNLIKELY (ret < 0))
13791383
crun_error_write_warning_and_release (entrypoint_args->context->output_handler_arg, &err);
13801384

1381-
if (rootfs)
1382-
{
1383-
ret = libcrun_do_pivot_root (container, entrypoint_args->context->no_pivot, rootfs, err);
1384-
if (UNLIKELY (ret < 0))
1385-
return ret;
1386-
}
1387-
13881385
ret = libcrun_reopen_dev_null (err);
13891386
if (UNLIKELY (ret < 0))
13901387
return ret;

0 commit comments

Comments
 (0)