lib-util: Make useful function a common utility.
authorSimo Sorce <idra@samba.org>
Thu, 21 Apr 2011 13:45:27 +0000 (09:45 -0400)
committerAndreas Schneider <asn@samba.org>
Wed, 10 Aug 2011 16:14:02 +0000 (18:14 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
lib/util/util.c
lib/util/util.h
source3/smbd/process.c

index 7f30d436e8e0eee5b5fb4069f05477f9d948b1b1..4dd79266aff240eb28e5196005ecadf87dbce6ba 100644 (file)
@@ -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.
 **/
index b9734b07c88e0e05c9f95bf9c55b30934cc63fda..7f0de26781bda2ad73478300f05711bfc8a0f5ce 100644 (file)
@@ -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.
 **/
index fc6112c16130333fa0167d96997f6ed03c463b17..d96d5fb7e0135fc9f14eca70ab70a57541d94c34 100644 (file)
@@ -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.