This repository was archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceUnix.c
274 lines (238 loc) · 5.73 KB
/
ServiceUnix.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
//
// MODULE: ServiceUnix.c
//
// PURPOSE: Implements functions required by all daemon Unix.
//
// FUNCTIONs:
// main(int argc, char **argv);
// daemon_start()
// daemon_stop()
// daemon_pause()
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <syslog.h>
#include <ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include "Service.h"
#ifndef SOMAXCONN
#define SOMAXCONN 32
#endif
#ifndef __linux__
//extern char *sys_siglist[]; adaptation pour DLINK DNS320
#else
#undef OPEN_MAX
#define OPEN_MAX 512
#endif
#ifndef OPEN_MAX
#define OPEN_MAX FOPEN_MAX
#endif
#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif
typedef int BOOL;
typedef struct sigaction sigact_t;
// internal variables
static int SignalArret = 0;
static int bFinDemon = FALSE;
// internal function prototypes
static void Arrete();
//
// FUNCTION: main
//
// PURPOSE: entrypoint for service
//
// PARAMETERS:
// argc - number of command line arguments
// argv - array of command line arguments
//
// RETURN VALUE:
// none
//
// COMMENTS:
// This routine performs the service initialization and then calls
// the user defined ServiceStart() routine to perform majority
// of the work.
//
/*
* Main - Traite les arguments et transforme notre process en demon
*/
int main(int argc, char *argv[])
{
sigact_t ActionSig;
sigset_t MasqueSig;
int dwErr = 0;
char *p;
int pidFilehandle;
char Str[64];
int i;
char *ConfigFile=NULL;
char *LogFile=NULL;
char *PidFile=NULL;
/*
* Reconstruction du nom du service
*/
strcpy(g_ServiceChemin, *argv);
p = strrchr(g_ServiceChemin, '/');
if(p != NULL)
{
p++;
strcpy(g_ServiceNom, p);
*p = '\0';
}
else
{
strcpy(g_ServiceNom, g_ServiceChemin);
g_ServiceChemin[0]='\0';
}
p = strrchr(g_ServiceNom, '.');
if(p != NULL) *p = '\0';
/*
* Lecture des paramètres
*/
for (i=1; i < argc; i++)
{
if(strncmp(argv[i], "--config-file=", 14)==0) ConfigFile = argv[i]+14;
if(strncmp(argv[i], "--log-file=", 11)==0) LogFile = argv[i]+11;
if(strncmp(argv[i], "--pid-file=", 11)==0) PidFile = argv[i]+11;
}
/*
* Créer le fichier PID
*/
if(LogFile==NULL)
{
sprintf(Str, "/var/run/%s.pid",g_ServiceNom);
LogFile = Str;
}
pidFilehandle = open(LogFile, O_RDWR|O_CREAT, 0600);
if(pidFilehandle != -1)
{
sprintf(Str,"%d\n",getpid());
write(pidFilehandle, Str, strlen(Str));
close(pidFilehandle);
}
/*
* Ignorer les signaux
*/
ActionSig.sa_handler = SIG_IGN;
ActionSig.sa_flags = 0;
(void)sigemptyset(&ActionSig.sa_mask);
if ( sigaction(SIGHUP, &ActionSig, NULL) != 0
|| sigaction(SIGINT, &ActionSig, NULL) != 0
|| sigaction(SIGQUIT, &ActionSig, NULL) != 0
|| sigaction(SIGPIPE, &ActionSig, NULL) != 0
|| sigaction(SIGCHLD, &ActionSig, NULL) != 0
|| sigaction(SIGTSTP, &ActionSig, NULL) != 0
|| sigaction(SIGTTIN, &ActionSig, NULL) != 0
|| sigaction(SIGTTOU, &ActionSig, NULL) != 0)
{
dwErr = errno;
goto Cleanup;
}
/*
* Gestion du signal d'arret
*/
(void)sigemptyset(&MasqueSig);
(void)sigaddset(&MasqueSig, SIGTERM);
if (pthread_sigmask(SIG_BLOCK, &MasqueSig, NULL) != 0)
{
dwErr = errno;
goto Cleanup;
}
ActionSig.sa_handler = Arrete;
if ( sigaction(SIGTERM, &ActionSig, NULL) != 0)
{
dwErr = errno;
goto Cleanup;
}
if (pthread_sigmask(SIG_UNBLOCK, &MasqueSig, NULL) != 0)
{
dwErr = errno;
goto Cleanup;
}
/*
* --- Boucle principale du thread
*/
dwErr = ServiceStart(ConfigFile, LogFile);
/*
* Sortie du service, normale ou en erreur
*/
if (SignalArret != SIGTERM)
printf("Arret du service par reception du signal %d (%s)", SignalArret, strsignal(SignalArret)); //adaptation pour DLINK DNS320
//printf("Arret du service par reception du signal %d (%s)", SignalArret, sys_siglist[SignalArret]);
Cleanup:
/*
* On suppose que les ressources thread sont
* automatiquement liberees a la fin du process.
*/
if (dwErr != 0)
printf("Le service s'est arrêté suite à une erreur : %d", dwErr);
else
printf("Le service a été arrêté correctement");
return 0;
}
/*
* Arrete - Interception des signaux d'arret par le thread principal
*/
void Arrete(int Signal)
{
switch (Signal)
{
case SIGTERM :
if (!bFinDemon) /* Si l'on n'a pas déjà une demande de fin du service */
SignalArret = SIGTERM; /* c'est qu'il ne s'agit pas d'un kill dù à un autre signal */
bFinDemon = TRUE;
ServiceStop();
break;
default :
SignalArret = Signal; /* Conserver le n° du Signal initial */
bFinDemon = TRUE; /* Forcer la fin du Service */
kill(getpid(), SIGTERM);
break;
}
return;
}
/*
* OsJournalise - Ecrit dans le journal de l'OS
*/
void OsJournalise(int Code, char *Message)
{
#ifdef _AIX
static struct syslog_data LogData = SYSLOG_DATA_INIT;
#endif
static BOOL PremierAppel = TRUE;
int Level = LOG_ERR; /* Par défaut */
int Facility = LOG_LOCAL1;
char *Type;
char Info[32];
if (PremierAppel)
{
#ifdef _AIX
(void)openlog_r("", LOG_CONS | LOG_PID, Facility, &LogData);
#else
(void)openlog("", LOG_CONS | LOG_PID, Facility);
#endif
PremierAppel = FALSE;
}
switch (Code)
{
case LOG_INFO: Level = LOG_INFO; Type = "Info."; break;
case LOG_WARNING: Level = LOG_WARNING; Type = "Warn."; break;
case LOG_ERR: Level = LOG_ERR; Type = "ERROR"; break;
}
sprintf(Info, "(%s) ", Type);
#ifdef _AIX
(void)syslog_r(Level | Facility, &LogData, "%s%s", Info, Message);
#else
(void)syslog(Level | Facility, "%s%s", Info, Message);
#endif
return;
}