Blame tests/child.c

Packit 086201
/*
Packit 086201
 * Copyright 2011 Red Hat, Inc.
Packit 086201
 *
Packit 086201
 * Permission is hereby granted, free of charge, to any person
Packit 086201
 * obtaining a copy of this software and associated documentation files
Packit 086201
 * (the "Software"), to deal in the Software without restriction,
Packit 086201
 * including without limitation the rights to use, copy, modify, merge,
Packit 086201
 * publish, distribute, sublicense, and/or sell copies of the Software,
Packit 086201
 * and to permit persons to whom the Software is furnished to do so,
Packit 086201
 * subject to the following conditions:
Packit 086201
 *
Packit 086201
 * The above copyright notice and this permission notice shall be
Packit 086201
 * included in all copies or substantial portions of the Software.
Packit 086201
 *
Packit 086201
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 086201
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 086201
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 086201
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 086201
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 086201
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 086201
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 086201
 * SOFTWARE.
Packit 086201
 */
Packit 086201
Packit 086201
#include <sys/types.h>
Packit 086201
#include <sys/wait.h>
Packit 086201
Packit 086201
#include <verto-module.h>
Packit 086201
#include "test.h"
Packit 086201
Packit 086201
#define EXITCODE 17
Packit 086201
Packit 086201
static int exitstatus;
Packit 086201
static int freed;
Packit 086201
Packit 086201
void
Packit 086201
exit_cb(verto_ctx *ctx, verto_ev *ev)
Packit 086201
{
Packit 086201
    (void) ctx;
Packit 086201
    (void) ev;
Packit 086201
Packit 086201
    if (WEXITSTATUS(exitstatus) != EXITCODE) {
Packit 086201
        printf("ERROR: Child event never fired!\n");
Packit 086201
        retval = 1;
Packit 086201
    }
Packit 086201
    if (!freed) {
Packit 086201
        printf("ERROR: On free never fired!\n");
Packit 086201
        retval = 1;
Packit 086201
    }
Packit 086201
Packit 086201
    verto_break(ctx);
Packit 086201
}
Packit 086201
Packit 086201
void
Packit 086201
onfree(verto_ctx *ctx, verto_ev *ev)
Packit 086201
{
Packit 086201
    (void) ctx;
Packit 086201
    (void) ev;
Packit 086201
Packit 086201
    freed = 1;
Packit 086201
}
Packit 086201
Packit 086201
void
Packit 086201
cb(verto_ctx *ctx, verto_ev *ev)
Packit 086201
{
Packit 086201
    (void) ctx;
Packit 086201
    (void) ev;
Packit 086201
Packit 086201
    exitstatus = verto_get_proc_status(ev);
Packit 086201
}
Packit 086201
Packit 086201
int
Packit 086201
do_test(verto_ctx *ctx)
Packit 086201
{
Packit 086201
    pid_t pid;
Packit 086201
    verto_ev *ev;
Packit 086201
    exitstatus = 0;
Packit 086201
    freed = 0;
Packit 086201
Packit 086201
    if (!(verto_get_supported_types(ctx) & VERTO_EV_TYPE_CHILD)) {
Packit 086201
        printf("WARNING: Child not supported!\n");
Packit 086201
        verto_break(ctx);
Packit 086201
        return 0;
Packit 086201
    }
Packit 086201
Packit 086201
    pid = fork();
Packit 086201
    if (pid < 0)
Packit 086201
        return 1;
Packit 086201
    else if (pid == 0) {
Packit 086201
        usleep(50000); /* 0.05 seconds */
Packit 086201
        exit(EXITCODE);
Packit 086201
    }
Packit 086201
Packit 086201
    /* Persist makes no sense for children events */
Packit 086201
    assert(!verto_add_child(ctx, VERTO_EV_FLAG_PERSIST, cb, pid));
Packit 086201
    assert(verto_add_timeout(ctx, VERTO_EV_FLAG_NONE, exit_cb, 1000));
Packit 086201
    ev = verto_add_child(ctx, VERTO_EV_FLAG_NONE, cb, pid);
Packit 086201
    assert(ev);
Packit 086201
    verto_set_private(ev, NULL, onfree);
Packit 086201
Packit 086201
    return 0;
Packit 086201
}