-adding missing break statements
[oweals/gnunet.git] / src / include / gnunet_helper_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 Christian Grothoff
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_helper_lib.h
23  * @brief API for dealing with (SUID) helper processes that communicate via
24  *          GNUNET_MessageHeaders on stdin/stdout
25  * @author Philipp Toelke
26  * @author Christian Grothoff
27  */
28
29 #ifndef GNUNET_HELPER_LIB_H
30 #define GNUNET_HELPER_LIB_H
31
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_server_lib.h"
34
35 /**
36  * The handle to a helper process.
37  */
38 struct GNUNET_HELPER_Handle;
39
40
41 /**
42  * Callback that will be called when the helper process dies. This is not called
43  * when the helper process is stoped using GNUNET_HELPER_stop()
44  *
45  * @param cls the closure from GNUNET_HELPER_start()
46  */
47 typedef void (*GNUNET_HELPER_ExceptionCallback) (void *cls);
48
49
50 /**
51  * Starts a helper and begins reading from it. The helper process is
52  * restarted when it dies except when it is stopped using GNUNET_HELPER_stop()
53  * or when the exp_cb callback is not NULL.
54  *
55  * @param with_control_pipe does the helper support the use of a control pipe for signalling?
56  * @param binary_name name of the binary to run
57  * @param binary_argv NULL-terminated list of arguments to give when starting the binary (this
58  *                    argument must not be modified by the client for
59  *                     the lifetime of the helper handle)
60  * @param cb function to call if we get messages from the helper
61  * @param exp_cb the exception callback to call. Set this to NULL if the helper
62  *          process has to be restarted automatically when it dies/crashes
63  * @param cb_cls closure for the above callbacks
64  * @return the new Handle, NULL on error
65  */
66 struct GNUNET_HELPER_Handle *
67 GNUNET_HELPER_start (int with_control_pipe,
68                      const char *binary_name,
69                      char *const binary_argv[],
70                      GNUNET_SERVER_MessageTokenizerCallback cb,
71                      GNUNET_HELPER_ExceptionCallback exp_cb,
72                      void *cb_cls);
73
74
75 /**
76  * Sends termination signal to the helper process.  The helper process is not
77  * reaped; call GNUNET_HELPER_wait() for reaping the dead helper process.
78  *
79  * @param h the helper handle
80  * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
81  *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
82  * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
83  */
84 int
85 GNUNET_HELPER_kill (struct GNUNET_HELPER_Handle *h, int soft_kill);
86
87
88 /**
89  * Reap the helper process.  This call is blocking (!).  The helper process
90  * should either be sent a termination signal before or should be dead before
91  * calling this function
92  *
93  * @param h the helper handle
94  * @return #GNUNET_OK on success; #GNUNET_SYSERR on error
95  */
96 int
97 GNUNET_HELPER_wait (struct GNUNET_HELPER_Handle *h);
98
99
100 /**
101  * Free's the resources occupied by the helper handle
102  *
103  * @param h the helper handle to free
104  */
105 void
106 GNUNET_HELPER_destroy (struct GNUNET_HELPER_Handle *h);
107
108
109 /**
110  * Kills the helper, closes the pipe, frees the handle and calls wait() on the
111  * helper process
112  *
113  * @param h handle to helper to stop
114  * @param soft_kill if #GNUNET_YES, signals termination by closing the helper's
115  *          stdin; #GNUNET_NO to signal termination by sending SIGTERM to helper
116  */
117 void
118 GNUNET_HELPER_stop (struct GNUNET_HELPER_Handle *h, int soft_kill);
119
120
121 /**
122  * Continuation function.
123  *
124  * @param cls closure
125  * @param result #GNUNET_OK on success,
126  *               #GNUNET_NO if helper process died
127  *               #GNUNET_SYSERR during GNUNET_HELPER_destroy
128  */
129 typedef void (*GNUNET_HELPER_Continuation)(void *cls,
130                                            int result);
131
132
133 /**
134  * Handle to cancel 'send'
135  */
136 struct GNUNET_HELPER_SendHandle;
137
138
139 /**
140  * Send an message to the helper.
141  *
142  * @param h helper to send message to
143  * @param msg message to send
144  * @param can_drop can the message be dropped if there is already one in the queue?
145  * @param cont continuation to run once the message is out (#GNUNET_OK on succees, #GNUNET_NO
146  *             if the helper process died, #GNUNET_SYSERR during #GNUNET_HELPER_destroy).
147  * @param cont_cls closure for @a cont
148  * @return NULL if the message was dropped,
149  *         otherwise handle to cancel @a cont (actual transmission may
150  *         not be abortable)
151  */
152 struct GNUNET_HELPER_SendHandle *
153 GNUNET_HELPER_send (struct GNUNET_HELPER_Handle *h,
154                     const struct GNUNET_MessageHeader *msg,
155                     int can_drop,
156                     GNUNET_HELPER_Continuation cont,
157                     void *cont_cls);
158
159
160 /**
161  * Cancel a #GNUNET_HELPER_send operation.  If possible, transmitting
162  * the message is also aborted, but at least 'cont' won't be called.
163  *
164  * @param sh operation to cancel
165  */
166 void
167 GNUNET_HELPER_send_cancel (struct GNUNET_HELPER_SendHandle *sh);
168
169 #endif
170 /* end of include guard: GNUNET_HELPER_LIB_H */