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