git ssb

0+

cel / foostudio



Commit 9a4197f942e271323580995751fd9296ef58aad6

Use mkstemp. Delete temporary file immediately

cel committed on 10/25/2016, 12:33:06 AM
Parent: 2b3308729b9a63d56cc2c4712e8bab2aa818a492

Files changed

ccdl.cchanged
ccdl.hchanged
ccdl.cView
@@ -9,24 +9,22 @@
99 #include <pthread.h>
1010
1111 #include "ccdl.h"
1212
13-static unsigned int tmp_i = 0;
14-
1513 static const char *my_mktemp()
1614 {
1715 const char *tmpdir = getenv("TMPDIR");
1816 if (!tmpdir) tmpdir = "/tmp";
1917 static char buf[256];
20- if (snprintf(buf, sizeof(buf), "%s/ccdl-%d-%i.so",
21- tmpdir, getpid(), tmp_i++) < 0) return NULL;
18 + ssize_t len = snprintf(buf, sizeof(buf), "%s/ccdl-XXXXXX", tmpdir);
19 + if (len < 0) return NULL;
20 + if (mkstemp(buf) < 0) return NULL;
2221 return buf;
2322 }
2423
2524 void lib_init(struct lib *lib, const char *src_fname, const char *sym_name)
2625 {
2726 lib->src_fname = src_fname;
28- lib->lib_fname = NULL;
2927 lib->sym_name = sym_name;
3028 lib->handle = NULL;
3129 lib->sym = NULL;
3230 }
@@ -57,18 +55,24 @@
5755 int lib_build(struct lib *lib)
5856 {
5957 void *handle = lib->handle;
6058 if (!handle) {
61- const char *lib_fname = lib->lib_fname;
59 + const char *src_fname = lib->src_fname;
60 + if (!src_fname) return -1;
61 + const char *lib_fname = my_mktemp();
6262 if (!lib_fname) {
63- const char *src_fname = lib->src_fname;
64- if (!src_fname) return -1;
65- lib_fname = my_mktemp();
66- if (!lib_fname) return -1;
67- lib->lib_fname = lib_fname;
68- if (compile(src_fname, lib_fname)) return -1;
63 + warn("mktemp");
64 + return -1;
6965 }
66 + if (compile(src_fname, lib_fname)) return -1;
67 + dlerror();
7068 handle = dlopen(lib_fname, RTLD_NOW);
69 + char *err = dlerror();
70 + if (unlink(lib_fname) < 0) warn("unlink");
71 + if (err) {
72 + warnx("dlopen: %s", err);
73 + return -1;
74 + }
7175 lib->handle = handle;
7276 }
7377 const char *sym_name = lib->sym_name;
7478 if (!sym_name) return -1;
@@ -90,12 +94,8 @@
9094 dlclose(lib->handle);
9195 lib->handle = NULL;
9296 lib->sym = NULL;
9397 }
94- if (lib->lib_fname) {
95- unlink(lib->lib_fname);
96- lib->lib_fname = NULL;
97- }
9898 }
9999
100100 void ccdl_init(struct ccdl *ccdl, const char *src_fname, const char *sym_name)
101101 {
ccdl.hView
@@ -3,9 +3,8 @@
33 #include <pthread.h>
44
55 struct lib {
66 const char *src_fname;
7- const char *lib_fname;
87 const char *sym_name;
98 void *handle;
109 void *sym;
1110 };

Built with git-ssb-web