1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2015 Red Hat, Inc. 4f08c3bdfSopenharmony_ci * Matus Marhefka <mmarhefk@redhat.com> 5f08c3bdfSopenharmony_ci * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com> 6f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2020-2023 7f08c3bdfSopenharmony_ci */ 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci/*\ 10f08c3bdfSopenharmony_ci * [Description] 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * Creates a child process in the new specified namespace(s), child is then 13f08c3bdfSopenharmony_ci * daemonized and is running in the background. PID of the daemonized child 14f08c3bdfSopenharmony_ci * process is printed on the stdout. As the new namespace(s) is(are) maintained 15f08c3bdfSopenharmony_ci * by the daemonized child process it(they) can be removed by killing this 16f08c3bdfSopenharmony_ci * process. 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#define TST_NO_DEFAULT_MAIN 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_ci#include <stdio.h> 22f08c3bdfSopenharmony_ci#include <string.h> 23f08c3bdfSopenharmony_ci#include "tst_test.h" 24f08c3bdfSopenharmony_ci#include "tst_ns_common.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ciextern struct tst_test *tst_test; 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic struct tst_test test = { 29f08c3bdfSopenharmony_ci .forks_child = 1, /* Needed by SAFE_CLONE */ 30f08c3bdfSopenharmony_ci}; 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_cistatic void print_help(void) 33f08c3bdfSopenharmony_ci{ 34f08c3bdfSopenharmony_ci int i; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_ci printf("usage: tst_ns_create <%s", params[0].name); 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_ci for (i = 1; params[i].name; i++) 39f08c3bdfSopenharmony_ci printf("|,%s", params[i].name); 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_ci printf(">\n"); 42f08c3bdfSopenharmony_ci} 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_cistatic void child_fn(void) 45f08c3bdfSopenharmony_ci{ 46f08c3bdfSopenharmony_ci int i; 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_ci SAFE_SETSID(); 49f08c3bdfSopenharmony_ci SAFE_CHDIR("/"); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci for (i = 0; i < SAFE_SYSCONF(_SC_OPEN_MAX); i++) 52f08c3bdfSopenharmony_ci close(i); 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci printf("pausing child\n"); 55f08c3bdfSopenharmony_ci pause(); 56f08c3bdfSopenharmony_ci} 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 59f08c3bdfSopenharmony_ci{ 60f08c3bdfSopenharmony_ci struct tst_clone_args args = { 0, SIGCHLD }; 61f08c3bdfSopenharmony_ci char *token; 62f08c3bdfSopenharmony_ci int pid; 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_ci if (argc < 2) { 65f08c3bdfSopenharmony_ci print_help(); 66f08c3bdfSopenharmony_ci return 1; 67f08c3bdfSopenharmony_ci } 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_ci tst_test = &test; 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci while ((token = strsep(&argv[1], ","))) { 72f08c3bdfSopenharmony_ci struct param *p = get_param(token); 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci if (!p) { 75f08c3bdfSopenharmony_ci printf("Unknown namespace: %s\n", token); 76f08c3bdfSopenharmony_ci print_help(); 77f08c3bdfSopenharmony_ci return 1; 78f08c3bdfSopenharmony_ci } 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci args.flags |= p->flag; 81f08c3bdfSopenharmony_ci } 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci pid = SAFE_CLONE(&args); 84f08c3bdfSopenharmony_ci if (!pid) { 85f08c3bdfSopenharmony_ci child_fn(); 86f08c3bdfSopenharmony_ci return 0; 87f08c3bdfSopenharmony_ci } 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci printf("%d", pid); 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci return 0; 92f08c3bdfSopenharmony_ci} 93