/
tools.c
298 lines (263 loc) · 9.13 KB
/
tools.c
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*******************************************************************************
*
* Copyright (c) 2004 Guillaume Cottenceau
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
******************************************************************************/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glib.h>
#include <pwd.h>
#include "tools.h"
#include "log.h"
#include "net.h"
// converts a char* to the number it represents, with:
// - when failing it returns 0
// - it stops on first non-digit char
int charstar_to_int(const char * s)
{
int number = 0;
while (*s && isdigit(*s)) {
number = (number * 10) + (*s - '0');
s++;
}
return number;
}
char * vasprintf_(const char *msg, va_list args)
{
char s[8192];
vsnprintf(s, sizeof(s), msg, args);
return strdup(s);
}
// _GNU_SOURCE's asprintf like, but
// - doesn't need _GNU_SOURCE
// - returns allocated string
// - never returns NULL (prints failure and exit on out of memory)
char * asprintf_(const char *msg, ...)
{
char * results;
va_list arg_ptr;
va_start(arg_ptr, msg);
results = vasprintf_(msg, arg_ptr);
va_end(arg_ptr);
return results;
}
void * malloc_(size_t size)
{
void * ret = malloc(size);
if (ret == NULL) {
fprintf(stderr, "Out of memory, exiting - size was %zd.\n", size);
exit(EXIT_FAILURE);
}
return ret;
}
void * realloc_(void * ptr, size_t size)
{
void * ret = realloc(ptr, size);
if (ret == NULL) {
fprintf(stderr, "Out of memory, exiting - size was %zd.\n", size);
exit(EXIT_FAILURE);
}
return ret;
}
char* strdup_(char* input)
{
char* ret = strdup(input);
if (ret == NULL) {
fprintf(stderr, "Out of memory, exiting - input was %s.\n", input);
exit(EXIT_FAILURE);
}
return ret;
}
void * memdup(void *src, size_t size)
{
void * r = malloc_(size);
memcpy(r, src, size);
return r;
}
/** Should be using strlcat but could not find a GPL implementation.
Check http://www.courtesan.com/todd/papers/strlcpy.html it rulz. */
size_t strconcat(char *dst, const char *src, size_t size)
{
char *ptr = dst + strlen(dst);
while (ptr - dst < size - 1 && *src) {
*ptr = *src;
ptr++;
src++;
}
*ptr = '\0';
return ptr - dst;
}
// is there a glist function to do that already?
void * GListp2data(GList * elem)
{
if (elem)
return elem->data;
else
return NULL;
}
static gpointer _g_list_fold_left_partial;
static GFoldFunc _g_list_fold_left_func;
static void _g_list_fold_left_aux(gpointer data, gpointer user_data)
{
_g_list_fold_left_partial = _g_list_fold_left_func(data, _g_list_fold_left_partial, user_data);
}
gpointer g_list_fold_left(GList * list, gpointer first, GFoldFunc func, gpointer user_data)
{
_g_list_fold_left_partial = first;
_g_list_fold_left_func = func;
g_list_foreach(list, _g_list_fold_left_aux, user_data);
return _g_list_fold_left_partial;
}
static GTruthFunc _g_list_any_func;
static gint _g_list_any_aux(gconstpointer data, gconstpointer user_data)
{
if (_g_list_any_func(data, user_data) == TRUE)
return 0;
else
return 1;
}
gboolean g_list_any(GList * list, GTruthFunc func, gpointer user_data)
{
_g_list_any_func = func;
if (g_list_find_custom(list, user_data, _g_list_any_aux) == NULL)
return FALSE;
else
return TRUE;
}
static void close_fds(gpointer data, gpointer user_data)
{
close(GPOINTER_TO_INT(data));
}
static time_t last_server_register = -1;
void reregister_server_if_needed() {
time_t current_time;
if (last_server_register == -1 || interval_reregister == 0)
// feature disabled
return;
// don't stack zombies, do minimal housework
waitpid(-1, NULL, WNOHANG);
current_time = get_current_time();
if ((current_time - last_server_register)/60 >= interval_reregister) {
l0(OUTPUT_TYPE_INFO, "Reregistering to master server");
last_server_register = current_time;
int pid = fork();
if (pid < 0) {
l1(OUTPUT_TYPE_ERROR, "Cannot fork: %s", strerror(errno));
return;
}
if (pid == 0) {
// Need to register from a separate process because master server will test us
register_server(1);
_exit(EXIT_SUCCESS);
}
}
}
void daemonize() {
pid_t pid, sid;
GList * retained_fds = NULL;
int fd;
// Using the file descriptor number 10 is not possible because newlines are checked
// for message termination. Retain it.
while (1) {
fd = open("/dev/null", 0);
if (fd == -1) {
l1(OUTPUT_TYPE_ERROR, "opening /dev/null: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (fd < 10) {
retained_fds = g_list_append(retained_fds, GINT_TO_POINTER(fd));
} else if (fd == 10) {
break;
} else {
// Past 10, so close this one and break
close(fd);
break;
}
}
if (retained_fds) {
g_list_foreach(retained_fds, close_fds, NULL);
g_list_free(retained_fds);
}
if (debug_mode)
return;
last_server_register = get_current_time();
pid = fork();
if (pid < 0) {
l1(OUTPUT_TYPE_ERROR, "Cannot fork: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (pid > 0) {
// Save pid if needed
if (pidfile != NULL) {
FILE* f = fopen(pidfile, "w");
if (f == NULL) {
l2(OUTPUT_TYPE_ERROR, "Cannot open pidfile '%s' for writing: %s", pidfile, strerror(errno));
} else {
char* pid_s = asprintf_("%d\n", pid);
if (fwrite(pid_s, 1, strlen(pid_s), f) < strlen(pid_s))
l1(OUTPUT_TYPE_ERROR, "Error writing pidfile to '%s'", pidfile);
fclose(f);
free(pid_s);
}
}
// Need to register from a separate process because master server will test us
l0(OUTPUT_TYPE_INFO, "registering server");
register_server(0);
exit(EXIT_SUCCESS);
}
// Switch to specified used if needed
if (user_to_switch != NULL) {
struct passwd* user = getpwnam(user_to_switch);
if (user) {
setgid(user->pw_gid);
setuid(user->pw_uid);
} else {
l2(OUTPUT_TYPE_ERROR, "Cannot switch user to %s: %s", user_to_switch, strerror(errno));
}
}
// Don't stay orphan
sid = setsid();
if (sid < 0) {
l1(OUTPUT_TYPE_ERROR, "Cannot setsid: %s", strerror(errno));
exit(EXIT_FAILURE);
}
// Don't lock a directory
if (chdir("/") < 0) {
l1(OUTPUT_TYPE_ERROR, "Cannot chdir: %s", strerror(errno));
exit(EXIT_FAILURE);
}
printf("Entering daemon mode.\n");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// Using the file descriptor number 0 is not possible due to the string-oriented protocol when
// negociating the game (since C strings cannot contain the NULL char). Retain one file descriptor.
fd = open("/dev/null", 0);
if (fd == -1) {
l1(OUTPUT_TYPE_ERROR, "opening /dev/null: %s", strerror(errno));
exit(EXIT_FAILURE);
}
l0(OUTPUT_TYPE_INFO, "Entered daemon mode.");
}