summaryrefslogtreecommitdiff
path: root/sbin/ipf/libipf/save_execute.c
blob: dce5c2eeacdaf86470472054ebbfa22b1ac9bc70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "ipf.h"
#include "ipmon.h"

static void *execute_parse(char **);
static void execute_destroy(void *);
static int execute_send(void *, ipmon_msg_t *);
static void execute_print(void *);

typedef struct execute_opts_s {
	char	*path;
} execute_opts_t;

ipmon_saver_t executesaver = {
	"execute",
	execute_destroy,
	NULL,			/* dup */
	NULL,			/* match */
	execute_parse,
	execute_print,
	execute_send
};


static void *
execute_parse(char **strings)
{
	execute_opts_t *ctx;

	ctx = calloc(1, sizeof(*ctx));

	if (ctx != NULL && strings[0] != NULL && strings[0][0] != '\0') {
		ctx->path = strdup(strings[0]);

	} else {
		free(ctx);
		return (NULL);
	}

	return (ctx);
}


static void
execute_print(void *ctx)
{
	execute_opts_t *exe = ctx;

	printf("%s", exe->path);
}


static void
execute_destroy(void *ctx)
{
	execute_opts_t *exe = ctx;

	if (exe != NULL)
		free(exe->path);
	free(exe);
}


static int
execute_send(void *ctx, ipmon_msg_t *msg)
{
	execute_opts_t *exe = ctx;
	FILE *fp;

	fp = popen(exe->path, "w");
	if (fp != NULL) {
		fwrite(msg->imm_msg, msg->imm_msglen, 1, fp);
		pclose(fp);
	}
	return (0);
}