zuluCrypt  5.7.1
About: zuluCrypt is a simple but feature rich solution for hard drives encryption. It can manage PLAIN dm-crypt, LUKS, TrueCrypt and VeraCrypt encrypted volumes.
  Fossies Dox: zuluCrypt-5.7.1.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

process.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2012-2015
4  * name : Francis Banyikwa
5  * email: mhogomchungu@gmail.com
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef PROCESS
21 #define PROCESS
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <sys/types.h>
28 
29 typedef struct{
30  /*
31  * if this variable is set,then it is expect to be in the same format the last argument of execv() expect
32  */
33  char * const * args ;
34  /*
35  * this variable is set will cause the program to be executed will run with the identify of this user.
36  */
37  uid_t user_id ;
38  /*
39  * If this variable is set,then it is expected to be in the format the last argument of execve() expect
40  */
41  char * const * env ;
42  /*
43  * If this variable is set,the process will be terminate if its still running after timeout seconds
44  * The default signal sent is sigterm
45  */
46  int timeout ;
47  /*
48  * if this variable is set,then this signal will be sent when the timeout above expires
49  */
50  int signal ;
51  /*
52  * priority of forked process will run with
53  */
54  int priority ;
56 
57 typedef struct ProcessType_t * process_t ;
58 
59 #define ProcessVoid ( ( process_t ) 0 )
60 
61 /*
62  * Examples on how to use the library are at the end of this header file
63  */
64 
65 /*
66  * Takes a pointer to a function to be called when memory allocation can not take place
67  * ie if the system has run out of memory and malloc() or realloc() has failed.
68  * This function is optional and "ProcessVoid" will be returned on memory exaustion if the function
69  * isnt set.
70  */
71 void ProcessExitOnMemoryExaustion( void (*)( void ) ) ;
72 
73 /*
74  * create a handle to a process that will be started.
75  * Path to executable to be started must be in full path format
76  */
77 process_t Process( const char * path,... ) __attribute__ ( ( sentinel ) ) ;
78 
79 /*
80  * run exe process with its NULL terminated arguments and then return the
81  * process' exit status
82  */
83 int ProcessExecute( const char * exe,... ) __attribute__ ( ( sentinel ) ) ;
84 
85 /*
86  * This structure can be used an an alternative way to set various options.
87  * There exist a separate function to set each one of them
88  */
90 
91 /*
92  * call fork() and then exec and run a program specified when handle p was created.
93  * return value: pid of the child process
94  */
95 pid_t ProcessStart( process_t p ) ;
96 
97 /*
98  * pass data to the child process,the child process will get the data from reading its stdin.
99  * ProcessSetOption( p,WRITE_STD_IN ) must be called first for this to work. *
100  */
101 size_t ProcessWrite( process_t p,const char * data,size_t len ) ;
102 
103 /*
104  * close the writing connection to the child process.This maybe necessary if a child process
105  * blocks waiting for more data in its std in until EOF is received.
106  */
108 
109 /*
110  * send a forked process sigterm to terminate it.
111  */
113 
114 /*
115  * run the child process with privileges of a user with UID of uid
116  */
117 void ProcessSetOptionUser( process_t,uid_t uid ) ;
118 
119 /*
120  * set the nice value the forked process will run with
121  */
122 void ProcessSetOptionPriority( process_t,int priority ) ;
123 
124 /*
125  * send a forked process sigkill to kill it
126  */
127 int ProcessKill( process_t ) ;
128 
129 /*
130  * the forked process will be started with arguments list given by the second argument
131  * look at the example at the end of this header file for info on how to use the API
132  */
133 void ProcessSetArguments( process_t p,char * const argv[] ) ;
134 
135 /*
136  * look at the example at the end of this header file for info on how to use the API
137  */
138 void ProcessSetArgumentList( process_t p,... ) __attribute__ ( ( sentinel ) ) ;
139 
140 /*
141  * set the child process to start with the given enviromental variables
142  */
143 void ProcessSetEnvironmentalVariable( process_t p,char * const * env ) ;
144 
145 /*
146  * get state of the process handled by handle p
147  */
148 typedef enum{
155 
157 
158 /*
159  * wait for "timeout" seconds and then send the forked process a signal "signal"
160  */
161 void ProcessSetOptionTimeout( process_t p,int timeout,int signal ) ;
162 
163 /*
164  * block waiting for the forked process to exit and then get its exit status.
165  */
167 
168 /*
169  * block waiting for the forked process to exit.
170  */
171 void ProcessWait( process_t ) ;
172 
173 /*
174  * block waiting for the forked process to exit and then get its exit status and then clean up
175  * used resources.
176  *
177  * Use this function if you have to wait for the forked process to finish so that you can
178  * get its exit status.
179  */
181 
182 /*
183  * clean up used resources and dont wait for the forked process.
184  * Use this function if you dont want to wait for the exit status or if the forked
185  * process is meant to live past the life of the parent process.
186  */
187 void ProcessCleanUp( process_t * ) ;
188 
189 /*
190  * get contents of std out/std error from the process.
191  * remember to free() the return buffer when done with it.
192  *
193  * return value is the size of the data.
194  * this function must be called after ProcessStart()
195  */
196 
197 typedef enum{
201 }ProcessIO;
202 
203 size_t ProcessGetOutPut( process_t,char ** data,ProcessIO ) ;
204 
205 /*
206  * read size number of bytes from the ourput of the forket process.
207  */
208 ssize_t ProcessGetOutPut_1( process_t,char * buffer,int size,ProcessIO ) ;
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif
215 
216 /*
217  * Example use case of ProcessSetArgumentList API
218  */
219 #if 0
220 
221 #include "process.h"
222 #include <stdio.h>
223 #include <stdlib.h>
224 
225 int main( void )
226 {
227  process_t p = Process( "/bin/ls","-l",NULL ) ;
228  ProcessStart( p ) ;
229  char * c = NULL ;
230  while( 1 ){
232  if( c ){
233  printf( "%s",c ) ;
234  free( c ) ;
235  c = NULL ;
236  }else{
237  break ;
238  }
239  }
241  return 0 ;
242 }
243 
244 #endif
245 
246 /*
247  * Example use case of ProcessSetArguments API
248  */
249 #if 0
250 
251 #include "process.h"
252 #include <stdio.h>
253 #include <stdlib.h>
254 
255 int main( void )
256 {
257  const char * argv[ 3 ] ;
258  process_t p = Process( NULL,NULL ) ;
259  argv[ 0 ] = "/bin/ls" ;
260  argv[ 1 ] = "-l" ;
261  argv[ 2 ] = NULL ;
262  ProcessSetArguments( p,argv ) ;
263  ProcessStart( p ) ;
264  char * c = NULL ;
265  while( 1 ){
267  if( c ){
268  printf( "%s",c ) ;
269  free( c ) ;
270  c = NULL ;
271  }else{
272  break ;
273  }
274  }
276  return 0 ;
277 }
278 
279 #endif
280 
281 
282 /*
283  * Example use case of ProcessArgumentStructure API
284  */
285 #if 0
286 
287 #include "process.h"
288 #include <stdio.h>
289 #include <stdlib.h>
290 
291 int main( void )
292 {
293  const char * argv[ 3 ] ;
294  process_t p = Process( NULL,NULL ) ;
295  argv[ 0 ] = "/bin/ls" ;
296  argv[ 1 ] = "-l" ;
297  argv[ 2 ] = NULL ;
299  str->args = argv ;
300  ProcessStart( p ) ;
301  char * c = NULL ;
302  while( 1 ){
304  if( c ){
305  printf( "%s",c ) ;
306  free( c ) ;
307  c = NULL ;
308  }else{
309  break ;
310  }
311  }
313  return 0 ;
314 }
315 
316 #endif
ProcessStructure::timeout
int timeout
Definition: process.h:46
Process
process_t Process(const char *path,...)
Definition: process.c:185
ProcessStructure::signal
int signal
Definition: process.h:50
ProcessStructure::env
char *const * env
Definition: process.h:41
ProcessGetOutPut_1
ssize_t ProcessGetOutPut_1(process_t, char *buffer, int size, ProcessIO)
Definition: process.c:510
ProcessWait
void ProcessWait(process_t)
Definition: process.c:654
ProcessSetOptionPriority
void ProcessSetOptionPriority(process_t, int priority)
Definition: process.c:325
ProcessCleanUp
void ProcessCleanUp(process_t *)
Definition: process.c:570
ProcessCloseStdWrite
void ProcessCloseStdWrite(process_t p)
Definition: process.c:532
ProcessGetOutPut
size_t ProcessGetOutPut(process_t, char **data, ProcessIO)
Definition: process.c:438
ProcessWaitUntilFinished
int ProcessWaitUntilFinished(process_t *)
Definition: process.c:614
ProcessType_t::exe
char * exe
Definition: process.c:48
ProcessArgumentStructure
ProcessStructure * ProcessArgumentStructure(process_t)
Definition: process.c:73
ProcessStatusUndefined
@ ProcessStatusUndefined
Definition: process.h:153
ProcessExecute
int ProcessExecute(const char *exe,...)
Definition: process.c:242
ProcessStdError
@ ProcessStdError
Definition: process.h:200
ProcessSetArguments
void ProcessSetArguments(process_t p, char *const argv[])
Definition: process.c:659
ProcessIsStillRunning
@ ProcessIsStillRunning
Definition: process.h:150
ProcessStructure
Definition: process.h:29
process.h
ProcessKill
int ProcessKill(process_t)
Definition: process.c:600
ProcessType_t
Definition: process.c:41
ProcessCancelled
@ ProcessCancelled
Definition: process.h:152
ProcessStatus
ProcessStatus
Definition: process.h:148
ProcessHasNotStarted
@ ProcessHasNotStarted
Definition: process.h:149
ProcessExitOnMemoryExaustion
void ProcessExitOnMemoryExaustion(void(*)(void))
Definition: process.c:62
ProcessWrite
size_t ProcessWrite(process_t p, const char *data, size_t len)
Definition: process.c:523
ProcessSetEnvironmentalVariable
void ProcessSetEnvironmentalVariable(process_t p, char *const *env)
Definition: process.c:82
ProcessStart
pid_t ProcessStart(process_t p)
Definition: process.c:337
main
int main(int argc, char *argv[])
Definition: main.c:268
ProcessStdIn
@ ProcessStdIn
Definition: process.h:198
ProcessSetOptionUser
void ProcessSetOptionUser(process_t, uid_t uid)
Definition: process.c:593
ProcessType_t::signal
int signal
Definition: process.c:50
ProcessState
ProcessStatus ProcessState(process_t p)
Definition: process.c:501
ProcessStructure::user_id
uid_t user_id
Definition: process.h:37
ProcessSetArgumentList
void ProcessSetArgumentList(process_t p,...)
Definition: process.c:138
process_t
struct ProcessType_t * process_t
Definition: process.h:57
ProcessIO
ProcessIO
Definition: process.h:197
ProcessSetOptionTimeout
void ProcessSetOptionTimeout(process_t p, int timeout, int signal)
Definition: process.c:538
ProcessCompleted
@ ProcessCompleted
Definition: process.h:151
ProcessExitStatus
int ProcessExitStatus(process_t)
Definition: process.c:634
ProcessStructure::args
char *const * args
Definition: process.h:33
ProcessStructure::priority
int priority
Definition: process.h:54
ProcessStdOut
@ ProcessStdOut
Definition: process.h:199
ProcessType_t::str
ProcessStructure str
Definition: process.c:53
ProcessTerminate
int ProcessTerminate(process_t)
Definition: process.c:578