Merge lp:~cbehrens/agent-smith/signals into lp:agent-smith

Proposed by Chris Behrens
Status: Merged
Merged at revision: 73
Proposed branch: lp:~cbehrens/agent-smith/signals
Merge into: lp:agent-smith
Diff against target: 165 lines (+80/-5)
4 files modified
src/agent.c (+59/-2)
src/main.c (+2/-1)
src/spool.h (+2/-0)
tests/check_agent.c (+17/-2)
To merge this branch: bzr merge lp:~cbehrens/agent-smith/signals
Reviewer Review Type Date Requested Status
Agent Smith devs Pending
Review via email: mp+33607@code.launchpad.net

Description of the change

Added signal handler.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/agent.c'
2--- src/agent.c 2010-08-20 15:19:08 +0000
3+++ src/agent.c 2010-08-25 03:57:43 +0000
4@@ -19,6 +19,7 @@
5 #include <malloc.h>
6 #include <stdio.h>
7 #include <string.h>
8+#include <signal.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12@@ -27,8 +28,39 @@
13 #include "spool.h"
14 #include "xen.h"
15
16+/*
17+ * Global variables
18+ */
19+
20 char *dispatcher = NULL;
21-
22+int agent_got_sigterm = 0;
23+
24+/*
25+ * Private function prototypes
26+ */
27+static void _agent_signal_handler(int signum);
28+
29+/*
30+ * Private functions
31+ */
32+static void _agent_signal_handler(int signum)
33+{
34+ switch(signum) {
35+ case SIGCHLD:
36+ case SIGPIPE:
37+ case SIGHUP:
38+ break;
39+ case SIGTERM:
40+ agent_got_sigterm = 1;
41+ break;
42+ default:
43+ break;
44+ }
45+}
46+
47+/*
48+ * Public functions
49+ */
50 void read_from_spool_and_send_to_xen(const char *msg) {
51 char *buf = NULL, xs_path[255];
52 const char *msgid;
53@@ -68,8 +100,30 @@
54 return 0;
55 }
56
57+
58+int agent_setup_signals(void)
59+{
60+ struct sigaction sa;
61+
62+ memset(&sa, 0, sizeof(sa));
63+ sa.sa_handler = _agent_signal_handler;
64+
65+ sigaction(SIGCHLD, &sa, NULL);
66+ sigaction(SIGPIPE, &sa, NULL);
67+ sigaction(SIGHUP, &sa, NULL);
68+ sigaction(SIGTERM, &sa, NULL);
69+
70+ return 0;
71+}
72+
73 int agent_init(const char *dispatcher_cmd) {
74 AGENT_LOG0("Starting Agent Smith");
75+
76+ if (agent_setup_signals() < 0) {
77+ AGENT_LOG0("Failed to initialise signal handlers");
78+ return -1;
79+ }
80+
81 if (xen_init() < 0) {
82 AGENT_LOG0("Failed to initialise Xen connection");
83 return -1;
84@@ -104,5 +158,8 @@
85 {
86 spool_process_pending_events();
87 xen_process_pending_events();
88- return 1;
89+ if (agent_got_sigterm)
90+ return 1;
91+ return 0;
92 }
93+
94
95=== modified file 'src/main.c'
96--- src/main.c 2010-08-20 15:18:41 +0000
97+++ src/main.c 2010-08-25 03:57:43 +0000
98@@ -86,6 +86,7 @@
99 return -1;
100 }
101
102- while (agent_loop());
103+ while (!agent_loop());
104+
105 return 0;
106 }
107
108=== modified file 'src/spool.h'
109--- src/spool.h 2010-07-07 09:26:52 +0000
110+++ src/spool.h 2010-08-25 03:57:43 +0000
111@@ -21,6 +21,8 @@
112 int spool_init(void);
113 char *spool_incoming_dir(void);
114 char *spool_outgoing_dir(void);
115+int agent_setup_signals(void);
116+
117 /**
118 * Store an incoming message in the spool
119 *
120
121=== modified file 'tests/check_agent.c'
122--- tests/check_agent.c 2010-08-24 17:30:29 +0000
123+++ tests/check_agent.c 2010-08-25 03:57:43 +0000
124@@ -15,10 +15,11 @@
125 */
126 #include <stdio.h>
127 #include <stdlib.h>
128+#include <sys/types.h>
129+#include <sys/stat.h>
130+#include <sys/wait.h>
131 #include <check.h>
132 #include <unistd.h>
133-#include <sys/stat.h>
134-#include <sys/wait.h>
135 #include "../src/agent.h"
136 #include "../src/xen.h"
137 #include "../src/spool.h"
138@@ -88,6 +89,19 @@
139 }
140 END_TEST
141
142+START_TEST(test_agent_signals)
143+{
144+ fail_unless(agent_init("/bin/true") == 0, "agent_init() failed");
145+
146+ fail_unless(kill(getpid(), SIGCHLD) == 0);
147+ fail_unless(kill(getpid(), SIGCHLD) == 0);
148+ fail_unless(kill(getpid(), SIGPIPE) == 0);
149+ fail_unless(kill(getpid(), SIGTERM) == 0);
150+ /* agent_loop() should return != 0 for receiving SIGTERM */
151+ fail_unless(agent_loop() != 0);
152+}
153+END_TEST
154+
155 Suite *agent_suite(void)
156 {
157 Suite *s = suite_create("Agent");
158@@ -96,6 +110,7 @@
159 tcase_add_checked_fixture(tc_agent, test_spool_to_xen_setup, test_spool_to_xen_teardown);
160 tcase_add_test(tc_agent, test_spool_to_xen);
161 tcase_add_test(tc_agent, test_xen_to_spool);
162+ tcase_add_test(tc_agent, test_agent_signals);
163 suite_add_tcase(s, tc_agent);
164
165 return s;

Subscribers

People subscribed via source and target branches