masqmail

annotate src/online.c @ 388:aa40710f09fe

Refactoring and code layouting.
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 13:37:40 +0100
parents 41958685480d
children
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 1999-2001 Oliver Kurth
meillo@367 4 ** Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>
meillo@367 5 **
meillo@367 6 ** This program is free software; you can redistribute it and/or modify
meillo@367 7 ** it under the terms of the GNU General Public License as published by
meillo@367 8 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 9 ** (at your option) any later version.
meillo@367 10 **
meillo@367 11 ** This program is distributed in the hope that it will be useful,
meillo@367 12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 14 ** GNU General Public License for more details.
meillo@367 15 **
meillo@367 16 ** You should have received a copy of the GNU General Public License
meillo@367 17 ** along with this program; if not, write to the Free Software
meillo@367 18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 19 */
meillo@0 20
meillo@0 21 #include <sys/wait.h>
meillo@15 22
meillo@0 23 #include "masqmail.h"
meillo@0 24 #include "peopen.h"
meillo@0 25
meillo@0 26
meillo@310 27 gchar*
meillo@310 28 online_query()
meillo@0 29 {
meillo@366 30 gchar *pipe = conf.online_query;
meillo@10 31 pid_t pid;
meillo@10 32 void (*old_signal) (int);
meillo@10 33 int status;
meillo@10 34 FILE *in;
meillo@10 35 gchar *name = NULL;
meillo@0 36
meillo@310 37 if (!conf.online_query) {
meillo@310 38 return NULL;
meillo@310 39 }
meillo@310 40 DEBUG(3) debugf("online query `%s'\n", pipe);
meillo@310 41
meillo@10 42 old_signal = signal(SIGCHLD, SIG_DFL);
meillo@0 43
meillo@10 44 in = peopen(pipe, "r", environ, &pid);
meillo@310 45 if (!in) {
meillo@33 46 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
meillo@33 47 signal(SIGCHLD, old_signal);
meillo@33 48 return NULL;
meillo@33 49 }
meillo@33 50
meillo@33 51 gchar output[256];
meillo@33 52 if (fgets(output, 255, in)) {
meillo@33 53 g_strchomp(g_strchug(output));
meillo@33 54 if (strlen(output) == 0) {
meillo@33 55 logwrite(LOG_ALERT, "only whitespace connection name\n");
meillo@33 56 name = NULL;
meillo@18 57 } else {
meillo@33 58 name = g_strdup(output);
meillo@10 59 }
meillo@33 60 } else {
meillo@33 61 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
meillo@33 62 name = NULL;
meillo@33 63 }
meillo@33 64 fclose(in);
meillo@33 65 waitpid(pid, &status, 0);
meillo@262 66 if (WEXITSTATUS(status) != 0) {
meillo@33 67 g_free(name);
meillo@33 68 name = NULL;
meillo@33 69 }
meillo@0 70
meillo@10 71 signal(SIGCHLD, old_signal);
meillo@0 72
meillo@10 73 return name;
meillo@0 74 }