aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2005-06-26 10:40:46 +0000
committerChristian Grothoff <christian@grothoff.org>2005-06-26 10:40:46 +0000
commitd1c2a379d13208c9d9bae89a92485628b4d22fe8 (patch)
tree0279fe25853ee6e57830ace57666ad6c4eedee13
parenta3053968e14e25105e7351f7ecd0b17aa93745f2 (diff)
downloadgnunet-gtk-d1c2a379d13208c9d9bae89a92485628b4d22fe8.tar.gz
gnunet-gtk-d1c2a379d13208c9d9bae89a92485628b4d22fe8.zip
backend daemon code moved to util
-rw-r--r--src/daemon.c211
1 files changed, 11 insertions, 200 deletions
diff --git a/src/daemon.c b/src/daemon.c
index ab5b4da3..b294f313 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -29,165 +29,7 @@
29#include "daemon.h" 29#include "daemon.h"
30#include "main.h" 30#include "main.h"
31#include "helper.h" 31#include "helper.h"
32#ifndef MINGW
33 #include <sys/wait.h>
34#endif
35#include <GNUnet/gnunet_util.h> 32#include <GNUnet/gnunet_util.h>
36#include <GNUnet/gnunet_protocols.h>
37
38/**
39 * Checks if gnunetd is running
40 *
41 * NOTE: Uses CS_PROTO_CLIENT_COUNT query to determine if
42 * gnunetd is running
43 */
44static int checkDaemonRunning(void) {
45 GNUNET_TCP_SOCKET * sock;
46 CS_HEADER csHdr;
47 int ret;
48
49 sock = getClientSocket();
50 if(sock == NULL) {
51 BREAK();
52 return SYSERR;
53 }
54
55 csHdr.size
56 = htons(sizeof(CS_HEADER));
57 csHdr.type
58 = htons(CS_PROTO_CLIENT_COUNT);
59 if (SYSERR == writeToSocket(sock,
60 &csHdr)) {
61 LOG(LOG_DEBUG,
62 _("gnunetd is NOT running.\n"));
63 releaseClientSocket(sock);
64 return SYSERR;
65 }
66 if (SYSERR == readTCPResult(sock,
67 &ret)) {
68 releaseClientSocket(sock);
69 return SYSERR;
70 }
71 releaseClientSocket(sock);
72
73 return OK;
74}
75
76
77
78#if LINUX || OSX || SOLARIS || SOMEBSD
79static int launchWithExec() {
80 pid_t pid;
81
82 pid = fork();
83 if (pid == 0) {
84 char * args[4];
85 char * path;
86 char * cp;
87
88 path = NULL;
89 cp = getConfigurationString("MAIN",
90 "ARGV[0]");
91 if (cp != NULL) {
92 int i = strlen(cp);
93 while ( (i >= 0) &&
94 (cp[i] != DIR_SEPARATOR) )
95 i--;
96 if ( i != -1 ) {
97 cp[i+1] = '\0';
98 path = MALLOC(i+1+strlen("gnunetd"));
99 strcpy(path, cp);
100 strcat(path, "gnunetd");
101 args[0] = path;
102 FREE(cp);
103 } else {
104 args[0] = "gnunetd";
105 }
106 }
107 cp = getConfigurationString("GNUNET-GTK",
108 "GNUNETD-CONFIG");
109 if (cp != NULL) {
110 args[1] = "-c";
111 args[2] = cp;
112 } else {
113 args[1] = NULL;
114 }
115 args[3] = NULL;
116 errno = 0;
117 nice(10); /* return value is not well-defined */
118 if (errno != 0)
119 LOG_STRERROR(LOG_WARNING, "nice");
120 if (path != NULL)
121 execv(path,
122 args);
123 else
124 execvp("gnunetd",
125 args);
126 LOG_STRERROR(LOG_FAILURE, "exec");
127 LOG(LOG_FAILURE,
128 _("Attempted path to '%s' was '%s'.\n"),
129 "gnunetd",
130 (path == NULL) ? "gnunetd" : path);
131 FREENONNULL(path); /* yeah, right, like we're likely to get
132 here... */
133 FREENONNULL(args[1]);
134 _exit(-1);
135 } else {
136 pid_t ret;
137 int status;
138
139 ret = waitpid(pid, &status, 0);
140 if (ret == -1) {
141 LOG_STRERROR(LOG_ERROR, "waitpid");
142 return SYSERR;
143 }
144 if ( (WIFEXITED(status) &&
145 (0 != WEXITSTATUS(status)) ) ) {
146 guiMessage(_("Starting gnunetd failed, error code: %d"),
147 WEXITSTATUS(status));
148 return SYSERR;
149 }
150#ifdef WCOREDUMP
151 if (WCOREDUMP(status)) {
152 guiMessage(_("Starting gnunetd failed (core dumped)."));
153 return SYSERR;
154 }
155#endif
156 if (WIFSIGNALED(status) ||
157 WTERMSIG(status) ) {
158 guiMessage(_("Starting gnunetd failed (aborted by signal)."));
159 return SYSERR;
160 }
161 return OK;
162 }
163}
164#endif
165
166static int doLaunch() {
167
168#if LINUX || OSX || SOLARIS || SOMEBSD
169 return launchWithExec();
170#elif MINGW
171 char szCall[_MAX_PATH + 1], szWd[_MAX_PATH + 1], szCWd[_MAX_PATH + 1];
172 char *args[1];
173
174 plibc_conv_to_win_path("/bin/gnunetd.exe", szCall);
175 plibc_conv_to_win_path("/bin", szWd);
176 _getcwd(szCWd, _MAX_PATH);
177
178 chdir(szWd);
179 args[0] = NULL;
180 spawnvp(_P_NOWAIT, szCall, (const char *const *) args);
181 chdir(szCWd);
182
183 return OK;
184#else
185 /* any system out there that does not support THIS!? */
186 system("gnunetd"); /* we may not have nice,
187 so let's be minimalistic here. */
188 return OK;
189#endif
190}
191 33
192/** 34/**
193 * Launch gnunetd w/ checks 35 * Launch gnunetd w/ checks
@@ -199,12 +41,16 @@ void on_startDaemon_clicked(GtkWidget * widget,
199 = glade_xml_get_widget(mainXML, 41 = glade_xml_get_widget(mainXML,
200 "startDaemon"); 42 "startDaemon");
201 gtk_widget_set_sensitive(launchEntry, FALSE); 43 gtk_widget_set_sensitive(launchEntry, FALSE);
202 if (OK == checkDaemonRunning() ) { 44 if (OK == checkGNUnetDaemonRunning() ) {
203 cronCheckDaemon(NULL); 45 cronCheckDaemon(NULL);
204 return; 46 return;
205 } else { 47 } else {
206 addLogEntry(_("Launching gnunetd...")); 48 addLogEntry(_("Launching gnunetd..."));
207 doLaunch(); 49 if (OK == startGNUnetDaemon(YES)) {
50 addLogEntry(_("Launched gnunetd"));
51 } else {
52 addLogEntry(_("Launching gnunetd failed"));
53 }
208 } 54 }
209} 55}
210 56
@@ -219,47 +65,12 @@ void on_stopDaemon_clicked(GtkWidget * widget,
219 "stopDaemon"); 65 "stopDaemon");
220 gtk_widget_set_sensitive(killEntry, FALSE); 66 gtk_widget_set_sensitive(killEntry, FALSE);
221 67
222 if (OK == checkDaemonRunning() ) { 68 if (OK == checkGNUnetDaemonRunning() ) {
223 GNUNET_TCP_SOCKET * sock; 69 if (OK != stopGNUnetDaemon()) {
224 CS_HEADER csHdr; 70 guiMessage(_("Error requesting shutdown by gnunetd."));
225 int ret;
226
227 sock = getClientSocket();
228 if (sock == NULL) {
229 cronCheckDaemon(NULL);
230 /* well, probably already dead */
231 return;
232 }
233 csHdr.size
234 = htons(sizeof(CS_HEADER));
235 csHdr.type
236 = htons(CS_PROTO_SHUTDOWN_REQUEST);
237 if (SYSERR == writeToSocket(sock,
238 &csHdr)) {
239 guiMessage(_("Error sending shutdown request to gnunetd."));
240 releaseClientSocket(sock);
241 cronCheckDaemon(NULL);
242 return;
243 }
244 if (SYSERR == readTCPResult(sock,
245 &ret)) {
246 guiMessage(_("Error reading shutdown confirmation from gnunetd."));
247 releaseClientSocket(sock);
248 cronCheckDaemon(NULL);
249 return;
250 }
251 if (ret == OK) {
252 static GtkWidget * killEntry;
253 killEntry
254 = glade_xml_get_widget(mainXML,
255 "stopDaemon");
256 gtk_widget_set_sensitive(killEntry, FALSE);
257 addLogEntry(_("Terminating gnunetd..."));
258 } else { 71 } else {
259 guiMessage(_("gnunetd refused to shut down (error code '%d')."), 72 addLogEntry(_("Terminating gnunetd..."));
260 ret);
261 } 73 }
262 releaseClientSocket(sock);
263 } 74 }
264 cronCheckDaemon(NULL); 75 cronCheckDaemon(NULL);
265} 76}
@@ -317,7 +128,7 @@ void cronCheckDaemon(void * dummy) {
317 static int last = 42; 128 static int last = 42;
318 int ret; 129 int ret;
319 130
320 ret = checkDaemonRunning(); 131 ret = checkGNUnetDaemonRunning();
321 if (ret != last) { 132 if (ret != last) {
322 last = ret; 133 last = ret;
323 gtkSaveCall((GtkFunction) doUpdateMenus, &ret); 134 gtkSaveCall((GtkFunction) doUpdateMenus, &ret);