masqmail-0.2

view src/online.c @ 27:3654c502a4df

g_malloc terminates the program on failure automatically
author meillo@marmaro.de
date Thu, 06 May 2010 11:50:40 +0200
parents 99c09ed776c1
children e1004fcc93c9
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 gchar output[256];
49 if (fgets(output, 255, in)) {
50 g_strchomp(g_strchug(output));
51 if (strlen(output) == 0) {
52 logwrite(LOG_ALERT, "only whitespace connection name\n");
53 name = NULL;
54 } else {
55 name = g_strdup(output);
56 }
57 } else {
58 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
59 name = NULL;
60 }
61 fclose(in);
62 waitpid(pid, &status, 0);
63 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
64 g_free(name);
65 name = NULL;
66 }
67 } else
68 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
70 signal(SIGCHLD, old_signal);
72 return name;
73 }
75 gchar*
76 detect_online()
77 {
78 if (conf.online_detect != NULL) {
79 if (strcmp(conf.online_detect, "file") == 0) {
80 DEBUG(3) debugf("online detection method 'file'\n");
81 if (conf.online_file != NULL) {
82 struct stat st;
83 if (stat(conf.online_file, &st) == 0) {
84 FILE *fptr = fopen(conf.online_file, "r");
85 if (fptr) {
86 char buf[256];
87 if (fgets(buf, 256, fptr) == NULL) {
88 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
89 fclose(fptr);
90 return NULL;
91 }
92 g_strchomp(g_strchug(buf));
93 fclose(fptr);
94 if (strlen(buf) == 0) {
95 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
96 return NULL;
97 }
98 return g_strdup(buf);
99 } else {
100 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
101 return NULL;
102 }
103 } else if (errno == ENOENT) {
104 logwrite(LOG_NOTICE, "not online.\n");
105 return NULL;
106 } else {
107 logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
108 return NULL;
109 }
110 } else
111 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
112 #ifdef ENABLE_MSERVER
113 } else if (strcmp(conf.online_detect, "mserver") == 0) {
114 DEBUG(3) debugf("connection method 'mserver'\n");
115 return mserver_detect_online(conf.mserver_iface);
116 #endif
117 } else if (strcmp(conf.online_detect, "pipe") == 0) {
118 DEBUG(3) debugf("connection method 'pipe'\n");
119 if (conf.online_pipe)
120 return detect_online_pipe(conf.online_pipe);
121 else {
122 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
123 return NULL;
124 }
125 } else if (strcmp(conf.online_detect, "argument") == 0) {
126 return connection_name;
127 } else {
128 DEBUG(3) debugf("no connection method selected\n");
129 }
130 }
131 return NULL;
132 }