summaryrefslogtreecommitdiff
path: root/mailcheck/remote-helper.c
blob: f591c20bb374df422ed4455d90114bcfe4451bce (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* GNOME remote helper, forks to run imap or pop checks
 * (C) 2001 Eazel, Inc.
 *
 * Author: George Lebl
 *
 * Utterly ugly, make this use corba at some point in the future.
 */

#include "config.h"
#include <gtk/gtk.h>
#include <libgnome/libgnome.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>

#include "popcheck.h"

#include "remote-helper.h"

typedef struct {
	pid_t pid;
	int fd;
	guint timeout;
	RemoteHandler handler;
       	gpointer data;
	GDestroyNotify destroy_notify;
} RemoteHandlerData;

static gboolean
try_reading (gpointer data)
{
	int retval;
	int mails;
	RemoteHandlerData *handler = data;
	sigset_t mask, omask;

	sigemptyset (&mask);
	sigaddset (&mask, SIGPIPE);
	sigprocmask (SIG_BLOCK, &mask, &omask); 

	retval = read (handler->fd, &mails, sizeof (mails));

	sigprocmask (SIG_SETMASK, &omask, NULL);

	if (retval == 0 ||
	    (retval < 0 && errno == EAGAIN)) {
		if (kill (handler->pid, 0) != 0) {
			handler->timeout = 0;
			helper_whack_handle (handler);
			return FALSE;
		}
		return TRUE;
	} else if (retval < 0) {
		handler->timeout = 0;
		helper_whack_handle (handler);
		return FALSE;
	}

	handler->handler (mails, handler->data);

	handler->timeout = 0;
	helper_whack_handle (handler);
	return FALSE;
}

void
helper_whack_handle (gpointer handle)
{
	RemoteHandlerData *handler = handle;

	if (handler->fd >= 0)
		close (handler->fd);
	handler->fd = -1;

	if (handler->pid > 0) {
		kill (handler->pid, SIGTERM);
	}
	handler->pid = 0;

	if (handler->timeout > 0)
		gtk_timeout_remove (handler->timeout);
	handler->timeout = 0;
	
	handler->handler = NULL;
	if (handler->destroy_notify != NULL)
		handler->destroy_notify (handler->data);
	handler->data = NULL;
	handler->destroy_notify = NULL;

	g_free (handler);
}

static RemoteHandlerData *
fork_new_handler (RemoteHandler handler, gpointer data,
		  GDestroyNotify destroy_notify)
{
	pid_t pid;
	int fd[2];
	RemoteHandlerData *handler_data;

	if (pipe (fd) != 0)
		return NULL;

	handler_data = g_new0 (RemoteHandlerData, 1);

	pid = fork ();
	if (pid < 0) {
		close (fd[0]);
		close (fd[1]);
		g_free (handler_data);
		return NULL;
	} else if (pid == 0) {
		/*child*/
		close (fd[0]);
		pid = fork ();
		if (pid != 0) {
			write (fd[1], &pid, sizeof (pid));
			_exit (0);
		} else {
			handler_data->pid = 0;
			handler_data->fd = fd[1];
			return handler_data;
		}
	} else {
		/*parent*/
		close (fd[1]);
		waitpid (pid, 0, 0);
		read (fd[0], &pid, sizeof (pid));
		
		if (pid < 0) {
			close (fd[0]);
			g_free (handler_data);
			return NULL;
		}

		/* set to nonblocking */
		fcntl(fd[0], F_SETFL, O_NONBLOCK);

		handler_data->pid = pid;
		handler_data->fd = fd[0];
		handler_data->handler = handler;
		handler_data->data = data;
		handler_data->destroy_notify = destroy_notify;
		handler_data->timeout = gtk_timeout_add (500, try_reading,
							 handler_data);

		return handler_data;
	}
}


gpointer
helper_pop3_check (RemoteHandler handler, gpointer data,
		   GDestroyNotify destroy_notify,
		   const char *command,
		   const char *h, const char* n, const char* e)
{
	RemoteHandlerData *handler_data;

	handler_data = fork_new_handler (handler, data, destroy_notify);

	if (handler_data == NULL) {
		handler (pop3_check (h, n, e), data);
		if (destroy_notify != NULL)
			destroy_notify (data);
		return NULL;
	}

	if (handler_data->pid == 0) {
		int mails;

		if (command != NULL &&
		    command[0] != '\0')
			system (command);
	       
		mails = pop3_check (h, n, e);

		write (handler_data->fd, &mails, sizeof (mails));

		_exit (0);
	}

	return handler_data;
}

gpointer
helper_imap_check (RemoteHandler handler, gpointer data,
		   GDestroyNotify destroy_notify,
		   const char *command,
		   const char *h, const char* n, const char* e, const char *f)
{
	RemoteHandlerData *handler_data;

	handler_data = fork_new_handler (handler, data, destroy_notify);

	if (handler_data == NULL) {
		handler (imap_check (h, n, e, f), data);
		return NULL;
	}

	if (handler_data->pid == 0) {
		int mails;

		if (command != NULL &&
		    command[0] != '\0')
			system (command);
	       
		mails = imap_check (h, n, e, f);

		write (handler_data->fd, &mails, sizeof (mails));

		_exit (0);
	}

	return handler_data;
}