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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/*
SSSD
Stress tests
Copyright (C) Jakub Hrozek <jhrozek@redhat.com> 2009
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <signal.h>
#include <stdlib.h>
#include <talloc.h>
#include <popt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include "util/util.h"
#include "tests/common.h"
#define DEFAULT_START 10
#define DEFAULT_STOP 20
#define NAME_SIZE 255
#define CHUNK 64
/* How many tests failed */
int failure_count;
/* Be chatty */
int verbose;
/*
* Look up one user. If the user is not found using getpwnam, the success
* or failure depends on enoent_fail being set.
*/
int test_lookup_user(const char *name, int enoent_fail)
{
struct passwd *pwd = NULL;
int ret = 0;
int error;
errno = 0;
pwd = getpwnam(name);
error = errno;
if (pwd == NULL) {
if (error == 0 || error == ENOENT) {
ret = (enoent_fail == 1) ? ENOENT : 0;
}
}
if (ret != 0 && verbose) {
fprintf(stderr,
"getpwnam failed (name: %s): errno = %d, error = %s\n",
name, ret, strerror(ret));
}
return ret;
}
/*
* Look up one group. If the user is not found using getgrnam, the success
* or failure depends on enoent_fail being set.
*/
int test_lookup_group(const char *name, int enoent_fail)
{
struct group *grp = NULL;
int ret = 0;
errno = 0;
grp = getgrnam(name);
if (grp == NULL) {
if (errno == 0 || errno == ENOENT) {
ret = enoent_fail ? ENOENT : 0;
}
}
if (ret != 0 && verbose) {
fprintf(stderr,
"getgrnam failed (name %s): errno = %d, error = %s\n",
name, ret, strerror(ret));
}
return ret;
}
int run_one_testcase(const char *name, int group, int enoent_fail)
{
if (group) {
return test_lookup_group(name, enoent_fail);
} else {
return test_lookup_user(name, enoent_fail);
}
}
/*
* Beware, has side-effects: changes global variable failure_count
*/
void child_handler(int signum)
{
int status, ret;
while ((ret = wait(&status)) > 0) {
if (ret == -1) {
perror("wait");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
if (ret) {
if (verbose) {
fprintf(stderr,
"A child exited with error code %d\n",
WEXITSTATUS(status));
}
++failure_count;
}
} else ++failure_count;
}
}
int generate_names(TALLOC_CTX *mem_ctx, const char *prefix,
int start, int stop, char ***_out)
{
char **out;
int num_names = stop-start+1;
int idx = 0;
out = talloc_array(mem_ctx, char *, num_names+1);
if (out == NULL) {
return ENOMEM;
}
for (idx = 0; idx < num_names; ++idx) {
out[idx] = talloc_asprintf(mem_ctx, "%s%d", prefix, idx);
if (out[idx] == NULL) {
return ENOMEM;
}
}
out[idx] = NULL;
*_out = out;
return EOK;
}
int read_names(TALLOC_CTX *mem_ctx, FILE *stream, char ***_out)
{
char one_name[NAME_SIZE];
int n = 0;
int array_size = CHUNK;
int ret;
char **out;
out = talloc_array(mem_ctx, char *, CHUNK+1);
if (out == NULL) {
return ENOMEM;
}
while (fgets(one_name, NAME_SIZE, stream)) {
out[n] = talloc_strdup(mem_ctx, one_name);
if (out[n] == NULL) {
return ENOMEM;
}
if ((n++ % CHUNK) == 0) {
array_size += CHUNK;
out = talloc_realloc(mem_ctx, out, char *, array_size);
if (out == NULL) {
return ENOMEM;
}
}
}
if ((ret = ferror(stream))) {
return ret;
}
out[n] = NULL;
*_out = out;
return EOK;
}
int main(int argc, const char *argv[])
{
int opt;
poptContext pc;
int pc_start=DEFAULT_START;
int pc_stop=DEFAULT_STOP;
int pc_enoent_fail=0;
int pc_groups=0;
int pc_verbosity = 0;
char *pc_prefix = NULL;
TALLOC_CTX *ctx = NULL;
char **names = NULL;
int status, idx, ret;
pid_t pid;
struct sigaction action, old_action;
struct poptOption long_options[] = {
POPT_AUTOHELP
{ "groups", 'g', POPT_ARG_NONE, &pc_groups, 0,
"Lookup in groups instead of users", NULL },
{ "prefix", '\0', POPT_ARG_STRING, &pc_prefix, 0,
"The username prefix", NULL },
{ "start", '\0', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT,
&pc_start, 0,
"Start value to append to prefix", NULL },
{ "stop", '\0', POPT_ARG_INT | POPT_ARGFLAG_SHOW_DEFAULT,
&pc_stop, 0,
"End value to append to prefix", NULL },
{ "enoent-fail", '\0', POPT_ARG_NONE, &pc_enoent_fail, 0,
"Fail on not getting the requested NSS data (default: No)",
NULL },
{ "verbose", 'v', POPT_ARG_NONE, 0, 'v',
"Be verbose", NULL },
POPT_TABLEEND
};
/* parse the params */
pc = poptGetContext(argv[0], argc, argv, long_options, 0);
while ((opt = poptGetNextOpt(pc)) != -1) {
switch (opt) {
case 'v':
pc_verbosity = 1;
break;
default:
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
return 1;
}
}
poptFreeContext(pc);
tests_set_cwd();
verbose = pc_verbosity;
if (pc_prefix) {
ret = generate_names(ctx, pc_prefix, pc_start, pc_stop, &names);
if (ret != EOK) {
if (verbose) {
errno = ret;
perror("generate_names");
}
exit(EXIT_FAILURE);
}
} else {
ret = read_names(ctx, stdin, &names);
if (ret != EOK) {
if (verbose) {
errno = ret;
perror("read_names");
}
exit(EXIT_FAILURE);
}
}
/* Reap the children in a handler asynchronously so we can
* somehow protect against too many processes */
memset(&action, 0, sizeof(action));
action.sa_handler = child_handler;
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, SIGCHLD);
action.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &action, &old_action);
/* Fire up the child processes */
idx = 0;
for (idx=0; names[idx]; idx++) {
pid = fork();
if (pid == -1) {
/* Try again in hope that some child has exited */
if (errno == EAGAIN) {
continue;
}
perror("fork");
exit(EXIT_FAILURE);
} else if ( pid == 0 ) {
/* child */
ret = run_one_testcase(names[idx], pc_groups, pc_enoent_fail);
exit(ret);
}
}
/* Process the rest of the children here in main */
sigaction(SIGCHLD, &old_action, NULL);
while ((ret = wait(&status)) > 0) {
if (ret == -1) {
perror("wait");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
if (ret) {
if (verbose) {
fprintf(stderr,
"A child exited with error code %d\n",
WEXITSTATUS(status));
}
++failure_count;
}
} else ++failure_count;
}
if (pc_verbosity) {
fprintf(stderr,
"Total tests run: %d\nPassed: %d\nFailed: %d\n",
idx,
idx - failure_count,
failure_count);
}
return (failure_count==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
|