1 /* ===========================================================================
2  *
3  *                            PUBLIC DOMAIN NOTICE
4  *               National Center for Biotechnology Information
5  *
6  *  This software/database is a "United States Government Work" under the
7  *  terms of the United States Copyright Act.  It was written as part of
8  *  the author's official duties as a United States Government employee and
9  *  thus cannot be copyrighted.  This software/database is freely available
10  *  to the public for use. The National Library of Medicine and the U.S.
11  *  Government have not placed any restriction on its use or reproduction.
12  *
13  *  Although all reasonable efforts have been taken to ensure the accuracy
14  *  and reliability of the software and data, the NLM and the U.S.
15  *  Government do not and cannot warrant the performance or results that
16  *  may be obtained by using this software or data. The NLM and the U.S.
17  *  Government disclaim all warranties, express or implied, including
18  *  warranties of performance, merchantability or fitness for any particular
19  *  purpose.
20  *
21  *  Please cite the author in any work or product based on this material.
22  *
23  * ===========================================================================
24  *
25  * Project:
26  *  sratools command line tool
27  *
28  * Purpose:
29  *  process stuff, like fork and exec
30  *
31  */
32 
33 #pragma once
34 #include <string>
35 #include <vector>
36 #include <map>
37 
38 #include "parse_args.hpp"
39 #include "util.hpp"
40 
41 typedef int pid_t;
42 
43 namespace sratools {
44 
45 struct process {
46     pid_t pid;
47 
is_selfsratools::process48     bool is_self() const { return pid == 0; }
get_pidsratools::process49     pid_t get_pid() const { return pid; }
50 
51     /// @brief the result of wait if child did terminate in some way
52     struct exit_status {
53         /// @brief does nothing; not really applicable to Windows; child processes always only exit
54         ///
55         /// @return true
exitedsratools::process::exit_status56         bool exited() const {
57             return true;
58         }
59         /// @brief child exit code
exit_codesratools::process::exit_status60         int exit_code() const {
61             assert(exited());
62             return value;
63         }
64 
65         /// @brief does nothing; not applicable to Windows; only for API compatibility with POSIX
66         ///
67         /// @return false
signaledsratools::process::exit_status68         bool signaled() const {
69             return false;
70         }
71         /// @brief does nothing; not applicable to Windows; only for API compatibility with POSIX
72         ///
73         /// @return 0
termsigsratools::process::exit_status74         int termsig() const {
75             assert(signaled());
76             return 0;
77         }
78         /// @brief does nothing; not applicable to Windows; only for API compatibility with POSIX
79         ///
80         /// @return nullptr
termsignamesratools::process::exit_status81         char const *termsigname() const {
82             assert(signaled());
83             return nullptr;
84         }
85         /// @brief does nothing; not applicable to Windows; only for API compatibility with POSIX
86         ///
87         /// @return false
coredumpsratools::process::exit_status88         bool coredump() const {
89             assert(signaled());
90             return false;
91         }
92 
93         /// @brief does nothing; not applicable to Windows; only for API compatibility with POSIX
stoppedsratools::process::exit_status94         bool stopped() const {
95             return false;
96         }
97         /// @brief does nothing; not applicable to Windows; only for API compatibility with POSIX
98         ///
99         /// @return 0
stopsigsratools::process::exit_status100         int stopsig() const {
101             assert(stopped());
102             return 0;
103         }
104 
105         /// @brief did child exit(0)
normalsratools::process::exit_status106         bool normal() const {
107             return exited() && exit_code() == 0;
108         }
operator boolsratools::process::exit_status109         operator bool() const {
110             return normal();
111         }
112 
exit_statussratools::process::exit_status113         exit_status(exit_status const &other) : value(other.value) {}
operator =sratools::process::exit_status114         exit_status &operator =(exit_status const &other) {
115             value = other.value;
116             return *this;
117         }
118     private:
119         int value;
120         friend struct process;
exit_statussratools::process::exit_status121         exit_status(int status) : value(status) {}
122     };
123 
124     static void run_child(char const *toolpath, char const *toolname, char const **argv, Dictionary const &env = {});
125     static exit_status run_child_and_wait(char const *toolpath, char const *toolname, char const **argv, Dictionary const &env = {});
126     static exit_status run_child_and_get_stdout(std::string *out, char const *toolpath, char const *toolname, char const **argv, bool const for_real = false, Dictionary const &env = {});
127 };
128 
129 } // namespace sratools
130