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

Revision 135, 2.9 KB (checked in by alexcv, 9 years ago)

UNTESTED!!! Massive update based on the code review wed. It compiles...

  • 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: /cvsroot/wifidog/wifidog/src/firewall.c,v 1.32 2004/04/23
23 * 11:37:43 aprilp Exp $
24 */
25/** @internal
26  @file firewall.c
27  @brief Firewall update functions
28  @author Copyright (C) 2004 Philippe April <papril777@yahoo.com>
29 */
30
31#define _GNU_SOURCE
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <syslog.h>
36#include <errno.h>
37#include <pthread.h>
38#include <sys/wait.h>
39#include <sys/types.h>
40#include <sys/unistd.h>
41
42#include <string.h>
43
44#include "util.h"
45#include "conf.h"
46#include "debug.h"
47
48/** @brief Execute a shell command
49 *
50 * Fork a child and execute a shell command, the parent
51 * process waits for the child to return and returns the child's exit()
52 * value.
53 * @return Return code of the command
54 */
55int
56execute(char *cmd_line, int quiet)
57{
58    int pid,
59        status,
60        rc;
61
62    const char *new_argv[4];
63    new_argv[0] = "/bin/sh";
64    new_argv[1] = "-c";
65    new_argv[2] = cmd_line;
66    new_argv[3] = NULL;
67
68    if ((pid = fork()) < 0) {    /* fork a child process           */
69        debug(LOG_ERR, "fork(): %s", strerror(errno));
70        exit(1);
71    } else if (pid == 0) {    /* for the child process:         */
72        /* We don't want to see any errors if quiet flag is on */
73        if (quiet) close(2);
74        if (execvp("/bin/sh", (char *const *)new_argv) < 0) {    /* execute the command  */
75            debug(LOG_ERR, "fork(): %s", strerror(errno));
76            exit(1);
77        }
78    } else {        /* for the parent:      */
79        do {
80            rc = wait(&status);
81        } while (rc != pid && rc != -1);    /* wait for completion  */
82    }
83
84    return (WEXITSTATUS(status));
85}
Note: See TracBrowser for help on using the browser.