blob: 31ccdaa1a15499a981abc9dc57d6c43ed86d5d24 [file] [log] [blame]
vapierb56735e2006-08-21 07:05:41 +00001/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: t -*- */
vapier45a8ba02009-07-20 10:59:32 +00002/*
vapierb56735e2006-08-21 07:05:41 +00003 * self_exec.c: self_exec magic required to run child functions on uClinux
vapier45a8ba02009-07-20 10:59:32 +00004 *
vapierb56735e2006-08-21 07:05:41 +00005 * Copyright (C) 2005 Paul J.Y. Lahaie <pjlahaie-at-steamballoon.com>
vapier45a8ba02009-07-20 10:59:32 +00006 *
vapierb56735e2006-08-21 07:05:41 +00007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
vapier45a8ba02009-07-20 10:59:32 +000011 *
vapierb56735e2006-08-21 07:05:41 +000012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
vapier45a8ba02009-07-20 10:59:32 +000016 *
vapierb56735e2006-08-21 07:05:41 +000017 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
vapier45a8ba02009-07-20 10:59:32 +000020 *
vapierb56735e2006-08-21 07:05:41 +000021 * This software was produced by Steamballoon Incorporated
22 * 55 Byward Market Square, 2nd Floor North, Ottawa, ON K1N 9C3, Canada
23 */
24
Mike Frysinger28606c12010-08-17 17:22:45 -040025#define _GNU_SOURCE /* for asprintf */
26
27#include "config.h"
28
vapierb56735e2006-08-21 07:05:41 +000029#ifdef UCLINUX
30
vapierb56735e2006-08-21 07:05:41 +000031#include <stdarg.h>
32#include <string.h>
33#include <stdio.h>
34#include "test.h"
35
36/* Set from parse_opts.c: */
37char *child_args; /* Arguments to child when -C is used */
38
39static char *start_cwd; /* Stores the starting directory for self_exec */
40
41int asprintf(char **app, const char *fmt, ...)
42{
43 va_list ptr;
44 int rv;
45 char *p;
46
47 /*
48 * First iteration - find out size of buffer required and allocate it.
49 */
50 va_start(ptr, fmt);
51 rv = vsnprintf(NULL, 0, fmt, ptr);
52 va_end(ptr);
53
54 p = malloc(++rv); /* allocate the buffer */
55 *app = p;
56 if (!p) {
57 return -1;
58 }
59
60 /*
61 * Second iteration - actually produce output.
62 */
63 va_start(ptr, fmt);
64 rv = vsnprintf(p, rv, fmt, ptr);
65 va_end(ptr);
66
67 return rv;
68}
69
70void
71maybe_run_child(void (*child)(), char *fmt, ...)
72{
73 va_list ap;
74 char *child_dir;
75 char *p, *tok;
76 int *iptr, i, j;
77 char *s;
78 char **sptr;
79 char *endptr;
80
81 /* Store the current directory for later use. */
82 start_cwd = getcwd(NULL, 0);
83
84 if (child_args) {
85 char *args = strdup(child_args);
86
87 child_dir = strtok(args, ",");
88 if (strlen(child_dir) == 0) {
Markos Chandras8d2d8ba2012-01-03 10:40:50 +000089 tst_brkm(TBROK, NULL, "Could not get directory from -C option");
vapierb56735e2006-08-21 07:05:41 +000090 }
91
92 va_start(ap, fmt);
93
94 for (p = fmt; *p; p++) {
95 tok = strtok(NULL, ",");
96 if (!tok || strlen(tok) == 0) {
Markos Chandras8d2d8ba2012-01-03 10:40:50 +000097 tst_brkm(TBROK, NULL, "Invalid argument to -C option");
vapierb56735e2006-08-21 07:05:41 +000098 }
99
100 switch (*p) {
101 case 'd':
102 iptr = va_arg(ap, int *);
103 i = strtol(tok, &endptr, 10);
104 if (*endptr != '\0') {
Markos Chandras8d2d8ba2012-01-03 10:40:50 +0000105 tst_brkm(TBROK, NULL, "Invalid argument to -C option");
vapierb56735e2006-08-21 07:05:41 +0000106 }
107 *iptr = i;
108 break;
109 case 'n':
110 j = va_arg(ap, int);
111 i = strtol(tok, &endptr, 10);
112 if (*endptr != '\0') {
Markos Chandras8d2d8ba2012-01-03 10:40:50 +0000113 tst_brkm(TBROK, NULL, "Invalid argument to -C option");
vapierb56735e2006-08-21 07:05:41 +0000114 }
vapier45a8ba02009-07-20 10:59:32 +0000115 if (j != i) {
vapierb56735e2006-08-21 07:05:41 +0000116 va_end(ap);
117 return;
118 }
119 break;
120 case 's':
121 s = va_arg(ap, char *);
122 if (!strncpy(s, tok, strlen(tok)+1)) {
Markos Chandras8d2d8ba2012-01-03 10:40:50 +0000123 tst_brkm(TBROK, NULL, "Could not strncpy for -C option");
vapierb56735e2006-08-21 07:05:41 +0000124 }
125 break;
126 case 'S':
127 sptr = va_arg(ap, char **);
128 *sptr = strdup(tok);
129 if (!*sptr) {
Markos Chandras8d2d8ba2012-01-03 10:40:50 +0000130 tst_brkm(TBROK, NULL, "Could not strdup for -C option");
vapierb56735e2006-08-21 07:05:41 +0000131 }
132 break;
133 default:
Markos Chandras8d2d8ba2012-01-03 10:40:50 +0000134 tst_brkm(TBROK, NULL, "Format string option %c not implemented", *p);
vapierb56735e2006-08-21 07:05:41 +0000135 break;
136 }
137 }
138
139 va_end(ap);
140
Markos Chandras8d2d8ba2012-01-03 10:40:50 +0000141 if (chdir(child_dir) < 0)
142 tst_brkm(TBROK, NULL, "Could not change to %s for child", child_dir);
vapierb56735e2006-08-21 07:05:41 +0000143
144 (*child)();
145 tst_resm(TWARN, "Child function returned unexpectedly");
146 /* Exit here? or exit silently? */
147 }
148}
149
150int
151self_exec(char *argv0, char *fmt, ...)
152{
153 va_list ap;
154 char *p;
155 char *tmp_cwd;
156 char *arg;
157 int ival;
158 char *str;
159
160 if ((tmp_cwd = getcwd(NULL, 0)) == NULL) {
161 tst_resm(TBROK, "Could not getcwd()");
162 return -1;
163 }
164
165 arg = strdup( tmp_cwd );
166
167 if (( arg = strdup( tmp_cwd )) == NULL) {
168 tst_resm(TBROK, "Could not produce self_exec string");
169 return -1;
170 }
171
172 va_start(ap, fmt);
173
174 for (p = fmt; *p; p++) {
175 switch (*p) {
176 case 'd':
177 case 'n':
178 ival = va_arg(ap, int);
179 if (asprintf(&arg, "%s,%d", arg, ival) < 0) {
180 tst_resm(TBROK, "Could not produce self_exec string");
181 return -1;
182 }
183 break;
184 case 's':
185 case 'S':
186 str = va_arg(ap, char *);
187 if (asprintf(&arg, "%s,%s", arg, str) < 0) {
188 tst_resm(TBROK, "Could not produce self_exec string");
189 return -1;
190 }
191 break;
192 default:
193 tst_resm(TBROK, "Format string option %c not implemented", *p);
194 return -1;
195 break;
196 }
197 }
198
199 va_end(ap);
200
201 if (chdir(start_cwd) < 0) {
202 tst_resm(TBROK, "Could not change to %s for self_exec", start_cwd);
203 return -1;
204 }
205
206 return execlp(argv0, argv0, "-C", arg, (char *) NULL);
207}
208
Markos Chandras8d2d8ba2012-01-03 10:40:50 +0000209#endif /* UCLINUX */