masqmail

view src/online.c @ 74:0a5b2e96ade3

do not reparent the daemon to init if init is already the parent
author meillo@marmaro.de
date Wed, 16 Jun 2010 10:39:44 +0200
parents e1004fcc93c9
children 3b344bf57162
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright 2008 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include <sys/stat.h>
21 #include <sys/wait.h>
23 #include "masqmail.h"
24 #include "mserver.h"
25 #include "peopen.h"
27 gchar *connection_name;
29 void
30 set_online_name(gchar * name)
31 {
32 connection_name = g_strdup(name);
33 }
35 static gchar*
36 detect_online_pipe(const gchar * pipe)
37 {
38 pid_t pid;
39 void (*old_signal) (int);
40 int status;
41 FILE *in;
42 gchar *name = NULL;
44 old_signal = signal(SIGCHLD, SIG_DFL);
46 in = peopen(pipe, "r", environ, &pid);
47 if (in == NULL) {
48 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
49 signal(SIGCHLD, old_signal);
50 return NULL;
51 }
53 gchar output[256];
54 if (fgets(output, 255, in)) {
55 g_strchomp(g_strchug(output));
56 if (strlen(output) == 0) {
57 logwrite(LOG_ALERT, "only whitespace connection name\n");
58 name = NULL;
59 } else {
60 name = g_strdup(output);
61 }
62 } else {
63 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
64 name = NULL;
65 }
66 fclose(in);
67 waitpid(pid, &status, 0);
68 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
69 g_free(name);
70 name = NULL;
71 }
73 signal(SIGCHLD, old_signal);
75 return name;
76 }
78 gchar*
79 detect_online()
80 {
81 if (conf.online_detect == NULL) {
82 return NULL;
83 }
85 if (strcmp(conf.online_detect, "file") == 0) {
86 DEBUG(3) debugf("online detection method 'file'\n");
87 if (conf.online_file == NULL) {
88 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
89 return NULL;
90 }
92 struct stat st;
93 if (stat(conf.online_file, &st) == 0) {
94 FILE *fptr = fopen(conf.online_file, "r");
95 if (!fptr) {
96 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
97 return NULL;
98 }
99 char buf[256];
100 if (fgets(buf, 256, fptr) == NULL) {
101 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
102 fclose(fptr);
103 return NULL;
104 }
105 g_strchomp(g_strchug(buf));
106 fclose(fptr);
107 if (strlen(buf) == 0) {
108 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
109 return NULL;
110 }
111 return g_strdup(buf);
112 } else if (errno == ENOENT) {
113 logwrite(LOG_NOTICE, "not online.\n");
114 return NULL;
115 } else {
116 logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
117 return NULL;
118 }
120 #ifdef ENABLE_MSERVER
121 } else if (strcmp(conf.online_detect, "mserver") == 0) {
122 DEBUG(3) debugf("connection method 'mserver'\n");
123 return mserver_detect_online(conf.mserver_iface);
124 #endif
125 } else if (strcmp(conf.online_detect, "pipe") == 0) {
126 DEBUG(3) debugf("connection method 'pipe'\n");
127 if (conf.online_pipe)
128 return detect_online_pipe(conf.online_pipe);
129 else {
130 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
131 return NULL;
132 }
133 } else if (strcmp(conf.online_detect, "argument") == 0) {
134 return connection_name;
135 } else {
136 DEBUG(3) debugf("no connection method selected\n");
137 }
139 return NULL;
140 }