Home | History | Annotate | Line # | Download | only in lib
      1 /*	$NetBSD: save_execute.c,v 1.1.1.1 2012/03/23 21:20:10 christos Exp $	*/
      2 
      3 #include "ipf.h"
      4 #include "ipmon.h"
      5 
      6 static void *execute_parse __P((char **));
      7 static void execute_destroy __P((void *));
      8 static int execute_send __P((void *, ipmon_msg_t *));
      9 static void execute_print __P((void *));
     10 
     11 typedef struct execute_opts_s {
     12 	char	*path;
     13 } execute_opts_t;
     14 
     15 ipmon_saver_t executesaver = {
     16 	"execute",
     17 	execute_destroy,
     18 	NULL,			/* dup */
     19 	NULL,			/* match */
     20 	execute_parse,
     21 	execute_print,
     22 	execute_send
     23 };
     24 
     25 
     26 static void *
     27 execute_parse(char **strings)
     28 {
     29 	execute_opts_t *ctx;
     30 
     31 	ctx = calloc(1, sizeof(*ctx));
     32 
     33 	if (ctx != NULL && strings[0] != NULL && strings[0][0] != '\0') {
     34 		ctx->path = strdup(strings[0]);
     35 
     36 	} else {
     37 		free(ctx);
     38 		return NULL;
     39 	}
     40 
     41 	return ctx;
     42 }
     43 
     44 
     45 static void
     46 execute_print(ctx)
     47 	void *ctx;
     48 {
     49 	execute_opts_t *exe = ctx;
     50 
     51 	printf("%s", exe->path);
     52 }
     53 
     54 
     55 static void
     56 execute_destroy(ctx)
     57 	void *ctx;
     58 {
     59 	execute_opts_t *exe = ctx;
     60 
     61 	if (exe != NULL)
     62 		free(exe->path);
     63 	free(exe);
     64 }
     65 
     66 
     67 static int
     68 execute_send(ctx, msg)
     69 	void *ctx;
     70 	ipmon_msg_t *msg;
     71 {
     72 	execute_opts_t *exe = ctx;
     73 	FILE *fp;
     74 
     75 	fp = popen(exe->path, "w");
     76 	if (fp != NULL) {
     77 		fwrite(msg->imm_msg, msg->imm_msglen, 1, fp);
     78 		pclose(fp);
     79 	}
     80 	return 0;
     81 }
     82 
     83