From 05455b459a93f620143d90fd34a28e4485fd606a Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Thu, 21 Apr 2011 09:45:27 -0400 Subject: [PATCH] lib-util: Make useful function a common utility. Signed-off-by: Andreas Schneider --- lib/util/util.c | 32 ++++++++++++++++++++++++++++++++ lib/util/util.h | 5 +++++ source3/smbd/process.c | 26 -------------------------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/lib/util/util.c b/lib/util/util.c index 7f30d436e8e..4dd79266aff 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -6,6 +6,7 @@ Copyright (C) Simo Sorce 2001 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003. Copyright (C) James J Myers 2003 + Copyright (C) Volker Lendecke 2010 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 @@ -60,6 +61,37 @@ _PUBLIC_ const char *tmpdir(void) } +/** + Create a tmp file, open it and immediately unlink it. + Returns the file descriptor or -1 on error. +**/ +int create_unlink_tmp(const char *dir) +{ + char *fname; + int fd; + + fname = talloc_asprintf(talloc_tos(), "%s/listenerlock_XXXXXX", dir); + if (fname == NULL) { + errno = ENOMEM; + return -1; + } + fd = mkstemp(fname); + if (fd == -1) { + TALLOC_FREE(fname); + return -1; + } + if (unlink(fname) == -1) { + int sys_errno = errno; + close(fd); + TALLOC_FREE(fname); + errno = sys_errno; + return -1; + } + TALLOC_FREE(fname); + return fd; +} + + /** Check if a file exists - call vfs_file_exist for samba files. **/ diff --git a/lib/util/util.h b/lib/util/util.h index b9734b07c88..7f0de26781b 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -622,6 +622,11 @@ bool file_compare(const char *path1, const char *path2); **/ _PUBLIC_ const char *tmpdir(void); +/** + * Creates and immediately unlinks a file. Returns open file descriptor. + **/ +_PUBLIC_ int create_unlink_tmp(const char *dir); + /** Check if a file exists - call vfs_file_exist for samba files. **/ diff --git a/source3/smbd/process.c b/source3/smbd/process.c index fc6112c1613..d96d5fb7e01 100644 --- a/source3/smbd/process.c +++ b/source3/smbd/process.c @@ -2422,32 +2422,6 @@ static bool housekeeping_fn(const struct timeval *now, void *private_data) return true; } -static int create_unlink_tmp(const char *dir) -{ - char *fname; - int fd; - - fname = talloc_asprintf(talloc_tos(), "%s/listenerlock_XXXXXX", dir); - if (fname == NULL) { - errno = ENOMEM; - return -1; - } - fd = mkstemp(fname); - if (fd == -1) { - TALLOC_FREE(fname); - return -1; - } - if (unlink(fname) == -1) { - int sys_errno = errno; - close(fd); - TALLOC_FREE(fname); - errno = sys_errno; - return -1; - } - TALLOC_FREE(fname); - return fd; -} - /* * Read an smb packet in the echo handler child, giving the parent * smbd one second to react once the socket becomes readable. -- 2.34.1