From patchwork Tue Dec 11 10:02:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pontus Fuchs X-Patchwork-Id: 205143 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from maxx.maxx.shmoo.com (maxx.shmoo.com [205.134.188.171]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "maxx.shmoo.com", Issuer "CA Cert Signing Authority" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 1B9872C00A3 for ; Tue, 11 Dec 2012 21:02:34 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id E32F69C16C; Tue, 11 Dec 2012 05:02:31 -0500 (EST) X-Virus-Scanned: amavisd-new at maxx.shmoo.com Received: from maxx.maxx.shmoo.com ([127.0.0.1]) by localhost (maxx.shmoo.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KSIwOoZjq+Gy; Tue, 11 Dec 2012 05:02:31 -0500 (EST) Received: from maxx.shmoo.com (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id 9E5E29C16D; Tue, 11 Dec 2012 05:02:27 -0500 (EST) X-Original-To: mailman-post+hostap@maxx.shmoo.com Delivered-To: mailman-post+hostap@maxx.shmoo.com Received: from localhost (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id 1A30E9C16D for ; Tue, 11 Dec 2012 05:02:26 -0500 (EST) X-Virus-Scanned: amavisd-new at maxx.shmoo.com Received: from maxx.maxx.shmoo.com ([127.0.0.1]) by localhost (maxx.shmoo.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5tp2htoSCdED for ; Tue, 11 Dec 2012 05:02:21 -0500 (EST) Received: from mail-la0-f44.google.com (mail-la0-f44.google.com [209.85.215.44]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority" (not verified)) by maxx.maxx.shmoo.com (Postfix) with ESMTPS id A6BF79C16C for ; Tue, 11 Dec 2012 05:02:20 -0500 (EST) Received: by mail-la0-f44.google.com with SMTP id d3so3702946lah.17 for ; Tue, 11 Dec 2012 02:02:17 -0800 (PST) Received: by 10.112.102.71 with SMTP id fm7mr6865842lbb.10.1355220137771; Tue, 11 Dec 2012 02:02:17 -0800 (PST) Received: from localhost.localdomain (host-95-195-138-192.mobileonline.telia.com. [95.195.138.192]) by mx.google.com with ESMTPS id sj3sm8963327lab.2.2012.12.11.02.02.15 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 11 Dec 2012 02:02:16 -0800 (PST) From: Pontus Fuchs To: hostap@lists.shmoo.com Subject: [PATCH] Allow PID file to be written in foreground mode Date: Tue, 11 Dec 2012 11:02:05 +0100 Message-Id: <1355220125-3764-1-git-send-email-pontus.fuchs@gmail.com> X-Mailer: git-send-email 1.7.10.4 X-BeenThere: hostap@lists.shmoo.com X-Mailman-Version: 2.1.9 Precedence: list List-Id: HostAP Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: hostap-bounces@lists.shmoo.com Errors-To: hostap-bounces@lists.shmoo.com I wanted hostapd to write a PID file in foreground mode but I found that it couldn't that. I will silently ignore the -P option when -B is not specified on the command line. Fix this by moving the PID file writing from os_daemonize to a new function. Signed-hostap: Pontus Fuchs --- hostapd/hostapd_cli.c | 3 ++- hostapd/main.c | 3 ++- src/utils/os.h | 10 ++++++++-- src/utils/os_internal.c | 12 +++++++++--- src/utils/os_none.c | 8 +++++++- src/utils/os_unix.c | 12 ++++++++---- src/utils/os_win32.c | 8 +++++++- wpa_supplicant/wpa_cli.c | 3 +++ wpa_supplicant/wpa_priv.c | 3 ++- wpa_supplicant/wpa_supplicant.c | 8 ++++---- 10 files changed, 52 insertions(+), 18 deletions(-) diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c index 633c13d..cf6d88b 100644 --- a/hostapd/hostapd_cli.c +++ b/hostapd/hostapd_cli.c @@ -1096,8 +1096,9 @@ int main(int argc, char *argv[]) } } - if (daemonize && os_daemonize(pid_file)) + if (daemonize && os_daemonize()) return -1; + os_write_pid_file(pid_file); if (interactive) hostapd_cli_interactive(); diff --git a/hostapd/main.c b/hostapd/main.c index 56f0002..d13a742 100644 --- a/hostapd/main.c +++ b/hostapd/main.c @@ -451,10 +451,11 @@ static int hostapd_global_run(struct hapd_interfaces *ifaces, int daemonize, } #endif /* EAP_SERVER_TNC */ - if (daemonize && os_daemonize(pid_file)) { + if (daemonize && os_daemonize()) { perror("daemon"); return -1; } + os_write_pid_file(pid_file); eloop_run(); diff --git a/src/utils/os.h b/src/utils/os.h index ad20834..18d8dc6 100644 --- a/src/utils/os.h +++ b/src/utils/os.h @@ -77,10 +77,9 @@ int os_gmtime(os_time_t t, struct os_tm *tm); /** * os_daemonize - Run in the background (detach from the controlling terminal) - * @pid_file: File name to write the process ID to or %NULL to skip this * Returns: 0 on success, -1 on failure */ -int os_daemonize(const char *pid_file); +int os_daemonize(); /** * os_daemonize_terminate - Stop running in the background (remove pid file) @@ -89,6 +88,13 @@ int os_daemonize(const char *pid_file); void os_daemonize_terminate(const char *pid_file); /** + * os_write_pid_file - Write current process ID to pid_file + * @pid_file: File name to write the process ID to or %NULL to skip this + * Returns: 0 on success, -1 on failure + */ +int os_write_pid_file(const char *pid_file); + +/** * os_get_random - Get cryptographically strong pseudo random data * @buf: Buffer for pseudo random data * @len: Length of the buffer diff --git a/src/utils/os_internal.c b/src/utils/os_internal.c index e4b7fdb..3ebae06 100644 --- a/src/utils/os_internal.c +++ b/src/utils/os_internal.c @@ -82,22 +82,28 @@ int os_gmtime(os_time_t t, struct os_tm *tm) } -int os_daemonize(const char *pid_file) +int os_daemonize() { if (daemon(0, 0)) { perror("daemon"); return -1; } + return 0; +} + + +int os_write_pid_file(const char *pid_file) +{ if (pid_file) { FILE *f = fopen(pid_file, "w"); if (f) { fprintf(f, "%u\n", getpid()); fclose(f); + return 0; } } - - return -0; + return -1; } diff --git a/src/utils/os_none.c b/src/utils/os_none.c index cabf73b..cf02476 100644 --- a/src/utils/os_none.c +++ b/src/utils/os_none.c @@ -38,7 +38,13 @@ int os_gmtime(os_time_t t, struct os_tm *tm) } -int os_daemonize(const char *pid_file) +int os_daemonize() +{ + return -1; +} + + +int os_write_pid_file(const char *pid_file) { return -1; } diff --git a/src/utils/os_unix.c b/src/utils/os_unix.c index 23a93be..1248a17 100644 --- a/src/utils/os_unix.c +++ b/src/utils/os_unix.c @@ -153,7 +153,7 @@ static int os_daemon(int nochdir, int noclose) #endif /* __APPLE__ */ -int os_daemonize(const char *pid_file) +int os_daemonize() { #if defined(__uClinux__) || defined(__sun__) return -1; @@ -162,17 +162,21 @@ int os_daemonize(const char *pid_file) perror("daemon"); return -1; } + return 0; +#endif /* defined(__uClinux__) || defined(__sun__) */ +} +int os_write_pid_file(const char *pid_file) +{ if (pid_file) { FILE *f = fopen(pid_file, "w"); if (f) { fprintf(f, "%u\n", getpid()); fclose(f); + return 0; } } - - return -0; -#endif /* defined(__uClinux__) || defined(__sun__) */ + return -1; } diff --git a/src/utils/os_win32.c b/src/utils/os_win32.c index 163cebe..b5a01a9 100644 --- a/src/utils/os_win32.c +++ b/src/utils/os_win32.c @@ -105,13 +105,19 @@ int os_gmtime(os_time_t t, struct os_tm *tm) } -int os_daemonize(const char *pid_file) +int os_daemonize() { /* TODO */ return -1; } +int os_write_pid_file(const char *pid_file) +{ + return -1; +} + + void os_daemonize_terminate(const char *pid_file) { } diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c index 61bb7fd..2591ab7 100644 --- a/wpa_supplicant/wpa_cli.c +++ b/wpa_supplicant/wpa_cli.c @@ -3482,8 +3482,11 @@ int main(int argc, char *argv[]) } } + if (daemonize && os_daemonize()) + return -1; if (daemonize && os_daemonize(pid_file)) return -1; + os_write_pid_file(pid_file); if (action_file) wpa_cli_action(ctrl_conn); diff --git a/wpa_supplicant/wpa_priv.c b/wpa_supplicant/wpa_priv.c index ad6a080..149ae89 100644 --- a/wpa_supplicant/wpa_priv.c +++ b/wpa_supplicant/wpa_priv.c @@ -1008,8 +1008,9 @@ int main(int argc, char *argv[]) interfaces = iface; } - if (daemonize && os_daemonize(pid_file)) + if (daemonize && os_daemonize()) goto out; + os_write_pid_file(pid_file); eloop_register_signal_terminate(wpa_priv_terminate, NULL); eloop_run(); diff --git a/wpa_supplicant/wpa_supplicant.c b/wpa_supplicant/wpa_supplicant.c index 784a471..74bec00 100644 --- a/wpa_supplicant/wpa_supplicant.c +++ b/wpa_supplicant/wpa_supplicant.c @@ -2400,10 +2400,10 @@ int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s) } -static int wpa_supplicant_daemon(const char *pid_file) +static int wpa_supplicant_daemon() { wpa_printf(MSG_DEBUG, "Daemonize.."); - return os_daemonize(pid_file); + return os_daemonize(); } @@ -3246,9 +3246,9 @@ int wpa_supplicant_run(struct wpa_global *global) { struct wpa_supplicant *wpa_s; - if (global->params.daemonize && - wpa_supplicant_daemon(global->params.pid_file)) + if (global->params.daemonize && wpa_supplicant_daemon()) return -1; + os_write_pid_file(global->params.pid_file); if (global->params.wait_for_monitor) { for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)