1f08c3bdfSopenharmony_ci/* Copyright (c) 2015 Red Hat, Inc. 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * This program is free software: you can redistribute it and/or modify 4f08c3bdfSopenharmony_ci * it under the terms of version 2 the GNU General Public License as 5f08c3bdfSopenharmony_ci * published by the Free Software Foundation. 6f08c3bdfSopenharmony_ci * 7f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 8f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 9f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10f08c3bdfSopenharmony_ci * GNU General Public License for more details. 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 13f08c3bdfSopenharmony_ci * along with this program. If not, see <http://www.gnu.org/licenses/>. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * Written by Matus Marhefka <mmarhefk@redhat.com> 16f08c3bdfSopenharmony_ci * 17f08c3bdfSopenharmony_ci *********************************************************************** 18f08c3bdfSopenharmony_ci * Creates a child process in the new specified namespace(s), child is then 19f08c3bdfSopenharmony_ci * daemonized and is running in the background. PID of the daemonized child 20f08c3bdfSopenharmony_ci * process is printed on the stdout. As the new namespace(s) is(are) maintained 21f08c3bdfSopenharmony_ci * by the daemonized child process it(they) can be removed by killing this 22f08c3bdfSopenharmony_ci * process. 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci */ 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_ci#define _GNU_SOURCE 27f08c3bdfSopenharmony_ci#include <sched.h> 28f08c3bdfSopenharmony_ci#include <sys/syscall.h> 29f08c3bdfSopenharmony_ci#include <sys/types.h> 30f08c3bdfSopenharmony_ci#include <sys/stat.h> 31f08c3bdfSopenharmony_ci#include <unistd.h> 32f08c3bdfSopenharmony_ci#include <string.h> 33f08c3bdfSopenharmony_ci#include <errno.h> 34f08c3bdfSopenharmony_ci#include "test.h" 35f08c3bdfSopenharmony_ci#include "lapi/namespaces_constants.h" 36f08c3bdfSopenharmony_ci#include "ns_common.h" 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cichar *TCID = "ns_create"; 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_ci 41f08c3bdfSopenharmony_civoid print_help(void) 42f08c3bdfSopenharmony_ci{ 43f08c3bdfSopenharmony_ci int i; 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci printf("usage: ns_create <%s", params[0].name); 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci for (i = 1; params[i].name; i++) 48f08c3bdfSopenharmony_ci printf("|,%s", params[i].name); 49f08c3bdfSopenharmony_ci printf(">\nThe only argument is a comma separated list " 50f08c3bdfSopenharmony_ci "of namespaces to create.\nExample: ns_create net,ipc\n"); 51f08c3bdfSopenharmony_ci} 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_cistatic int child_fn(void *arg LTP_ATTRIBUTE_UNUSED) 54f08c3bdfSopenharmony_ci{ 55f08c3bdfSopenharmony_ci int i; 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci if (setsid() == -1) { 58f08c3bdfSopenharmony_ci tst_resm(TINFO | TERRNO, "setsid"); 59f08c3bdfSopenharmony_ci exit(1); 60f08c3bdfSopenharmony_ci } 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci if (chdir("/") == -1) { 63f08c3bdfSopenharmony_ci tst_resm(TINFO | TERRNO, "chdir"); 64f08c3bdfSopenharmony_ci exit(1); 65f08c3bdfSopenharmony_ci } 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci /* close all inherrited file descriptors */ 68f08c3bdfSopenharmony_ci for (i = 0; i < sysconf(_SC_OPEN_MAX); i++) 69f08c3bdfSopenharmony_ci close(i); 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci pause(); 72f08c3bdfSopenharmony_ci return 0; 73f08c3bdfSopenharmony_ci} 74f08c3bdfSopenharmony_ci 75f08c3bdfSopenharmony_ci/* 76f08c3bdfSopenharmony_ci * ./ns_create <ipc,mnt,net,pid,user,uts> 77f08c3bdfSopenharmony_ci */ 78f08c3bdfSopenharmony_ciint main(int argc, char *argv[]) 79f08c3bdfSopenharmony_ci{ 80f08c3bdfSopenharmony_ci int pid, flags; 81f08c3bdfSopenharmony_ci char *token; 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci if (argc < 2) { 84f08c3bdfSopenharmony_ci print_help(); 85f08c3bdfSopenharmony_ci return 1; 86f08c3bdfSopenharmony_ci } 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ci flags = 0; 89f08c3bdfSopenharmony_ci while ((token = strsep(&argv[1], ","))) { 90f08c3bdfSopenharmony_ci struct param *p = get_param(token); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci if (!p) { 93f08c3bdfSopenharmony_ci tst_resm(TINFO, "Unknown namespace: %s", token); 94f08c3bdfSopenharmony_ci print_help(); 95f08c3bdfSopenharmony_ci return 1; 96f08c3bdfSopenharmony_ci } 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci flags |= p->flag; 99f08c3bdfSopenharmony_ci } 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci pid = ltp_clone_quick(flags | SIGCHLD, child_fn, NULL); 102f08c3bdfSopenharmony_ci if (pid == -1) { 103f08c3bdfSopenharmony_ci tst_resm(TINFO | TERRNO, "ltp_clone_quick"); 104f08c3bdfSopenharmony_ci return 1; 105f08c3bdfSopenharmony_ci } 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci printf("%d", pid); 108f08c3bdfSopenharmony_ci return 0; 109f08c3bdfSopenharmony_ci} 110