forked from justanhduc/task-spooler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_start.c
227 lines (184 loc) · 5.08 KB
/
server_start.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
/*
Task Spooler - a task queue system for the unix user
Copyright (C) 2007-2009 Lluís Batlle i Rossell
Please find the license in the provided COPYING file.
*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "main.h"
int server_socket;
static char *socket_path;
static int should_check_owner = 0;
static int fork_server();
void create_socket_path(char **path) {
char *tmpdir;
char userid[20] = "root";
int size;
/* As a priority, TS_SOCKET mandates over the path creation */
*path = getenv("TS_SOCKET");
if (*path != 0) {
/* We need this in our memory, for forks and future 'free'. */
size = strlen(*path) + 1;
*path = (char *)malloc(size);
strcpy(*path, getenv("TS_SOCKET"));
/* We don't want to check ownership of the socket here,
* as the user may have thought of some shared queue */
should_check_owner = 0;
return;
}
/* ... if the $TS_SOCKET doesn't exist ... */
/* Create the path */
tmpdir = getenv("TMPDIR");
if (tmpdir == NULL)
tmpdir = "/tmp";
/* Calculate the size */
size = strlen(tmpdir) + strlen("/socket-ts.") + strlen(userid) + 1;
/* Freed after preparing the socket address */
*path = (char *)malloc(size);
snprintf(*path, size, "%s/socket-ts.%s", tmpdir, userid);
should_check_owner = 1;
}
int try_connect(int s) {
struct sockaddr_un addr;
int res;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, socket_path);
res = connect(s, (struct sockaddr *)&addr, sizeof(addr));
return res;
}
void c_check_daemon() {
int res;
server_socket = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_socket == -1)
error("getting the server socket");
create_socket_path(&socket_path);
res = try_connect(server_socket);
/* Good connection */
if (res == 0) {
printf("Good connection to the task-spooler server\n");
exit(0);
} else {
printf("Cannot connect to the task-spooler server\n");
exit(1);
}
}
static void try_check_ownership() {
int res;
struct stat socketstat;
if (!should_check_owner)
return;
res = stat(socket_path, &socketstat);
if (res != 0)
error("Cannot state the socket %s.", socket_path);
// if (socketstat.st_uid != getuid())
// error("The uid %i does not own the socket %s.", getuid(), socket_path);
}
void wait_server_up(int fd) {
char a;
read(fd, &a, 1);
close(fd);
}
static void server_info() {
printf("Start tast-spooler server from root[%d]\n", root_UID);
printf(" Socket path: %s [TS_SOCKET]\n", socket_path);
printf(" Read user file from %s [TS_USER_PATH]\n", get_user_path());
printf(" Write log file to %s [TS_LOGFILE_PATH]\n", set_server_logfile());
printf(" Sqlite Database @ %s [TS_SQLITE_PATH]\n", get_sqlite_path());
}
static void server_daemon() {
server_info();
server_main(0, socket_path);
exit(0);
}
/* Returns the fd where to wait for the parent notification */
static int fork_server() {
int pid;
int p[2];
/* !!! stdin/stdout */
pipe(p);
pid = fork();
switch (pid) {
case 0: /* Child */
close(p[0]);
close(server_socket);
/* Close all std handles for the server */
close(0);
close(1);
close(2);
setsid();
server_info();
server_main(p[1], socket_path);
exit(0);
break;
case -1: /* Error */
return -1;
default: /* Parent */
server_info();
close(p[1]);
}
/* Return the read fd */
return p[0];
}
void notify_parent(int fd) {
char a = 'a';
write(fd, &a, 1);
close(fd);
}
int ensure_server_up(int daemonFlag) {
int res;
int notify_fd;
server_socket = socket(AF_UNIX, SOCK_STREAM, 0);
if (server_socket == -1)
error("getting the server socket");
create_socket_path(&socket_path);
if (daemonFlag == 1)
remove(socket_path); // try to delete it
res = try_connect(server_socket);
/* Good connection */
if (res == 0) {
try_check_ownership();
free(socket_path);
return 1;
}
/* If error other than "No one listens on the other end"... */
if (!(errno == ENOENT || errno == ECONNREFUSED))
error("Error: cannot connect to the server");
if (errno == ECONNREFUSED)
unlink(socket_path);
int optval = 1;
if (setsockopt(server_socket, SOL_SOCKET, SO_PASSCRED, &optval,
sizeof(optval)) == -1)
error("Error: cannot setup SO_PASSCRED");
/* Try starting the server */
if (getuid() == root_UID) {
if (daemonFlag) {
printf("Start task-spooler server as daemon\n");
server_daemon();
} else {
printf("start task-spooler server\n");
notify_fd = fork_server();
}
} else {
printf("Running the Task-Spooler server as the ROOT user is the only "
"allowed option.\n");
}
wait_server_up(notify_fd);
res = try_connect(server_socket);
/* The second time didn't work. Abort. */
if (res == -1) {
fprintf(stderr, "The server didn't come up.\n");
exit(-1);
}
free(socket_path);
/* Good connection on the second time */
return 1;
}