Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <errno.h>
     30 #include <stdbool.h>
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 #include <sys/resource.h>
     35 #include <sys/wait.h>
     36 #include <unistd.h>
     37 
     38 #define CHILD_EXIT_CODE  111
     39 
     40 typedef int (*wait_call_function)(pid_t child_pid);
     41 
     42 static bool check_wait_call(const char *title, wait_call_function wait_func,
     43                             int expected_exit_code) {
     44   printf("Testing %s(): ", title);
     45   int cpid = fork();
     46   if (cpid < 0) {
     47     fprintf(stderr, "ERROR: fork() failed: %s\n", strerror(errno));
     48     return false;
     49   }
     50 
     51   if (cpid == 0) {  /* in the chid process */
     52     printf("Child created pid=%d parent_pid=%d\n", getpid(), getppid());
     53     exit(expected_exit_code);
     54   }
     55 
     56   /* in the parent process */
     57   printf("Parent waiting for child with pid=%d\n", cpid);
     58   int exit_code = wait_func(cpid);
     59   if (exit_code < 0)
     60     return false;
     61 
     62   if (exit_code != expected_exit_code) {
     63     fprintf(stderr, "ERROR: Child exited with code %d, expected %d\n",
     64             exit_code, expected_exit_code);
     65     return false;
     66   }
     67   printf("Testing %s(): OK\n", title);
     68   return true;
     69 }
     70 
     71 // To be called by check_wait_call() to check wait().
     72 static int check_wait(pid_t child_pid) {
     73   int status = 0;
     74   pid_t ret = wait(&status);
     75   if (ret != child_pid) {
     76     fprintf(stderr, "ERROR: wait() returned %d, expected %d\n", ret, child_pid);
     77     return -1;
     78   }
     79   return WEXITSTATUS(status);
     80 }
     81 
     82 // To be called by check_wait_call() to check waitpid()
     83 static int check_waitpid(pid_t child_pid) {
     84   int status = 0;
     85   pid_t ret = waitpid((pid_t)-1, &status, 0);
     86   if (ret != child_pid) {
     87     fprintf(stderr, "ERROR: waitpid() returned %d, expected %d\n", ret, child_pid);
     88     return -1;
     89   }
     90   return WEXITSTATUS(status);
     91 }
     92 
     93 // To be called by check_wait_call() to check wait3()
     94 static int check_wait4(pid_t child_pid) {
     95   int status = 0;
     96   struct rusage ru;
     97   pid_t ret = wait4(-1, &status, 0, &ru);
     98   if (ret != child_pid) {
     99     fprintf(stderr, "ERROR: wait4() returned %d, expected %d\n", ret, child_pid);
    100     return -1;
    101   }
    102   return WEXITSTATUS(status);
    103 }
    104 
    105 int main(int argc, char *argv[]) {
    106   if (!check_wait_call("wait", check_wait, CHILD_EXIT_CODE + 0)) {
    107     return EXIT_FAILURE;
    108   }
    109   if (!check_wait_call("waitpid", check_waitpid, CHILD_EXIT_CODE + 1)) {
    110     return EXIT_FAILURE;
    111   }
    112   if (!check_wait_call("wait4", check_wait4, CHILD_EXIT_CODE + 3)) {
    113     return EXIT_FAILURE;
    114   }
    115 
    116   return EXIT_SUCCESS;
    117 }
    118 
    119