[6ccf55]: testcases / kernel / containers / netns / common.c Maximize Restore History

Download this file

common.c    159 lines (135 with data), 4.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) International Business Machines Corp., 2008
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
***************************************************************************/
/*=========================================================================
* This testcase creates the network namespace.
* It creates veth pair veth8 & veth9. Also assigns IP addresses to the childNS.
* Also it starts the sshd daemon @ port 7890
*
* Scripts Used: parentns.sh childns.sh
*
* Author: Veerendra <veeren@linux.vnet.ibm.com>
=========================================================================*/
#define _GNU_SOURCE
#include <sys/utsname.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "libclone.h"
#include "test.h"
#include "config.h"
static int child_fn(void *c1);
int crtchild(char *s1 , char *s2)
{
char *cmd[] = { "--", s1, s2, (char *)0 };
execve("/bin/sh", cmd, __environ);
fprintf(stderr, "Failed to execve(%s, %s): %m\n", s1, s2);
return 1;
}
int create_net_namespace(char *p1, char *c1)
{
int pid, status = 0, ret;
char *ltproot, *par;
long int clone_flags = 0;
if (tst_kvercmp(2, 6, 19) < 0)
return 1;
clone_flags |= CLONE_NEWNS;
/* Enable other namespaces too optionally */
#ifdef CLONE_NEWPID
clone_flags |= CLONE_NEWPID;
#endif
pid = ltp_clone_quick(clone_flags, child_fn, (void *) c1);
if (pid == -1) {
perror("Failed to do clone...");
return -1;
}
/* This code will be executed in parent */
ltproot = getenv("LTPROOT");
if (!ltproot) {
printf("LTPROOT env variable is not set\n");
printf("Please set LTPROOT and re-run the test.. Thankyou\n");
return -1;
}
par = malloc(FILENAME_MAX);
if (par == NULL) {
printf("FAIL: error while allocating memory");
exit(1);
}
/* We need to pass the child pid to the parentns.sh script */
sprintf(par, "%s/testcases/bin/parentns.sh %s %" PRId32 , ltproot, p1, pid);
ret = system(par);
status = WEXITSTATUS(ret);
if (ret == -1 || status != 0) {
printf("Error while running the script\n");
fflush(stdout);
exit(1);
}
fflush(stdout);
ret = waitpid(pid, &status, __WALL);
status = WEXITSTATUS(status);
if (ret == -1 || status != 0) {
printf("Error: waitpid() returns %d, status %d\n", ret, status);
}
return status;
}
/* The function to be executed in the child namespace */
int child_fn(void *c1)
{
char *ltproot, *child;
unsigned long flags = 0;
#if HAVE_UNSHARE
int ret;
#endif
/* Flags to unshare different Namespaces */
flags |= CLONE_NEWNS;
flags |= CLONE_NEWNET;
flags |= CLONE_NEWUTS;
flags |= CLONE_FS;
ltproot = getenv("LTPROOT");
if (!ltproot) {
printf("LTPROOT env variable is not set\n");
printf("Please set LTPROOT and re-run the test..\n");
return -1;
}
child = malloc(FILENAME_MAX);
if (child == NULL) {
printf("FAIL: error while allocating memory");
exit(1);
}
sprintf(child, "%s/testcases/bin/childns.sh", ltproot);
/* Unshare the network namespace in the child */
#if HAVE_UNSHARE
ret = unshare(flags);
if (ret < 0) {
perror("Failed to unshare for netns...");
return 1;
}
return crtchild(child, c1);
#else
printf("System doesn't support unshare.\n");
return -1;
#endif
}