Dave Shield wrote:
> There's nothing inherently agent-like in creating a temporary file.
> This could potentially be useful for an application too.
> 
> +1 for moving it to the core library.

How about the attached patch?

I must admit that I'm not fully convinced the temporary file handling in
mibgroup/utilities/execute.c:run_shell_command() combined with
make_tempfile/netsnmp_mktemp is safe, but I consider this out of scope for the
current library layering discussion. (Reviews appreciated, though.)


+Thomas

-- 
Thomas Anders (thomas.anders at blue-cable.de)
Index: agent/mibgroup/util_funcs.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/agent/mibgroup/util_funcs.c,v
retrieving revision 5.23
diff -u -r5.23 util_funcs.c
--- agent/mibgroup/util_funcs.c	1 Jan 2007 17:44:20 -0000	5.23
+++ agent/mibgroup/util_funcs.c	8 Jan 2007 23:08:53 -0000
@@ -117,6 +117,7 @@
     exit(var);
 }
 
+/** deprecated, use netsnmp_mktemp instead */
 const char *
 make_tempfile(void)
 {
Index: agent/mibgroup/utilities/execute.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/agent/mibgroup/utilities/execute.c,v
retrieving revision 1.15
diff -u -r1.15 execute.c
--- agent/mibgroup/utilities/execute.c	26 Dec 2006 16:52:36 -0000	1.15
+++ agent/mibgroup/utilities/execute.c	8 Jan 2007 23:08:53 -0000
@@ -61,7 +61,7 @@
     if (input) {
         FILE       *file;
 
-        ifname = make_tempfile();
+        ifname = netsnmp_mktemp();
         if(NULL == ifname)
             return -1;
         file = fopen(ifname, "w");
@@ -74,7 +74,7 @@
         fclose( file );
 
         if (output) {
-            ofname = make_tempfile();
+            ofname = netsnmp_mktemp();
             if(NULL == ofname) {
                 if(ifname)
                     unlink(ifname);
@@ -90,7 +90,7 @@
     } else {
         ifname = NULL;   /* Just to shut the compiler up! */
         if (output) {
-            ofname = make_tempfile();
+            ofname = netsnmp_mktemp();
             if(NULL == ofname)
                 return -1;
             snprintf( shellline, sizeof(shellline), "(%s) > \"%s\"",
Index: include/net-snmp/library/system.h
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/include/net-snmp/library/system.h,v
retrieving revision 5.14
diff -u -r5.14 system.h
--- include/net-snmp/library/system.h	15 Sep 2006 14:14:20 -0000	5.14
+++ include/net-snmp/library/system.h	8 Jan 2007 23:08:53 -0000
@@ -140,6 +140,7 @@
 
     int             mkdirhier(const char *pathname, mode_t mode,
                               int skiplast);
+    const char     *netsnmp_mktemp(void);
 #ifndef HAVE_STRLCPY
     size_t            strlcpy(char *, const char *, size_t);
 #endif
Index: snmplib/system.c
===================================================================
RCS file: /cvsroot/net-snmp/net-snmp/snmplib/system.c,v
retrieving revision 5.36
diff -u -r5.36 system.c
--- snmplib/system.c	2 Jan 2007 23:08:20 -0000	5.36
+++ snmplib/system.c	8 Jan 2007 23:08:53 -0000
@@ -1078,6 +1078,46 @@
     return SNMPERR_SUCCESS;
 }
 
+/**
+ * netsnmp_mktemp creates a temporary file based on the
+ *                 configured tempFilePattern
+ *
+ * @return file descriptor
+ */
+const char     *
+netsnmp_mktemp(void)
+{
+    static char     name[32];
+    int             fd = -1;
+
+    strcpy(name, get_temp_file_pattern());
+#ifdef HAVE_MKSTEMP
+    fd = mkstemp(name);
+#else
+    if (mktemp(name)) {
+# ifndef WIN32
+        fd = open(name, O_CREAT | O_EXCL | O_WRONLY);
+# else
+        /*
+         * Win32 needs _S_IREAD | _S_IWRITE to set permissions on file
+         * after closing
+         */
+        fd = _open(name, _O_CREAT,
+                   _S_IREAD | _S_IWRITE | _O_EXCL | _O_WRONLY);
+# endif
+    }
+#endif
+    if (fd >= 0) {
+        close(fd);
+        DEBUGMSGTL(("netsnmp_mktemp", "temp file created: %s\n",
+                    name));
+        return name;
+    }
+    snmp_log(LOG_ERR, "netsnmp_mktemp: error creating file %s\n",
+             name);
+    return NULL;
+}
+
 /*
  * This function was created to differentiate actions
  * that are appropriate for Linux 2.4 kernels, but not later kernels.
@@ -1147,3 +1187,4 @@
   return -1;
 #endif
 }
+
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to