vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: t -*- */ |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 2 | /* |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 3 | * self_exec.c: self_exec magic required to run child functions on uClinux |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 4 | * |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005 Paul J.Y. Lahaie <pjlahaie-at-steamballoon.com> |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 6 | * |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 7 | * 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 12 | * 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 16 | * |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 17 | * 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 20 | * |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 21 | * This software was produced by Steamballoon Incorporated |
| 22 | * 55 Byward Market Square, 2nd Floor North, Ottawa, ON K1N 9C3, Canada |
| 23 | */ |
| 24 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 25 | #define _GNU_SOURCE /* for asprintf */ |
Mike Frysinger | 28606c1 | 2010-08-17 17:22:45 -0400 | [diff] [blame] | 26 | |
| 27 | #include "config.h" |
| 28 | |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 29 | #ifdef UCLINUX |
| 30 | |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 31 | #include <stdarg.h> |
| 32 | #include <string.h> |
| 33 | #include <stdio.h> |
| 34 | #include "test.h" |
| 35 | |
| 36 | /* Set from parse_opts.c: */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 37 | char *child_args; /* Arguments to child when -C is used */ |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 38 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 39 | static char *start_cwd; /* Stores the starting directory for self_exec */ |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 40 | |
| 41 | int asprintf(char **app, const char *fmt, ...) |
| 42 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 43 | va_list ptr; |
| 44 | int rv; |
| 45 | char *p; |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 46 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 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); |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 53 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 54 | p = malloc(++rv); /* allocate the buffer */ |
| 55 | *app = p; |
| 56 | if (!p) { |
| 57 | return -1; |
| 58 | } |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 59 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | /* |
| 61 | * Second iteration - actually produce output. |
| 62 | */ |
| 63 | va_start(ptr, fmt); |
| 64 | rv = vsnprintf(p, rv, fmt, ptr); |
| 65 | va_end(ptr); |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 66 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 67 | return rv; |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Mike Frysinger | 55d8ae5 | 2014-02-13 04:11:45 -0500 | [diff] [blame] | 70 | void maybe_run_child(void (*child) (), const char *fmt, ...) |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 71 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 72 | va_list ap; |
| 73 | char *child_dir; |
| 74 | char *p, *tok; |
| 75 | int *iptr, i, j; |
| 76 | char *s; |
| 77 | char **sptr; |
| 78 | char *endptr; |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 79 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | /* Store the current directory for later use. */ |
| 81 | start_cwd = getcwd(NULL, 0); |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 82 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | if (child_args) { |
| 84 | char *args = strdup(child_args); |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 85 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | child_dir = strtok(args, ","); |
| 87 | if (strlen(child_dir) == 0) { |
| 88 | tst_brkm(TBROK, NULL, |
| 89 | "Could not get directory from -C option"); |
| 90 | } |
| 91 | |
| 92 | va_start(ap, fmt); |
| 93 | |
| 94 | for (p = fmt; *p; p++) { |
| 95 | tok = strtok(NULL, ","); |
| 96 | if (!tok || strlen(tok) == 0) { |
| 97 | tst_brkm(TBROK, NULL, |
| 98 | "Invalid argument to -C option"); |
| 99 | } |
| 100 | |
| 101 | switch (*p) { |
| 102 | case 'd': |
| 103 | iptr = va_arg(ap, int *); |
| 104 | i = strtol(tok, &endptr, 10); |
| 105 | if (*endptr != '\0') { |
| 106 | tst_brkm(TBROK, NULL, |
| 107 | "Invalid argument to -C option"); |
| 108 | } |
| 109 | *iptr = i; |
| 110 | break; |
| 111 | case 'n': |
| 112 | j = va_arg(ap, int); |
| 113 | i = strtol(tok, &endptr, 10); |
| 114 | if (*endptr != '\0') { |
| 115 | tst_brkm(TBROK, NULL, |
| 116 | "Invalid argument to -C option"); |
| 117 | } |
| 118 | if (j != i) { |
| 119 | va_end(ap); |
| 120 | return; |
| 121 | } |
| 122 | break; |
| 123 | case 's': |
| 124 | s = va_arg(ap, char *); |
| 125 | if (!strncpy(s, tok, strlen(tok) + 1)) { |
| 126 | tst_brkm(TBROK, NULL, |
| 127 | "Could not strncpy for -C option"); |
| 128 | } |
| 129 | break; |
| 130 | case 'S': |
| 131 | sptr = va_arg(ap, char **); |
| 132 | *sptr = strdup(tok); |
| 133 | if (!*sptr) { |
| 134 | tst_brkm(TBROK, NULL, |
| 135 | "Could not strdup for -C option"); |
| 136 | } |
| 137 | break; |
| 138 | default: |
| 139 | tst_brkm(TBROK, NULL, |
| 140 | "Format string option %c not implemented", |
| 141 | *p); |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | va_end(ap); |
| 147 | |
| 148 | if (chdir(child_dir) < 0) |
| 149 | tst_brkm(TBROK, NULL, |
| 150 | "Could not change to %s for child", child_dir); |
| 151 | |
| 152 | (*child) (); |
| 153 | tst_resm(TWARN, "Child function returned unexpectedly"); |
| 154 | /* Exit here? or exit silently? */ |
| 155 | } |
| 156 | } |
| 157 | |
Mike Frysinger | 55d8ae5 | 2014-02-13 04:11:45 -0500 | [diff] [blame] | 158 | int self_exec(const char *argv0, const char *fmt, ...) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | { |
| 160 | va_list ap; |
| 161 | char *p; |
| 162 | char *tmp_cwd; |
| 163 | char *arg; |
| 164 | int ival; |
| 165 | char *str; |
| 166 | |
| 167 | if ((tmp_cwd = getcwd(NULL, 0)) == NULL) { |
| 168 | tst_resm(TBROK, "Could not getcwd()"); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | arg = strdup(tmp_cwd); |
| 173 | |
| 174 | if ((arg = strdup(tmp_cwd)) == NULL) { |
| 175 | tst_resm(TBROK, "Could not produce self_exec string"); |
| 176 | return -1; |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | va_start(ap, fmt); |
| 180 | |
| 181 | for (p = fmt; *p; p++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | switch (*p) { |
| 183 | case 'd': |
| 184 | case 'n': |
| 185 | ival = va_arg(ap, int); |
| 186 | if (asprintf(&arg, "%s,%d", arg, ival) < 0) { |
| 187 | tst_resm(TBROK, |
| 188 | "Could not produce self_exec string"); |
| 189 | return -1; |
| 190 | } |
| 191 | break; |
| 192 | case 's': |
| 193 | case 'S': |
| 194 | str = va_arg(ap, char *); |
| 195 | if (asprintf(&arg, "%s,%s", arg, str) < 0) { |
| 196 | tst_resm(TBROK, |
| 197 | "Could not produce self_exec string"); |
| 198 | return -1; |
| 199 | } |
| 200 | break; |
| 201 | default: |
| 202 | tst_resm(TBROK, |
| 203 | "Format string option %c not implemented", *p); |
| 204 | return -1; |
| 205 | break; |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 206 | } |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | va_end(ap); |
| 210 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 211 | if (chdir(start_cwd) < 0) { |
| 212 | tst_resm(TBROK, "Could not change to %s for self_exec", |
| 213 | start_cwd); |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 214 | return -1; |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 215 | } |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 216 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 217 | return execlp(argv0, argv0, "-C", arg, (char *)NULL); |
vapier | b56735e | 2006-08-21 07:05:41 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Markos Chandras | 8d2d8ba | 2012-01-03 10:40:50 +0000 | [diff] [blame] | 220 | #endif /* UCLINUX */ |