30 #ifdef HAVE_SYS_UTSNAME_H
31 # include <sys/utsname.h>
36 #include <sys/types.h>
40 #include <sys/resource.h>
46 const gchar *message, gpointer user_data);
57 return g_strdup(un.nodename);
60 # ifdef HAVE_GETCOMPUTERNAME
66 char sn[MAX_COMPUTERNAME_LENGTH + 1];
67 DWORD snsize =
sizeof(sn);
70 ret = GetComputerName((LPSTR) sn, &snsize);
75 return g_strdup(
"GetComputerName failed");
78 # error "Need some function to get our computer name!"
86 const char* dirtorunin,
89 struct rlimit nofile_limits;
90 int nullperms[] = { O_RDONLY, O_WRONLY, O_WRONLY};
92 getrlimit(RLIMIT_NOFILE, &nofile_limits);
97 g_message(
"Already running.");
103 if (!stay_in_foreground) {
110 for (k=0; k < 2; ++k) {
113 g_error(
"Cannot fork [%s %d]", g_strerror(errno), errno);
124 if (chdir(dirtorunin ? dirtorunin :
"/" )) {
125 g_warning(
"%s.%d: Cannot change directory to [%s]",
__FUNCTION__
126 , __LINE__, dirtorunin);
132 g_message(
"%s.%d: Already running.",
__FUNCTION__, __LINE__);
141 if (!stay_in_foreground) {
143 for (j=0; j <
DIMOF(nullperms); ++j) {
145 nullfd = open(
"/dev/null", nullperms[j]);
148 g_error(
"%s.%d: Cannot open /dev/null(!)",
__FUNCTION__, __LINE__);
153 if (nullfd != (
int)j) {
154 if (dup2(nullfd, j) != (
int)j) {
155 g_error(
"dup2(%d,%d) failed. World coming to end.", nullfd, j);
162 for (j=
DIMOF(nullperms); j < nofile_limits.rlim_cur; ++j) {
169 const char* dirtorunin,
174 g_message(
"Already running.");
185 static gboolean syslog_opened = FALSE;
190 const int syslog_options = LOG_PID|LOG_NDELAY;
191 const int syslog_facility = LOG_DAEMON;
193 if (!syslog_opened) {
194 g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION
206 g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR|G_LOG_LEVEL_CRITICAL);
208 syslog_opened = TRUE;
213 GLogLevelFlags log_level,
214 const gchar *message,
221 #define LOG_WARNING 4
227 int syslogprio = LOG_INFO;
228 const char * prefix =
"INFO:";
231 if (!syslog_opened) {
234 if (log_level & G_LOG_LEVEL_DEBUG) {
235 syslogprio = LOG_DEBUG;
238 if (log_level & G_LOG_LEVEL_INFO) {
239 syslogprio = LOG_INFO;
242 if (log_level & G_LOG_LEVEL_MESSAGE) {
243 syslogprio = LOG_NOTICE;
246 if (log_level & G_LOG_LEVEL_WARNING) {
247 syslogprio = LOG_WARNING;
250 if (log_level & G_LOG_LEVEL_CRITICAL) {
251 syslogprio = LOG_ERR;
254 if (log_level & G_LOG_LEVEL_ERROR) {
255 syslogprio = LOG_EMERG;
259 syslog(syslogprio,
"%s:%s %s", prefix
260 , log_domain == NULL ?
"" : log_domain
265 g_snprintf(msg,
sizeof(msg),
"%s: %s:%s %s\n",
assim_syslogid, prefix
266 , log_domain == NULL ?
"" : log_domain
268 OutputDebugString((LPCSTR) msg);
272 , log_domain == NULL ?
"" : log_domain
279 #define PROCSELFEXE "/"
280 #define PROCOTHEREXE "/"
283 #define PROCSELFEXE "/proc/self/exe"
284 #define PROCOTHEREXE "/proc/%d/exe"
289 static gboolean created_pid_file = FALSE;
314 if (!g_file_test(pidfile, G_FILE_TEST_IS_REGULAR)) {
319 if (!g_file_get_contents(pidfile, &pidcontents, NULL, NULL)) {
324 pid = atoi(pidcontents);
325 g_free(pidcontents); pidcontents = NULL;
336 if(TerminateProcess((
void *)pid, 0) == 0)
338 if (kill(pid, 0) < 0 && errno != EPERM)
351 ret = GetModuleFileName(NULL, w_ourexepath, nSize);
356 ourexepath = g_strdup(w_ourexepath);
358 if (NULL == ourexepath) {
361 if (strrchr(ourexepath,
SEP) != NULL) {
362 ourexecmd = strrchr(ourexepath,
SEP)+1;
364 ourexecmd = ourexepath;
366 g_snprintf(pidexename,
sizeof(pidexename),
PROCOTHEREXE, pid);
369 pidexepath = g_file_read_link(pidexename, NULL);
370 if (pidexepath == NULL) {
371 g_free(ourexepath); ourexepath = NULL;
374 if (strrchr(pidexepath,
SEP) != NULL) {
375 pidexecmd = strrchr(pidexepath,
SEP)+1;
377 pidexecmd = pidexepath;
380 if (strcmp(ourexecmd, pidexecmd) == 0) {
383 g_free(ourexepath); ourexepath = NULL;
384 g_free(pidexepath); pidexepath = NULL;
389 g_free(ourexepath); ourexepath = NULL;
390 g_free(pidexepath); pidexepath = NULL;
399 GError* errptr = NULL;
408 #define GETPID GetCurrentProcessId()
410 #define GETPID getpid()
417 g_snprintf(pidbuf,
sizeof(pidbuf),
"%6d\n",
GETPID);
423 if (g_file_set_contents(pidfile, pidbuf, strlen(pidbuf), &errptr)) {
427 if (chmod(pidfile, 0644) < 0) {
428 g_warning(
"%s.%d: Could not chmod pid file %s to 0644",
__FUNCTION__, __LINE__
432 created_pid_file = TRUE;
435 g_critical(
"%s.%d: Cannot create pid file [%s]. Reason: %s"
437 fprintf(stderr,
"%s.%d: Cannot create pid file [%s]. Reason: %s\n"
446 p_pidfile = g_build_filename(
STD_PID_DIR, procname, NULL);
448 const char *
const *dirs = g_get_system_config_dirs();
449 p_pidfile = g_build_filename(dirs[0], procname, NULL);
458 if (created_pid_file) {
473 return kill((pid_t)service_pid, signal);
475 if(TerminateProcess((HANDLE) service_pid, signal) != 0) {
485 static const char * saved_pidfile = NULL;
491 struct sigaction sigact;
494 if (pidfile != NULL) {
495 saved_pidfile = pidfile;
498 memset(&sigact, 0,
sizeof(sigact));
500 sigaction(signal_in, &sigact, NULL);
546 gchar** newenv = NULL;
552 env = (
const gchar*
const*) newenv;
555 for (initenvcount = 0; env[initenvcount]; ++initenvcount) {
559 updatecount = update->
keycount(update);
563 result = (gchar**) g_malloc((updatecount+initenvcount+1)*
sizeof(gchar*));
566 GSList* updatekeys = NULL;
570 updatekeys = update->
keys(update);
573 for (thiskeylist = updatekeys; thiskeylist; thiskeylist=thiskeylist->next) {
574 char * thiskey = (
char *)thiskeylist->data;
576 GString * gsvalue = g_string_new(
"");
578 g_string_printf(gsvalue,
"%s=", thiskey);
583 g_string_append(gsvalue, update->
getbool(update, thiskey) ?
"true" :
"false");
587 g_string_append_printf(gsvalue,
FMT_64BIT"d", update->
getint(update,thiskey));
591 g_string_append(gsvalue, update->
getstring(update, thiskey));
596 g_string_append(gsvalue, s);
602 g_string_free(gsvalue, TRUE);
605 thiskeylist->data = thiskey = NULL;
608 result[resultelem] = g_string_free(gsvalue, FALSE);
613 thiskeylist->data = thiskey = NULL;
616 g_slist_free(updatekeys);
622 for (j = 0; env[j]; ++j) {
625 eqpos = strchr(env[j],
'=');
626 if (NULL == eqpos || eqpos == env[j]) {
629 envname = g_strndup(env[j], eqpos - env[j]);
632 result[resultelem] = g_strdup(env[j]);
637 result[resultelem] = NULL;