luasandbox  1.4.0
Generic Lua sandbox for dynamic data analysis
sandbox.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /** Heka sandbox implementation @file */
8 
9 #ifndef luasandbox_heka_sandbox_h_
10 #define luasandbox_heka_sandbox_h_
11 
12 #include <stdbool.h>
13 #include <time.h>
14 
15 #include "../../luasandbox.h"
16 #include "../error.h"
17 #include "../util/heka_message.h"
18 
19 #ifdef _WIN32
20 #ifdef luasandboxheka_EXPORTS
21 #define LSB_HEKA_EXPORT __declspec(dllexport)
22 #else
23 #define LSB_HEKA_EXPORT __declspec(dllimport)
24 #endif
25 #else
26 #if __GNUC__ >= 4
27 #define LSB_HEKA_EXPORT __attribute__ ((visibility ("default")))
28 #else
29 #define LSB_HEKA_EXPORT
30 #endif
31 #endif
32 
33 #define LSB_HEKA_MAX_MESSAGE_SIZE "max_message_size"
34 #define LSB_HEKA_UPDATE_CHECKPOINT "update_checkpoint"
35 #define LSB_HEKA_THIS_PTR "lsb_heka_this_ptr"
36 
44 };
45 
48  LSB_HEKA_IM_ERROR = 1, // generic error for backward compatibility
51 };
52 
54 
55 typedef struct lsb_heka_stats {
56  unsigned long long mem_cur;
57  unsigned long long mem_max;
58  unsigned long long ins_max;
59  unsigned long long out_max;
60  unsigned long long im_cnt;
61  unsigned long long im_bytes;
62  unsigned long long pm_cnt;
63  unsigned long long pm_failures;
64  double pm_avg;
65  double pm_sd;
66  double te_avg;
67  double te_sd;
69 
70 #ifdef __cplusplus
71 extern "C"
72 {
73 #endif
74 
75 #include "../lauxlib.h"
76 #include "../lua.h"
77 
79 
80 /**
81  * inject_message callback function provided by the host. Only one (or neither)
82  * of the checkpoint values will be set in a call. Numeric checkpoints can have
83  * a measurable performance benefit but are not always applicable so both
84  * options are provided to support various types of input plugins. This function
85  * can be called with a NULL pb pointer by the 'is_running' API or if only a
86  * checkpoint update is being performed; it should be treated as a no-op and
87  * used as a synchronization point to collect statistics and communicate
88  * shutdown status.
89  *
90  * @param parent Opaque pointer the host object owning this sandbox
91  * @param pb Pointer to a Heka protobuf encoded message being injected.
92  * @param pb_en Length of s
93  * @param cp_numeric Numeric based checkpoint (can be NAN)
94  * @param cp_string String based checkpoint (can be NULL)
95  *
96  * @return 0 on success
97  */
98 typedef int (*lsb_heka_im_input)(void *parent,
99  const char *pb,
100  size_t pb_len,
101  double cp_numeric,
102  const char *cp_string);
103 
104 /**
105  * inject_message callback function provided by the host.
106  *
107  * @param parent Opaque pointer the host object owning this sandbox.
108  * @param pb Pointer to a Heka protobuf encoded message being injected.
109  * @param len Length of pb_len
110  *
111  * @return 0 on success
112  */
113 typedef int (*lsb_heka_im_analysis)(void *parent,
114  const char *pb,
115  size_t pb_len);
116 
117 /**
118  * update_checkpoint callback function provided by the host.
119  *
120  * @param parent Opaque pointer the host object owning this sandbox.
121  * @param sequence_id Opaque pointer to the host message sequencing (passed into
122  * process_message).
123  *
124  * @return 0 on success
125  */
126 typedef int (*lsb_heka_update_checkpoint)(void *parent, void *sequence_id);
127 
128 /**
129  * Create a sandbox supporting the Heka Input Plugin API
130  *
131  * @param parent Opaque pointer the host object owning this sandbox
132  * @param lua_file Fully qualified path to the Lua source file
133  * @param state_file Fully qualified filename to the state preservation file
134  * (NULL if no preservation is required)
135  * @param lsb_cfg Full configuration string as a Lua table (NULL for lsb
136  * defaults)
137  * @param logger Struct for error reporting/debug printing (NULL to disable)
138  * @param im inject_message callback
139  * @return lsb_heka_sandbox* On success a pointer to the sandbox otherwise NULL
140  */
143  const char *lua_file,
144  const char *state_file,
145  const char *lsb_cfg,
146  lsb_logger *logger,
147  lsb_heka_im_input im);
148 
149 /**
150  * Host access to the input sandbox process_message API. If a numeric
151  * checkpoint is set the string checkpoint is ignored.
152  *
153  * @param hsb Heka input sandbox
154  * @param cp_numeric NAN if no numeric checkpoint
155  * @param cp_string NULL if no string checkpoint
156  * @param profile Take a timing sample on this execution
157  *
158  * @return int
159  * >0 fatal error
160  * 0 success
161  * <0 non-fatal error (status message is logged)
162  *
163  */
166  double cp_numeric,
167  const char *cp_string,
168  bool profile);
169 
170 /**
171  * Create a sandbox supporting the Heka Analysis Plugin API
172  *
173  * @param parent Opaque pointer the host object owning this sandbox
174  * @param lua_file Fully qualified filename to the Lua source file
175  * @param state_file Fully qualified filename to the state preservation file
176  * (NULL if no preservation is required)
177  * @param lsb_cfg Full configuration string as a Lua table (NULL for lsb
178  * defaults)
179  * @param logger Struct for error reporting/debug printing (NULL to disable)
180  * @param im inject_message callback
181  * @return lsb_heka_sandbox* On success a pointer to the sandbox otherwise NULL
182  */
185  const char *lua_file,
186  const char *state_file,
187  const char *lsb_cfg,
188  lsb_logger *logger,
190 
191 /**
192  * Host access to the analysis sandbox process_message API
193  *
194  * @param hsb Heka analysis sandbox
195  * @param msg Heka message to process
196  * @param profile Take a timing sample on this execution
197  *
198  * @return int
199  * >0 fatal error
200  * 0 success
201  * <0 non-fatal error (status message is logged)
202  *
203  */
206  lsb_heka_message *msg,
207  bool profile);
208 
209 /**
210  * Create a sandbox supporting the Heka Output Plugin API
211  *
212  * @param parent Opaque pointer the host object owning this sandbox
213  * @param lua_file Fully qualified path to the Lua source file
214  * @param state_file Fully qualified filename to the state preservation file
215  * (NULL if no preservation is required)
216  * @param lsb_cfg Full configuration string as a Lua table (NULL for lsb
217  * defaults)
218  * @param logger Struct for error reporting/debug printing (NULL to disable)
219  * @param ucp checkpoint_updated callback when using batch or async output
220  *
221  * @return lsb_heka_sandbox* On success a pointer to the sandbox otherwise NULL
222  */
225  const char *lua_file,
226  const char *state_file,
227  const char *lsb_cfg,
228  lsb_logger *logger,
230 
231 /**
232  * Create a sandbox supporting the Heka Output Plugin API with
233  * inject_message support
234  *
235  * @param parent Opaque pointer the host object owning this sandbox
236  * @param lua_file Fully qualified path to the Lua source file
237  * @param state_file Fully qualified filename to the state preservation file
238  * (NULL if no preservation is required)
239  * @param lsb_cfg Full configuration string as a Lua table (NULL for lsb
240  * defaults)
241  * @param logger Struct for error reporting/debug printing (NULL to disable)
242  * @param ucp checkpoint_updated callback when using batch or async output
243  * @param im inject_message callback
244  *
245  * @return lsb_heka_sandbox* On success a pointer to the sandbox otherwise NULL
246  */
249  const char *lua_file,
250  const char *state_file,
251  const char *lsb_cfg,
252  lsb_logger *logger,
255 /**
256  * Host access to the output sandbox process_message API
257  *
258  * @param hsb Heka output sandbox
259  * @param msg Heka message to process
260  * @param sequence_id Opaque pointer to the message sequence id (only used for
261  * async output plugin otherwise it should be NULL)
262  * @param profile Take a timing sample on this execution
263  *
264  * @return int
265  * >0 fatal error
266  * 0 success
267  * -1 non-fatal_error (status message is logged)
268  * -2 skip
269  * -3 retry
270  * -4 batching
271  * -5 async output
272  *
273  */
276  lsb_heka_message *msg,
277  void *sequence_id,
278  bool profile);
279 /**
280  * Requests a long running input sandbox to stop. This call is not thread safe.
281  *
282  * @param hsb Heka sandbox to cleanly stop
283  *
284  * @return
285  *
286  */
287 LSB_HEKA_EXPORT void
289 
290 /**
291  * Aborts the running sandbox from a different thread of execution. A "shutting
292  * down" termination message is generated. Used to abort long runnning sandboxes
293  * such as an input sandbox.
294  *
295  * @param hsb Heka sandbox to forcibly stop
296  *
297  * @return
298  *
299  */
300 LSB_HEKA_EXPORT void
302 
303 /**
304  * Terminates the sandbox as if it had a fatal error (not thread safe).
305  *
306  * @param hsb Heka sandbox to terminate
307  * @param err Reason for termination
308  */
309 LSB_HEKA_EXPORT void
310 lsb_heka_terminate_sandbox(lsb_heka_sandbox *hsb, const char *err);
311 
312 /**
313  * Frees all memory associated with the sandbox; hsb cannont be used after this
314  * point and the host should set it to NULL.
315  *
316  * @param hsb Heka sandbox to free
317  *
318  * @return NULL on success, pointer to an error message on failure that MUST BE
319  * FREED by the caller.
320  *
321  */
322 LSB_HEKA_EXPORT char*
324 
325 /**
326  * Host access to the timer_event API
327  *
328  * @param hsb Heka sandbox
329  * @param t Clock time of the timer_event execution
330  * @param shutdown Flag indicating the Host is shutting down allowing the
331  * sandbox to do any desired finialization)
332  *
333  * @return int 0 on success
334  */
336 int lsb_heka_timer_event(lsb_heka_sandbox *hsb, time_t t, bool shutdown);
337 
338 /**
339  * Return the last error in human readable form.
340  *
341  * @param hsb Heka sandbox
342  *
343  * @return const char* error message
344  */
346 
347 /**
348  * Returns the filename of the Lua source.
349  *
350  * @param hsb Heka sandbox
351  *
352  * @return const char* filename.
353  */
355 
356 /**
357  * Retrieve the sandbox profiling/monitoring statistics. This call accesses
358  * internal data and is not thread safe.
359  *
360  * @param hsb Heka sandbox
361  *
362  * @return lsb_heka_stats A copy of the stats structure
363  */
365 
366 /**
367  * Convenience function to test if a sandbox is running.
368  *
369  * @param hsb Heka sandbox
370  *
371  * @return True if the sandbox has not been terminated
372  */
374 
375 /**
376  * Queries the state of the sandbox.
377  *
378  * @param hsb
379  *
380  * @return lsb_state
381  *
382  */
384 
385 /**
386  * Retrieve the currently active sandbox message. This call returns a handle to
387  * internal data and is not thread safe.
388  * *
389  * @param hsb Heka sandbox
390  *
391  * @return const lsb_heka_message* NULL if there is no active message
392  */
395 
396 /**
397  * Retrieve the sandbox type.
398  * *
399  * @param hsb Heka sandbox
400  *
401  * @return char Heka sandbox type identifer
402  */
404 
405 #ifdef __cplusplus
406 }
407 #endif
408 
409 #endif
LSB_HEKA_EXPORT int lsb_heka_timer_event(lsb_heka_sandbox *hsb, time_t t, bool shutdown)
Host access to the timer_event API.
LSB_HEKA_EXPORT lsb_heka_sandbox * lsb_heka_create_input(void *parent, const char *lua_file, const char *state_file, const char *lsb_cfg, lsb_logger *logger, lsb_heka_im_input im)
Create a sandbox supporting the Heka Input Plugin API.
LSB_HEKA_EXPORT lsb_heka_sandbox * lsb_heka_create_analysis(void *parent, const char *lua_file, const char *state_file, const char *lsb_cfg, lsb_logger *logger, lsb_heka_im_analysis im)
Create a sandbox supporting the Heka Analysis Plugin API.
int(* lsb_heka_im_analysis)(void *parent, const char *pb, size_t pb_len)
inject_message callback function provided by the host.
Definition: sandbox.h:113
LSB_HEKA_EXPORT lsb_heka_sandbox * lsb_heka_create_output_im(void *parent, const char *lua_file, const char *state_file, const char *lsb_cfg, lsb_logger *logger, lsb_heka_update_checkpoint ucp, lsb_heka_im_analysis im)
Create a sandbox supporting the Heka Output Plugin API with inject_message support.
struct lsb_heka_stats lsb_heka_stats
lsb_heka_pm_rv
Definition: sandbox.h:37
unsigned long long ins_max
Definition: sandbox.h:58
LSB_HEKA_EXPORT void lsb_heka_terminate_sandbox(lsb_heka_sandbox *hsb, const char *err)
Terminates the sandbox as if it had a fatal error (not thread safe).
unsigned long long out_max
Definition: sandbox.h:59
double te_avg
Definition: sandbox.h:66
LSB_HEKA_EXPORT void lsb_heka_stop_sandbox(lsb_heka_sandbox *hsb)
Aborts the running sandbox from a different thread of execution.
const char lsb_err_id[]
Definition: error.h:14
double pm_avg
Definition: sandbox.h:64
struct lsb_heka_sandbox lsb_heka_sandbox
Definition: sandbox.h:53
LSB_HEKA_EXPORT int lsb_heka_pm_analysis(lsb_heka_sandbox *hsb, lsb_heka_message *msg, bool profile)
Host access to the analysis sandbox process_message API.
int(* lsb_heka_update_checkpoint)(void *parent, void *sequence_id)
update_checkpoint callback function provided by the host.
Definition: sandbox.h:126
LSB_HEKA_EXPORT lsb_state lsb_heka_get_state(lsb_heka_sandbox *hsb)
Queries the state of the sandbox.
LSB_HEKA_EXPORT const char * lsb_heka_get_lua_file(lsb_heka_sandbox *hsb)
Returns the filename of the Lua source.
unsigned long long mem_cur
Definition: sandbox.h:56
LSB_HEKA_EXPORT const char * lsb_heka_get_error(lsb_heka_sandbox *hsb)
Return the last error in human readable form.
unsigned long long im_cnt
Definition: sandbox.h:60
LSB_HEKA_EXPORT int lsb_heka_pm_output(lsb_heka_sandbox *hsb, lsb_heka_message *msg, void *sequence_id, bool profile)
Host access to the output sandbox process_message API.
LSB_HEKA_EXPORT bool lsb_heka_is_running(lsb_heka_sandbox *hsb)
Convenience function to test if a sandbox is running.
LSB_HEKA_EXPORT lsb_err_id LSB_ERR_HEKA_INPUT
unsigned long long mem_max
Definition: sandbox.h:57
LSB_HEKA_EXPORT lsb_heka_sandbox * lsb_heka_create_output(void *parent, const char *lua_file, const char *state_file, const char *lsb_cfg, lsb_logger *logger, lsb_heka_update_checkpoint ucp)
Create a sandbox supporting the Heka Output Plugin API.
#define LSB_HEKA_EXPORT
Definition: sandbox.h:29
unsigned long long pm_failures
Definition: sandbox.h:63
lsb_state
Definition: luasandbox.h:42
LSB_HEKA_EXPORT lsb_heka_stats lsb_heka_get_stats(lsb_heka_sandbox *hsb)
Retrieve the sandbox profiling/monitoring statistics.
int(* lsb_heka_im_input)(void *parent, const char *pb, size_t pb_len, double cp_numeric, const char *cp_string)
inject_message callback function provided by the host.
Definition: sandbox.h:98
lsb_heka_im_rv
Definition: sandbox.h:46
unsigned long long pm_cnt
Definition: sandbox.h:62
LSB_HEKA_EXPORT const lsb_heka_message * lsb_heka_get_message(lsb_heka_sandbox *hsb)
Retrieve the currently active sandbox message.
LSB_HEKA_EXPORT char lsb_heka_get_type(lsb_heka_sandbox *hsb)
Retrieve the sandbox type.
double te_sd
Definition: sandbox.h:67
LSB_HEKA_EXPORT char * lsb_heka_destroy_sandbox(lsb_heka_sandbox *hsb)
Frees all memory associated with the sandbox; hsb cannont be used after this point and the host shoul...
double pm_sd
Definition: sandbox.h:65
LSB_HEKA_EXPORT int lsb_heka_pm_input(lsb_heka_sandbox *hsb, double cp_numeric, const char *cp_string, bool profile)
Host access to the input sandbox process_message API.
unsigned long long im_bytes
Definition: sandbox.h:61
LSB_HEKA_EXPORT void lsb_heka_stop_sandbox_clean(lsb_heka_sandbox *hsb)
Requests a long running input sandbox to stop.