From 116ce19b10a1fd60776764974ad50776ff7c4714 Mon Sep 17 00:00:00 2001 From: Steven Danneman Date: Fri, 20 Feb 2009 15:10:21 -0800 Subject: Moved become_daemon() and close_low_fds() to shared util lib --- lib/util/become_daemon.c | 22 +++++++++++----------- lib/util/util.h | 9 ++++++--- 2 files changed, 17 insertions(+), 14 deletions(-) (limited to 'lib/util') diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c index 034114eade..5a97b65407 100644 --- a/lib/util/become_daemon.c +++ b/lib/util/become_daemon.c @@ -28,14 +28,15 @@ /******************************************************************* Close the low 3 fd's and open dev/null in their place. ********************************************************************/ -static void close_low_fds(bool stderr_too) + +_PUBLIC_ void close_low_fds(bool stderr_too) { #ifndef VALGRIND int fd; int i; close(0); - close(1); + close(1); if (stderr_too) close(2); @@ -61,11 +62,11 @@ static void close_low_fds(bool stderr_too) #endif } -/** +/**************************************************************************** Become a daemon, discarding the controlling terminal. -**/ +****************************************************************************/ -_PUBLIC_ void become_daemon(bool Fork) +_PUBLIC_ void become_daemon(bool Fork, bool no_process_group) { if (Fork) { if (fork()) { @@ -73,14 +74,14 @@ _PUBLIC_ void become_daemon(bool Fork) } } - /* detach from the terminal */ + /* detach from the terminal */ #ifdef HAVE_SETSID - setsid(); + if (!no_process_group) setsid(); #elif defined(TIOCNOTTY) - { - int i = open("/dev/tty", O_RDWR, 0); + if (!no_process_group) { + int i = sys_open("/dev/tty", O_RDWR, 0); if (i != -1) { - ioctl(i, (int) TIOCNOTTY, (char *)0); + ioctl(i, (int) TIOCNOTTY, (char *)0); close(i); } } @@ -90,4 +91,3 @@ _PUBLIC_ void become_daemon(bool Fork) close_low_fds(false); /* Don't close stderr, let the debug system attach it to the logfile */ } - diff --git a/lib/util/util.h b/lib/util/util.h index 7873f0e769..4d4df21600 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -724,12 +724,15 @@ _PUBLIC_ int idr_remove(struct idr_context *idp, int id); /* The following definitions come from lib/util/become_daemon.c */ -#if _SAMBA_BUILD_ == 4 +/** + Close the low 3 fd's and open dev/null in their place +**/ +_PUBLIC_ void close_low_fds(bool stderr_too); + /** Become a daemon, discarding the controlling terminal. **/ -_PUBLIC_ void become_daemon(bool fork); -#endif +_PUBLIC_ void become_daemon(bool fork, bool no_process_group); /** * Load a ini-style file. -- cgit From 39b508e38e831f826be254391f7f341bc935f792 Mon Sep 17 00:00:00 2001 From: Tim Prouty Date: Sat, 21 Feb 2009 13:54:43 -0800 Subject: Fix shadowed declaration warning --- lib/util/util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/util') diff --git a/lib/util/util.h b/lib/util/util.h index 4d4df21600..d3e446f488 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -732,7 +732,7 @@ _PUBLIC_ void close_low_fds(bool stderr_too); /** Become a daemon, discarding the controlling terminal. **/ -_PUBLIC_ void become_daemon(bool fork, bool no_process_group); +_PUBLIC_ void become_daemon(bool do_fork, bool no_process_group); /** * Load a ini-style file. -- cgit From 8d63c596a0f512c96f5663c0a9bd49d3c98c6df9 Mon Sep 17 00:00:00 2001 From: Steven Danneman Date: Mon, 23 Feb 2009 20:46:11 -0800 Subject: Refactored sys_fork() and sys_pid() into shared util library This fixes a bug in 116ce19b, where we didn't clear the pid cache in become_daemon() and thus the /var/run/smbd.pid didn't match the actual pid of the parent process. Currently S4 will clear the pid cache on fork but doesn't yet take advantage of the pid cache by using sys_pid() instead of the direct get_pid(). --- lib/util/become_daemon.c | 6 +++--- lib/util/system.c | 29 +++++++++++++++++++++++++++++ lib/util/util.h | 10 ++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) (limited to 'lib/util') diff --git a/lib/util/become_daemon.c b/lib/util/become_daemon.c index 5a97b65407..3d06a4363d 100644 --- a/lib/util/become_daemon.c +++ b/lib/util/become_daemon.c @@ -66,10 +66,10 @@ _PUBLIC_ void close_low_fds(bool stderr_too) Become a daemon, discarding the controlling terminal. ****************************************************************************/ -_PUBLIC_ void become_daemon(bool Fork, bool no_process_group) +_PUBLIC_ void become_daemon(bool do_fork, bool no_process_group) { - if (Fork) { - if (fork()) { + if (do_fork) { + if (sys_fork()) { _exit(0); } } diff --git a/lib/util/system.c b/lib/util/system.c index 9bd1800233..9bf5de1a83 100644 --- a/lib/util/system.c +++ b/lib/util/system.c @@ -88,3 +88,32 @@ _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host) return in2; } +/************************************************************************** + Wrapper for fork. Ensures we clear our pid cache. +****************************************************************************/ + +static pid_t mypid = (pid_t)-1; + +_PUBLIC_ pid_t sys_fork(void) +{ + pid_t forkret = fork(); + + if (forkret == (pid_t)0) { + /* Child - reset mypid so sys_getpid does a system call. */ + mypid = (pid_t) -1; + } + + return forkret; +} + +/************************************************************************** + Wrapper for getpid. Ensures we only do a system call *once*. +****************************************************************************/ + +_PUBLIC_ pid_t sys_getpid(void) +{ + if (mypid == (pid_t)-1) + mypid = getpid(); + + return mypid; +} diff --git a/lib/util/util.h b/lib/util/util.h index d3e446f488..27f94cd685 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -134,6 +134,16 @@ apparent reason. _PUBLIC_ struct hostent *sys_gethostbyname(const char *name); _PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host); +/** + * Wrapper for fork used to invalid pid cache. + **/ +_PUBLIC_ pid_t sys_fork(void); + +/** + * Wrapper for getpid. Ensures we only do a system call *once*. + **/ +_PUBLIC_ pid_t sys_getpid(void); + /* The following definitions come from lib/util/genrand.c */ /** -- cgit From c50233695e002d9f7c872821f7b90cdea632dd30 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 22 Feb 2009 19:47:54 +0100 Subject: Add tevent_req_is_unix_error --- lib/util/tevent_unix.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lib/util/tevent_unix.h | 27 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 lib/util/tevent_unix.c create mode 100644 lib/util/tevent_unix.h (limited to 'lib/util') diff --git a/lib/util/tevent_unix.c b/lib/util/tevent_unix.c new file mode 100644 index 0000000000..b89d5cd4d4 --- /dev/null +++ b/lib/util/tevent_unix.c @@ -0,0 +1,46 @@ +/* + Unix SMB/CIFS implementation. + Wrap unix errno around tevent_req + Copyright (C) Volker Lendecke 2009 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "tevent_unix.h" +#include "../replace/replace.h" + +bool tevent_req_is_unix_error(struct tevent_req *req, int *perrno) +{ + enum tevent_req_state state; + uint64_t err; + + if (!tevent_req_is_error(req, &state, &err)) { + return false; + } + switch (state) { + case TEVENT_REQ_TIMED_OUT: + *perrno = ETIMEDOUT; + break; + case TEVENT_REQ_NO_MEMORY: + *perrno = ENOMEM; + break; + case TEVENT_REQ_USER_ERROR: + *perrno = err; + break; + default: + *perrno = EINVAL; + break; + } + return true; +} diff --git a/lib/util/tevent_unix.h b/lib/util/tevent_unix.h new file mode 100644 index 0000000000..dc3ffaef33 --- /dev/null +++ b/lib/util/tevent_unix.h @@ -0,0 +1,27 @@ +/* + Unix SMB/CIFS implementation. + Wrap unix errno around tevent_req + Copyright (C) Volker Lendecke 2009 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _TEVENT_UNIX_H +#define _TEVENT_UNIX_H + +#include "../tevent/tevent.h" + +bool tevent_req_is_unix_error(struct tevent_req *req, int *perrno); + +#endif -- cgit From 4d22554e98134755cea609aa6d888c8e67fc123b Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 26 Feb 2009 10:48:41 +0100 Subject: Add tevent_ntstatus --- lib/util/tevent_ntstatus.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ lib/util/tevent_ntstatus.h | 32 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 lib/util/tevent_ntstatus.c create mode 100644 lib/util/tevent_ntstatus.h (limited to 'lib/util') diff --git a/lib/util/tevent_ntstatus.c b/lib/util/tevent_ntstatus.c new file mode 100644 index 0000000000..1a34e9c749 --- /dev/null +++ b/lib/util/tevent_ntstatus.c @@ -0,0 +1,51 @@ +/* + Unix SMB/CIFS implementation. + Wrap unix errno around tevent_req + Copyright (C) Volker Lendecke 2009 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "tevent_ntstatus.h" +#include "../replace/replace.h" + +bool tevent_req_nterror(struct tevent_req *req, NTSTATUS status) +{ + return tevent_req_error(req, NT_STATUS_V(status)); +} + +bool tevent_req_is_nterror(struct tevent_req *req, NTSTATUS *status) +{ + enum tevent_req_state state; + uint64_t err; + + if (!tevent_req_is_error(req, &state, &err)) { + return false; + } + switch (state) { + case TEVENT_REQ_TIMED_OUT: + *status = NT_STATUS_IO_TIMEOUT; + break; + case TEVENT_REQ_NO_MEMORY: + *status = NT_STATUS_NO_MEMORY; + break; + case TEVENT_REQ_USER_ERROR: + *status = NT_STATUS(err); + break; + default: + *status = NT_STATUS_INTERNAL_ERROR; + break; + } + return true; +} diff --git a/lib/util/tevent_ntstatus.h b/lib/util/tevent_ntstatus.h new file mode 100644 index 0000000000..84c275fb13 --- /dev/null +++ b/lib/util/tevent_ntstatus.h @@ -0,0 +1,32 @@ +/* + Unix SMB/CIFS implementation. + Wrap unix errno around tevent_req + Copyright (C) Volker Lendecke 2009 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _TEVENT_NTSTATUS_H +#define _TEVENT_NTSTATUS_H + +#include +#include +#include "config.h" +#include "../libcli/util/ntstatus.h" +#include "../tevent/tevent.h" + +bool tevent_req_nterror(struct tevent_req *req, NTSTATUS status); +bool tevent_req_is_nterror(struct tevent_req *req, NTSTATUS *pstatus); + +#endif -- cgit From d776ac03c3ce8a9090b847f20d2cdd16d745441f Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 1 Mar 2009 16:16:16 +0100 Subject: Move next_token_talloc() to top-level. --- lib/util/util.h | 15 ++++++++ lib/util/util_str.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) (limited to 'lib/util') diff --git a/lib/util/util.h b/lib/util/util.h index 27f94cd685..1f6e3b193b 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -204,6 +204,21 @@ _PUBLIC_ void display_set_stderr(void); /* The following definitions come from lib/util/util_str.c */ +bool next_token_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep); + +/** + * Get the next token from a string, return false if none found. Handles + * double-quotes. This version does not trim leading separator characters + * before looking for a token. + */ +bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep); + /** Trim the specified elements off the front and back of a string. diff --git a/lib/util/util_str.c b/lib/util/util_str.c index a2c50fd38f..8bf6026105 100644 --- a/lib/util/util_str.c +++ b/lib/util/util_str.c @@ -332,4 +332,104 @@ _PUBLIC_ void string_replace(char *s, char oldc, char newc) } } +/** + * @file + * @brief String utilities. + **/ + +static bool next_token_internal_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep, + bool ltrim) +{ + char *s; + char *saved_s; + char *pbuf; + bool quoted; + size_t len=1; + + *pp_buff = NULL; + if (!ptr) { + return(false); + } + + s = (char *)*ptr; + + /* default to simple separators */ + if (!sep) { + sep = " \t\n\r"; + } + + /* find the first non sep char, if left-trimming is requested */ + if (ltrim) { + while (*s && strchr_m(sep,*s)) { + s++; + } + } + + /* nothing left? */ + if (!*s) { + return false; + } + + /* When restarting we need to go from here. */ + saved_s = s; + + /* Work out the length needed. */ + for (quoted = false; *s && + (quoted || !strchr_m(sep,*s)); s++) { + if (*s == '\"') { + quoted = !quoted; + } else { + len++; + } + } + + /* We started with len = 1 so we have space for the nul. */ + *pp_buff = talloc_array(ctx, char, len); + if (!*pp_buff) { + return false; + } + + /* copy over the token */ + pbuf = *pp_buff; + s = saved_s; + for (quoted = false; *s && + (quoted || !strchr_m(sep,*s)); s++) { + if ( *s == '\"' ) { + quoted = !quoted; + } else { + *pbuf++ = *s; + } + } + + *ptr = (*s) ? s+1 : s; + *pbuf = 0; + + return true; +} + +bool next_token_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep) +{ + return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true); +} + +/* + * Get the next token from a string, return false if none found. Handles + * double-quotes. This version does not trim leading separator characters + * before looking for a token. + */ + +bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep) +{ + return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false); +} + -- cgit From 55903e6f9120f1ec58a8554813229975c3028a09 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 1 Mar 2009 16:19:38 +0100 Subject: Move next_token_talloc to util.c, as util_str.c is only compiled inside samba 4. --- lib/util/util.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/util/util_str.c | 100 ---------------------------------------------------- 2 files changed, 100 insertions(+), 100 deletions(-) (limited to 'lib/util') diff --git a/lib/util/util.c b/lib/util/util.c index 988d8f9fa0..1f31f55e8b 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -836,4 +836,104 @@ _PUBLIC_ size_t utf16_len_n(const void *src, size_t n) return len; } +/** + * @file + * @brief String utilities. + **/ + +static bool next_token_internal_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep, + bool ltrim) +{ + char *s; + char *saved_s; + char *pbuf; + bool quoted; + size_t len=1; + + *pp_buff = NULL; + if (!ptr) { + return(false); + } + + s = (char *)*ptr; + + /* default to simple separators */ + if (!sep) { + sep = " \t\n\r"; + } + + /* find the first non sep char, if left-trimming is requested */ + if (ltrim) { + while (*s && strchr_m(sep,*s)) { + s++; + } + } + + /* nothing left? */ + if (!*s) { + return false; + } + + /* When restarting we need to go from here. */ + saved_s = s; + + /* Work out the length needed. */ + for (quoted = false; *s && + (quoted || !strchr_m(sep,*s)); s++) { + if (*s == '\"') { + quoted = !quoted; + } else { + len++; + } + } + + /* We started with len = 1 so we have space for the nul. */ + *pp_buff = talloc_array(ctx, char, len); + if (!*pp_buff) { + return false; + } + + /* copy over the token */ + pbuf = *pp_buff; + s = saved_s; + for (quoted = false; *s && + (quoted || !strchr_m(sep,*s)); s++) { + if ( *s == '\"' ) { + quoted = !quoted; + } else { + *pbuf++ = *s; + } + } + + *ptr = (*s) ? s+1 : s; + *pbuf = 0; + + return true; +} + +bool next_token_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep) +{ + return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true); +} + +/* + * Get the next token from a string, return false if none found. Handles + * double-quotes. This version does not trim leading separator characters + * before looking for a token. + */ + +bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, + const char **ptr, + char **pp_buff, + const char *sep) +{ + return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false); +} + diff --git a/lib/util/util_str.c b/lib/util/util_str.c index 8bf6026105..a2c50fd38f 100644 --- a/lib/util/util_str.c +++ b/lib/util/util_str.c @@ -332,104 +332,4 @@ _PUBLIC_ void string_replace(char *s, char oldc, char newc) } } -/** - * @file - * @brief String utilities. - **/ - -static bool next_token_internal_talloc(TALLOC_CTX *ctx, - const char **ptr, - char **pp_buff, - const char *sep, - bool ltrim) -{ - char *s; - char *saved_s; - char *pbuf; - bool quoted; - size_t len=1; - - *pp_buff = NULL; - if (!ptr) { - return(false); - } - - s = (char *)*ptr; - - /* default to simple separators */ - if (!sep) { - sep = " \t\n\r"; - } - - /* find the first non sep char, if left-trimming is requested */ - if (ltrim) { - while (*s && strchr_m(sep,*s)) { - s++; - } - } - - /* nothing left? */ - if (!*s) { - return false; - } - - /* When restarting we need to go from here. */ - saved_s = s; - - /* Work out the length needed. */ - for (quoted = false; *s && - (quoted || !strchr_m(sep,*s)); s++) { - if (*s == '\"') { - quoted = !quoted; - } else { - len++; - } - } - - /* We started with len = 1 so we have space for the nul. */ - *pp_buff = talloc_array(ctx, char, len); - if (!*pp_buff) { - return false; - } - - /* copy over the token */ - pbuf = *pp_buff; - s = saved_s; - for (quoted = false; *s && - (quoted || !strchr_m(sep,*s)); s++) { - if ( *s == '\"' ) { - quoted = !quoted; - } else { - *pbuf++ = *s; - } - } - - *ptr = (*s) ? s+1 : s; - *pbuf = 0; - - return true; -} - -bool next_token_talloc(TALLOC_CTX *ctx, - const char **ptr, - char **pp_buff, - const char *sep) -{ - return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true); -} - -/* - * Get the next token from a string, return false if none found. Handles - * double-quotes. This version does not trim leading separator characters - * before looking for a token. - */ - -bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx, - const char **ptr, - char **pp_buff, - const char *sep) -{ - return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false); -} - -- cgit