Browse Source

Make all file opens close on exec to prevent leaving files open on existing and possible future execves

master
Con Kolivas 10 years ago
parent
commit
97b03d016f
  1. 18
      src/ckdb.c
  2. 8
      src/ckpool.c
  3. 2
      src/ktree.c
  4. 6
      src/libckpool.c
  5. 6
      src/stratifier.c

18
src/ckdb.c

@ -1337,7 +1337,7 @@ static bool rotating_log_nolock(char *msg)
bool ok = false; bool ok = false;
filename = rotating_filename(logname, time(NULL)); filename = rotating_filename(logname, time(NULL));
fp = fopen(filename, "a+"); fp = fopen(filename, "a+e");
if (unlikely(!fp)) { if (unlikely(!fp)) {
LOGERR("Failed to fopen %s in rotating_log!", filename); LOGERR("Failed to fopen %s in rotating_log!", filename);
goto stageleft; goto stageleft;
@ -6100,7 +6100,7 @@ static bool reload()
start.tv_sec = DATE_BEGIN; start.tv_sec = DATE_BEGIN;
start.tv_usec = 0L; start.tv_usec = 0L;
filename = rotating_filename(restorefrom, start.tv_sec); filename = rotating_filename(restorefrom, start.tv_sec);
fp = fopen(filename, "r"); fp = fopen(filename, "re");
if (fp) if (fp)
fclose(fp); fclose(fp);
else { else {
@ -6139,7 +6139,7 @@ static bool write_pid(ckpool_t *ckp, const char *path, pid_t pid)
int oldpid; int oldpid;
LOGWARNING("File %s exists", path); LOGWARNING("File %s exists", path);
fp = fopen(path, "r"); fp = fopen(path, "re");
if (!fp) { if (!fp) {
LOGEMERG("Failed to open file %s", path); LOGEMERG("Failed to open file %s", path);
return false; return false;
@ -6159,7 +6159,7 @@ static bool write_pid(ckpool_t *ckp, const char *path, pid_t pid)
LOGWARNING("Killing off old process %s pid %d", path, oldpid); LOGWARNING("Killing off old process %s pid %d", path, oldpid);
} }
} }
fp = fopen(path, "w"); fp = fopen(path, "we");
if (!fp) { if (!fp) {
LOGERR("Failed to open file %s", path); LOGERR("Failed to open file %s", path);
return false; return false;
@ -8789,7 +8789,7 @@ static bool reload_from(tv_t *start)
LOGWARNING("%s(): from %s", __func__, buf); LOGWARNING("%s(): from %s", __func__, buf);
filename = rotating_filename(restorefrom, start->tv_sec); filename = rotating_filename(restorefrom, start->tv_sec);
fp = fopen(filename, "r"); fp = fopen(filename, "re");
if (!fp) if (!fp)
quithere(1, "Failed to open '%s'", filename); quithere(1, "Failed to open '%s'", filename);
@ -8827,7 +8827,7 @@ static bool reload_from(tv_t *start)
break; break;
start->tv_sec += ROLL_S; start->tv_sec += ROLL_S;
filename = rotating_filename(restorefrom, start->tv_sec); filename = rotating_filename(restorefrom, start->tv_sec);
fp = fopen(filename, "r"); fp = fopen(filename, "re");
if (!fp) { if (!fp) {
missingfirst = strdup(filename); missingfirst = strdup(filename);
free(filename); free(filename);
@ -8848,7 +8848,7 @@ static bool reload_from(tv_t *start)
break; break;
} }
filename = rotating_filename(restorefrom, start->tv_sec); filename = rotating_filename(restorefrom, start->tv_sec);
fp = fopen(filename, "r"); fp = fopen(filename, "re");
if (fp) if (fp)
break; break;
errno = 0; errno = 0;
@ -9173,7 +9173,7 @@ static void confirm_reload()
start.tv_sec = DATE_BEGIN; start.tv_sec = DATE_BEGIN;
start.tv_usec = 0L; start.tv_usec = 0L;
filename = rotating_filename(restorefrom, start.tv_sec); filename = rotating_filename(restorefrom, start.tv_sec);
fp = fopen(filename, "r"); fp = fopen(filename, "re");
if (fp) if (fp)
fclose(fp); fclose(fp);
else { else {
@ -9370,7 +9370,7 @@ int main(int argc, char **argv)
/* Create the logfile */ /* Create the logfile */
sprintf(buf, "%s%s%s.log", ckp.logdir, ckp.name, dbcode); sprintf(buf, "%s%s%s.log", ckp.logdir, ckp.name, dbcode);
ckp.logfp = fopen(buf, "a"); ckp.logfp = fopen(buf, "ae");
if (!ckp.logfp) if (!ckp.logfp)
quit(1, "Failed to open log file %s", buf); quit(1, "Failed to open log file %s", buf);
ckp.logfd = fileno(ckp.logfp); ckp.logfd = fileno(ckp.logfp);

8
src/ckpool.c

@ -388,7 +388,7 @@ static int get_proc_pid(proc_instance_t *pi)
FILE *fp; FILE *fp;
sprintf(path, "%s%s.pid", pi->ckp->socket_dir, pi->processname); sprintf(path, "%s%s.pid", pi->ckp->socket_dir, pi->processname);
fp = fopen(path, "r"); fp = fopen(path, "re");
if (!fp) if (!fp)
goto out; goto out;
ret = fscanf(fp, "%d", &pid); ret = fscanf(fp, "%d", &pid);
@ -615,7 +615,7 @@ static bool write_pid(ckpool_t *ckp, const char *path, pid_t pid)
int oldpid; int oldpid;
LOGNOTICE("File %s exists", path); LOGNOTICE("File %s exists", path);
fp = fopen(path, "r"); fp = fopen(path, "re");
if (!fp) { if (!fp) {
LOGEMERG("Failed to open file %s", path); LOGEMERG("Failed to open file %s", path);
return false; return false;
@ -635,7 +635,7 @@ static bool write_pid(ckpool_t *ckp, const char *path, pid_t pid)
LOGWARNING("Killing off old process %s pid %d", path, oldpid); LOGWARNING("Killing off old process %s pid %d", path, oldpid);
} }
} }
fp = fopen(path, "w"); fp = fopen(path, "we");
if (!fp) { if (!fp) {
LOGERR("Failed to open file %s", path); LOGERR("Failed to open file %s", path);
return false; return false;
@ -1223,7 +1223,7 @@ int main(int argc, char **argv)
/* Create the logfile */ /* Create the logfile */
sprintf(buf, "%s%s.log", ckp.logdir, ckp.name); sprintf(buf, "%s%s.log", ckp.logdir, ckp.name);
ckp.logfp = fopen(buf, "a"); ckp.logfp = fopen(buf, "ae");
if (!ckp.logfp) if (!ckp.logfp)
quit(1, "Failed to make open log file %s", buf); quit(1, "Failed to make open log file %s", buf);
/* Make logging line buffered */ /* Make logging line buffered */

2
src/ktree.c

@ -144,7 +144,7 @@ void _dsp_ktree(K_LIST *list, K_TREE *root, char *filename, char *msg, KTREE_FFL
tm.tm_min, tm.tm_min,
tm.tm_sec); tm.tm_sec);
stream = fopen(filename, "a"); stream = fopen(filename, "ae");
if (!stream) if (!stream)
{ {
fprintf(stderr, "%s %s() failed to open '%s' (%d) %s", fprintf(stderr, "%s %s() failed to open '%s' (%d) %s",

6
src/libckpool.c

@ -999,15 +999,15 @@ bool rotating_log(const char *path, const char *msg)
bool ok = false; bool ok = false;
filename = rotating_filename(path, time(NULL)); filename = rotating_filename(path, time(NULL));
fd = open(filename, O_CREAT|O_RDWR, mode); fd = open(filename, O_CREAT | O_RDWR | O_CLOEXEC , mode);
if (unlikely(fd == -1)) { if (unlikely(fd == -1)) {
LOGERR("Failed to open %s in rotating_log!", filename); LOGERR("Failed to open %s in rotating_log!", filename);
goto stageleft; goto stageleft;
} }
fp = fdopen(fd, "a"); fp = fdopen(fd, "ae");
if (unlikely(!fp)) { if (unlikely(!fp)) {
close(fd); close(fd);
LOGERR("Failed to fopen %s in rotating_log!", filename); LOGERR("Failed to fdopen %s in rotating_log!", filename);
goto stageleft; goto stageleft;
} }
if (unlikely(flock(fd, LOCK_EX))) { if (unlikely(flock(fd, LOCK_EX))) {

6
src/stratifier.c

@ -1873,7 +1873,7 @@ out_unlock:
json_set_string(val, "username", user_instance->username); json_set_string(val, "username", user_instance->username);
if (ckp->logshares) { if (ckp->logshares) {
fp = fopen(fname, "a"); fp = fopen(fname, "ae");
if (likely(fp)) { if (likely(fp)) {
s = json_dumps(val, 0); s = json_dumps(val, 0);
len = strlen(s); len = strlen(s);
@ -2400,7 +2400,7 @@ static void *statsupdate(void *arg)
suffix_string(ghs1440, suffix1440, 16, 0); suffix_string(ghs1440, suffix1440, 16, 0);
snprintf(fname, 511, "%s/pool.status", ckp->logdir); snprintf(fname, 511, "%s/pool.status", ckp->logdir);
fp = fopen(fname, "w"); fp = fopen(fname, "we");
if (unlikely(!fp)) if (unlikely(!fp))
LOGERR("Failed to fopen %s", fname); LOGERR("Failed to fopen %s", fname);
@ -2474,7 +2474,7 @@ static void *statsupdate(void *arg)
"hashrate1d", suffix1440); "hashrate1d", suffix1440);
snprintf(fname, 511, "%s/%s", ckp->logdir, client->workername); snprintf(fname, 511, "%s/%s", ckp->logdir, client->workername);
fp = fopen(fname, "w"); fp = fopen(fname, "we");
if (unlikely(!fp)) { if (unlikely(!fp)) {
LOGERR("Failed to fopen %s", fname); LOGERR("Failed to fopen %s", fname);
continue; continue;

Loading…
Cancel
Save