root/trunk/wifidog/src/util.c @ 256

Revision 256, 3.4 KB (checked in by alexcv, 9 years ago)

Extra mutex logging and extra logging in ping_thread()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/********************************************************************\
2 * This program is free software; you can redistribute it and/or    *
3 * modify it under the terms of the GNU General Public License as   *
4 * published by the Free Software Foundation; either version 2 of   *
5 * the License, or (at your option) any later version.              *
6 *                                                                  *
7 * This program is distributed in the hope that it will be useful,  *
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
10 * GNU General Public License for more details.                     *
11 *                                                                  *
12 * You should have received a copy of the GNU General Public License*
13 * along with this program; if not, contact:                        *
14 *                                                                  *
15 * Free Software Foundation           Voice:  +1-617-542-5942       *
16 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
17 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
18 *                                                                  *
19 \********************************************************************/
20
21/*
22 * $Header$
23 */
24/**
25  @file util.c
26  @brief Misc utility functions
27  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com>
28 */
29
30#define _GNU_SOURCE
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <syslog.h>
35#include <errno.h>
36#include <pthread.h>
37#include <sys/wait.h>
38#include <sys/types.h>
39#include <sys/unistd.h>
40#include <netinet/in.h>
41
42#include <string.h>
43#include <pthread.h>
44#include <netdb.h>
45
46#include "util.h"
47#include "conf.h"
48#include "debug.h"
49
50static pthread_mutex_t ghbn_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52/** Fork a child and execute a shell command, the parent
53 * process waits for the child to return and returns the child's exit()
54 * value.
55 * @return Return code of the command
56 */
57int
58execute(char *cmd_line, int quiet)
59{
60    int pid,
61        status,
62        rc;
63
64    const char *new_argv[4];
65    new_argv[0] = "/bin/sh";
66    new_argv[1] = "-c";
67    new_argv[2] = cmd_line;
68    new_argv[3] = NULL;
69
70    if ((pid = fork()) < 0) {    /* fork a child process           */
71        debug(LOG_ERR, "fork(): %s", strerror(errno));
72        exit(1);
73    } else if (pid == 0) {    /* for the child process:         */
74        /* We don't want to see any errors if quiet flag is on */
75        if (quiet) close(2);
76        if (execvp("/bin/sh", (char *const *)new_argv) < 0) {    /* execute the command  */
77            debug(LOG_ERR, "execvp(): %s", strerror(errno));
78            exit(1);
79        }
80    } else {        /* for the parent:      */
81        do {
82            rc = wait(&status);
83        } while (rc != pid && rc != -1);    /* wait for completion  */
84    }
85
86    return (WEXITSTATUS(status));
87}
88
89struct in_addr *
90wd_gethostbyname(const char *name)
91{
92        struct hostent *he;
93        struct in_addr *h_addr, *in_addr_temp;
94
95        /* XXX Calling function is reponsible for free() */
96
97        h_addr = (struct in_addr *)malloc(sizeof(struct in_addr));
98       
99        if (h_addr == NULL)
100                return NULL;
101       
102        LOCK_GHBN();
103
104        he = gethostbyname(name);
105
106        if (he == NULL) {
107                free(h_addr);
108                UNLOCK_GHBN();
109                return NULL;
110        }
111
112        in_addr_temp = (struct in_addr *)he->h_addr_list[0];
113        h_addr->s_addr = in_addr_temp->s_addr;
114       
115        UNLOCK_GHBN();
116
117        return h_addr;
118}
Note: See TracBrowser for help on using the browser.