1272343Sngie/* $NetBSD: t_exit.c,v 1.1 2011/05/09 07:31:51 jruoho Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Jukka Ruohonen. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie#include <sys/cdefs.h> 32272343Sngie__RCSID("$NetBSD: t_exit.c,v 1.1 2011/05/09 07:31:51 jruoho Exp $"); 33272343Sngie 34272343Sngie#include <sys/wait.h> 35272343Sngie 36272343Sngie#include <atf-c.h> 37272343Sngie#include <stdio.h> 38272343Sngie#include <stdlib.h> 39272343Sngie#include <string.h> 40272343Sngie#include <unistd.h> 41272343Sngie 42272343Sngiestatic bool fail; 43272343Sngiestatic void func(void); 44272343Sngie 45272343Sngiestatic void 46272343Sngiefunc(void) 47272343Sngie{ 48272343Sngie fail = false; 49272343Sngie} 50272343Sngie 51272343SngieATF_TC(exit_atexit); 52272343SngieATF_TC_HEAD(exit_atexit, tc) 53272343Sngie{ 54272343Sngie atf_tc_set_md_var(tc, "descr", "A basic test of atexit(3)"); 55272343Sngie} 56272343Sngie 57272343SngieATF_TC_BODY(exit_atexit, tc) 58272343Sngie{ 59272343Sngie pid_t pid; 60272343Sngie int sta; 61272343Sngie 62272343Sngie pid = fork(); 63272343Sngie ATF_REQUIRE(pid >= 0); 64272343Sngie 65272343Sngie if (pid == 0) { 66272343Sngie 67272343Sngie if (atexit(func) != 0) 68272343Sngie _exit(EXIT_FAILURE); 69272343Sngie 70272343Sngie fail = true; 71272343Sngie 72272343Sngie _exit(EXIT_SUCCESS); 73272343Sngie } 74272343Sngie 75272343Sngie (void)wait(&sta); 76272343Sngie 77272343Sngie if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 78272343Sngie atf_tc_fail("atexit(3) failed"); 79272343Sngie 80272343Sngie if (fail != false) 81272343Sngie atf_tc_fail("atexit(3) was not called"); 82272343Sngie} 83272343Sngie 84272343SngieATF_TC(exit_basic); 85272343SngieATF_TC_HEAD(exit_basic, tc) 86272343Sngie{ 87272343Sngie atf_tc_set_md_var(tc, "descr", "A basic test of exit(3)"); 88272343Sngie} 89272343Sngie 90272343SngieATF_TC_BODY(exit_basic, tc) 91272343Sngie{ 92272343Sngie pid_t pid; 93272343Sngie int sta; 94272343Sngie 95272343Sngie pid = fork(); 96272343Sngie ATF_REQUIRE(pid >= 0); 97272343Sngie 98272343Sngie if (pid == 0) { 99272343Sngie exit(EXIT_SUCCESS); 100272343Sngie exit(EXIT_FAILURE); 101272343Sngie } 102272343Sngie 103272343Sngie (void)wait(&sta); 104272343Sngie 105272343Sngie if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 106272343Sngie atf_tc_fail("exit(3) did not exit successfully"); 107272343Sngie} 108272343Sngie 109272343SngieATF_TC(exit_status); 110272343SngieATF_TC_HEAD(exit_status, tc) 111272343Sngie{ 112272343Sngie atf_tc_set_md_var(tc, "descr", "Test exit(3) status"); 113272343Sngie} 114272343Sngie 115272343SngieATF_TC_BODY(exit_status, tc) 116272343Sngie{ 117272343Sngie const int n = 10; 118272343Sngie int i, sta; 119272343Sngie pid_t pid; 120272343Sngie 121272343Sngie for (i = 0; i < n; i++) { 122272343Sngie 123272343Sngie pid = fork(); 124272343Sngie 125272343Sngie if (pid < 0) 126272343Sngie exit(EXIT_FAILURE); 127272343Sngie 128272343Sngie if (pid == 0) 129272343Sngie exit(i); 130272343Sngie 131272343Sngie (void)wait(&sta); 132272343Sngie 133272343Sngie if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != i) 134272343Sngie atf_tc_fail("invalid exit(3) status"); 135272343Sngie } 136272343Sngie} 137272343Sngie 138272343SngieATF_TC(exit_tmpfile); 139272343SngieATF_TC_HEAD(exit_tmpfile, tc) 140272343Sngie{ 141272343Sngie atf_tc_set_md_var(tc, "descr", "Temporary files are unlinked?"); 142272343Sngie} 143272343Sngie 144272343SngieATF_TC_BODY(exit_tmpfile, tc) 145272343Sngie{ 146272343Sngie int sta, fd = -1; 147272343Sngie char buf[12]; 148272343Sngie pid_t pid; 149272343Sngie FILE *f; 150272343Sngie 151272343Sngie (void)strlcpy(buf, "exit.XXXXXX", sizeof(buf)); 152272343Sngie 153272343Sngie pid = fork(); 154272343Sngie ATF_REQUIRE(pid >= 0); 155272343Sngie 156272343Sngie if (pid == 0) { 157272343Sngie 158272343Sngie fd = mkstemp(buf); 159272343Sngie 160272343Sngie if (fd < 0) 161272343Sngie exit(EXIT_FAILURE); 162272343Sngie 163272343Sngie exit(EXIT_SUCCESS); 164272343Sngie } 165272343Sngie 166272343Sngie (void)wait(&sta); 167272343Sngie 168272343Sngie if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 169272343Sngie atf_tc_fail("failed to create temporary file"); 170272343Sngie 171272343Sngie f = fdopen(fd, "r"); 172272343Sngie 173272343Sngie if (f != NULL) 174272343Sngie atf_tc_fail("exit(3) did not clear temporary file"); 175272343Sngie} 176272343Sngie 177272343SngieATF_TP_ADD_TCS(tp) 178272343Sngie{ 179272343Sngie 180272343Sngie ATF_TP_ADD_TC(tp, exit_atexit); 181272343Sngie ATF_TP_ADD_TC(tp, exit_basic); 182272343Sngie ATF_TP_ADD_TC(tp, exit_status); 183272343Sngie ATF_TP_ADD_TC(tp, exit_tmpfile); 184272343Sngie 185272343Sngie return atf_no_error(); 186272343Sngie} 187