Home | History | Annotate | Line # | Download | only in dist
      1 // Copyright 2012 Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      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 the
     12 //   documentation and/or other materials provided with the distribution.
     13 // * Neither the name of Google Inc. nor the names of its contributors
     14 //   may be used to endorse or promote products derived from this software
     15 //   without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 /// \file run.h
     30 /// Structures and functions to run a program in an isolated manner.
     31 ///
     32 /// Pretty much all the functions of this module (ab)use global variables to
     33 /// store their state.  This means that they are not reentrant, but this is by
     34 /// design: the whole point of implementing standalone testers is to simplify
     35 /// the writing of this code by being able to make such assumptions.
     36 
     37 #if !defined(KYUA_RUN_H)
     38 #define KYUA_RUN_H
     39 
     40 #include <stdbool.h>
     41 #include <unistd.h>
     42 
     43 #include "defs.h"
     44 #include "error_fwd.h"
     45 #include "run_fwd.h"
     46 
     47 
     48 extern const char* kyua_run_tmpdir;
     49 
     50 
     51 /// Parameters that indicate how to isolate a subprocess.
     52 ///
     53 /// In the name of simplicity, the fields of this structure can be considered
     54 /// public.
     55 struct kyua_run_params {
     56     /// Execution deadline for the subprocess.
     57     unsigned long timeout_seconds;
     58 
     59     /// Work directory for the subprocess to use.
     60     const char* work_directory;
     61 
     62     /// UID of the user to switch to before running the subprocess.
     63     uid_t unprivileged_user;
     64 
     65     /// GID of the group to switch to before running the subprocess.
     66     gid_t unprivileged_group;
     67 };
     68 
     69 
     70 void kyua_run_params_init(kyua_run_params_t*);
     71 
     72 
     73 void kyua_run_exec(const char*, const char* const*) KYUA_DEFS_NORETURN;
     74 
     75 
     76 kyua_error_t kyua_run_fork(const kyua_run_params_t*, pid_t* const);
     77 kyua_error_t kyua_run_wait(const pid_t, int*, bool*);
     78 
     79 
     80 kyua_error_t kyua_run_work_directory_enter(const char*, const uid_t,
     81                                            const gid_t, char**);
     82 kyua_error_t kyua_run_work_directory_leave(char**);
     83 
     84 
     85 #endif  // !defined(KYUA_RUN_H)
     86