From 7a809dd8b82fb2443dcf09c9d8e88beacc0aed00 Mon Sep 17 00:00:00 2001 From: Mr-Dave Date: Sat, 16 Apr 2022 13:51:41 -0600 Subject: [PATCH] Revise close on exec --- src/conf.cpp | 16 ++++++++-------- src/logger.cpp | 2 +- src/motionplus.cpp | 8 ++++---- src/movie.cpp | 8 ++++---- src/picture.cpp | 16 ++++++++-------- src/util.cpp | 45 +++++++++++++-------------------------------- src/webu.cpp | 4 ++-- src/webu_file.cpp | 6 +++--- src/webu_html.cpp | 2 +- 9 files changed, 44 insertions(+), 63 deletions(-) diff --git a/src/conf.cpp b/src/conf.cpp index 8e363f7f..492313ab 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -3565,7 +3565,7 @@ static void conf_camera_filenm(struct ctx_motapp *motapp) indx_cam++; } if (motapp->cam_list[indx_cam] == NULL) { - fp = fopen(fullnm.c_str(), "r"); + fp = myfopen(fullnm.c_str(), "re"); if (fp) { myfclose(fp); } else { @@ -3635,7 +3635,7 @@ static void conf_parm_camera(struct ctx_motapp *motapp, std::string filename) int indx_cam, retcd; FILE *fp; - fp = fopen(filename.c_str(), "r"); + fp = myfopen(filename.c_str(), "re"); if (!fp) { MOTION_LOG(ALR, TYPE_ALL, SHOW_ERRNO ,_("Camera config file %s not found"), filename.c_str()); @@ -3855,7 +3855,7 @@ void conf_parms_write(struct ctx_motapp *motapp) indx_cam = 0; while (motapp->cam_list[indx_cam] != NULL) { - conffile = myfopen(motapp->cam_list[indx_cam]->conf_filename, "w"); + conffile = myfopen(motapp->cam_list[indx_cam]->conf_filename, "we"); if (!conffile) { MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO @@ -3973,7 +3973,7 @@ void conf_init_app(struct ctx_motapp *motapp, int argc, char *argv[]) if (motapp->conf_filename != "") { filename = motapp->conf_filename; - fp = fopen (filename.c_str(), "r"); + fp = myfopen(filename.c_str(), "re"); } if (!fp) { @@ -3982,17 +3982,17 @@ void conf_init_app(struct ctx_motapp *motapp, int argc, char *argv[]) exit(-1); } filename = path + std::string("/motionplus.conf"); - fp = fopen (filename.c_str(), "r"); + fp = myfopen(filename.c_str(), "re"); } if (!fp) { filename = std::string(getenv("HOME")) + std::string("/.motionplus/motionplus.conf"); - fp = fopen(filename.c_str(), "r"); + fp = myfopen(filename.c_str(), "re"); } if (!fp) { filename = std::string( sysconfdir ) + std::string("/motionplus.conf"); - fp = fopen(filename.c_str(), "r"); + fp = myfopen(filename.c_str(), "re"); } if (!fp) { @@ -4042,7 +4042,7 @@ void conf_init_cams(struct ctx_motapp *motapp) MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO ,_("Error using config file %s"), motapp->conf_filename.c_str()); } else { - fp = fopen (motapp->conf_filename.c_str(), "r"); + fp = myfopen(motapp->conf_filename.c_str(), "re"); } } diff --git a/src/logger.cpp b/src/logger.cpp index f2669062..d61449ef 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -96,7 +96,7 @@ static void log_set_logfile(const char *logfile_name) /* Setup temporary to let log if myfopen fails */ log_set_mode(LOGMODE_SYSLOG); - logfile = myfopen(logfile_name, "a"); + logfile = myfopen(logfile_name, "ae"); /* If logfile was opened correctly */ if (logfile) { diff --git a/src/motionplus.cpp b/src/motionplus.cpp index 11c62cf3..852373cd 100644 --- a/src/motionplus.cpp +++ b/src/motionplus.cpp @@ -206,7 +206,7 @@ static void motion_daemon(struct ctx_motapp *motapp) * for an enter. */ if (motapp->pid_file != "") { - pidf = myfopen(motapp->pid_file.c_str(), "w+"); + pidf = myfopen(motapp->pid_file.c_str(), "w+e"); if (pidf) { (void)fprintf(pidf, "%d\n", getpid()); @@ -234,20 +234,20 @@ static void motion_daemon(struct ctx_motapp *motapp) setpgrp(); #endif - if ((fd = open("/dev/tty", O_RDWR)) >= 0) { + if ((fd = open("/dev/tty", O_RDWR|O_CLOEXEC)) >= 0) { ioctl(fd, TIOCNOTTY, NULL); close(fd); } setsid(); - fd = open("/dev/null", O_RDONLY); + fd = open("/dev/null", O_RDONLY|O_CLOEXEC); if (fd != -1) { dup2(fd, STDIN_FILENO); close(fd); } - fd = open("/dev/null", O_WRONLY); + fd = open("/dev/null", O_WRONLY|O_CLOEXEC); if (fd != -1) { dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); diff --git a/src/movie.cpp b/src/movie.cpp index 159f0d39..2e0f7bca 100644 --- a/src/movie.cpp +++ b/src/movie.cpp @@ -82,9 +82,9 @@ static void movie_encode_nal(struct ctx_movie *movie) static int movie_timelapse_exists(const char *fname) { FILE *file; - file = fopen(fname, "r"); + file = myfopen(fname, "rbe"); if (file) { - fclose(file); + myfclose(file); return 1; } return 0; @@ -94,14 +94,14 @@ static int movie_timelapse_append(struct ctx_movie *movie, AVPacket *pkt) { FILE *file; - file = fopen(movie->filename, "a"); + file = myfopen(movie->filename, "abe"); if (!file) { return -1; } fwrite(pkt->data, 1, pkt->size, file); - fclose(file); + myfclose(file); return 0; } diff --git a/src/picture.cpp b/src/picture.cpp index 69f03efe..b4c49ea4 100644 --- a/src/picture.cpp +++ b/src/picture.cpp @@ -159,7 +159,7 @@ static void pic_save_yuv420p(FILE *fp, unsigned char *image, int width, int heig { int sz, image_size; - + image_size = (width * height * 3)/2; unsigned char *buf =(unsigned char*) mymalloc(image_size); @@ -176,9 +176,9 @@ static void pic_save_grey(FILE *picture, unsigned char *image, int width, int he { int sz, image_size; - + image_size = (width * height * 3)/2; - + unsigned char *buf =(unsigned char*) mymalloc(image_size); sz = jpgutl_put_grey(buf, image_size, image, width, height, quality, cam ,ts1, box); @@ -306,7 +306,7 @@ void pic_save_norm(struct ctx_cam *cam, char *file, unsigned char *image, int ft { FILE *picture; - picture = myfopen(file, "w"); + picture = myfopen(file, "wbe"); if (!picture) { /* Report to syslog - suggest solution if the problem is access rights to target dir. */ if (errno == EACCES) { @@ -343,7 +343,7 @@ void pic_save_roi(struct ctx_cam *cam, char *file, unsigned char *image) return; } - picture = myfopen(file, "w"); + picture = myfopen(file, "wbe"); if (!picture) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO ,_("Can't write picture to file %s"), file); @@ -468,7 +468,7 @@ static void pic_write_mask(struct ctx_cam *cam, const char *file) { FILE *picture; - picture = myfopen(file, "w"); + picture = myfopen(file, "wbe"); if (!picture) { /* Report to syslog - suggest solution if the problem is access rights to target dir. */ if (errno == EACCES) { @@ -574,7 +574,7 @@ void pic_init_privacy(struct ctx_cam *cam) cam->imgs.mask_privacy_high_uv = NULL; if (cam->conf->mask_privacy != "") { - if ((picture = myfopen(cam->conf->mask_privacy.c_str(), "r"))) { + if ((picture = myfopen(cam->conf->mask_privacy.c_str(), "rbe"))) { MOTION_LOG(INF, TYPE_ALL, NO_ERRNO, _("Opening privacy mask file")); /* * NOTE: The mask is expected to have the output dimensions. I.e., the mask @@ -671,7 +671,7 @@ void pic_init_mask(struct ctx_cam *cam) /* Load the mask file if any */ if (cam->conf->mask_file != "") { - if ((picture = myfopen(cam->conf->mask_file.c_str(), "r"))) { + if ((picture = myfopen(cam->conf->mask_file.c_str(), "rbe"))) { /* * NOTE: The mask is expected to have the output dimensions. I.e., the mask * applies to the already rotated image, not the capture image. Thus, use diff --git a/src/util.cpp b/src/util.cpp index 889f88c5..6e69c3e6 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -190,40 +190,30 @@ int mycreate_path(const char *path) return 0; } -/** myfopen */ -FILE * myfopen(const char *path, const char *mode) +/* myfopen */ +FILE *myfopen(const char *path, const char *mode) { - /* first, just try to open the file */ - FILE *dummy = fopen(path, mode); - if (dummy) { - return dummy; + FILE *fp; + + fp = fopen(path, mode); + if (fp) { + return fp; } - /* could not open file... */ - /* path did not exist? */ + /* If path did not exist, create and try again*/ if (errno == ENOENT) { - - /* create path for file... */ if (mycreate_path(path) == -1) { return NULL; } - - /* and retry opening the file */ - dummy = fopen(path, mode); + fp = fopen(path, mode); } - if (!dummy) { - /* - * Two possibilities - * 1: there was an other error while trying to open the file for the - * first time - * 2: could still not open the file after the path was created - */ + if (!fp) { MOTION_LOG(ERR, TYPE_ALL, SHOW_ERRNO ,_("Error opening file %s with mode %s"), path, mode); return NULL; } - return dummy; + return fp; } /** myfclose */ @@ -848,9 +838,9 @@ AVPacket *mypacket_alloc(AVPacket *pkt) pkt->data = NULL; pkt->size = 0; #endif - + return pkt; - + } /*********************************************/ @@ -1241,18 +1231,9 @@ void util_exec_command(struct ctx_cam *cam, const char *command, char *filename, mystrftime(cam, stamp, sizeof(stamp), command, &cam->current_image->imgts, filename, filetype); if (!fork()) { - int i; - /* Detach from parent */ setsid(); - /* - * Close any file descriptor except console because we will - * like to see error messages - */ - for (i = getdtablesize() - 1; i > 2; i--) - close(i); - execl("/bin/sh", "sh", "-c", stamp, " &",(char*)NULL); /* if above function succeeds the program never reach here */ diff --git a/src/webu.cpp b/src/webu.cpp index cf2a4100..9ccae9b9 100644 --- a/src/webu.cpp +++ b/src/webu.cpp @@ -1191,7 +1191,7 @@ static std::string webu_mhd_loadfile(std::string fname) filestr = ""; if (fname != "") { - infile = fopen(fname.c_str() , "rb"); + infile = myfopen(fname.c_str() , "rbe"); if (infile != NULL) { fseek(infile, 0, SEEK_END); file_size = ftell(infile); @@ -1208,7 +1208,7 @@ static std::string webu_mhd_loadfile(std::string fname) } free(file_char); } - fclose(infile); + myfclose(infile); } } return filestr; diff --git a/src/webu_file.cpp b/src/webu_file.cpp index 2b0d35b7..3f3accb0 100644 --- a/src/webu_file.cpp +++ b/src/webu_file.cpp @@ -40,7 +40,7 @@ static ssize_t webu_file_reader (void *cls, uint64_t pos, char *buf, size_t max) static void webu_file_free (void *cls) { struct ctx_webui *webui =(struct ctx_webui *)cls; - fclose (webui->req_file); + myfclose(webui->req_file); } /* Entry point for answering file request*/ @@ -85,7 +85,7 @@ mhdrslt webu_file_main(struct ctx_webui *webui) } if (stat(full_nm.c_str(), &statbuf) == 0) { - webui->req_file = fopen (full_nm.c_str(), "rbe"); + webui->req_file = myfopen(full_nm.c_str(), "rbe"); } else { webui->req_file = NULL; MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO @@ -109,7 +109,7 @@ mhdrslt webu_file_main(struct ctx_webui *webui) , webui , &webu_file_free); if (response == NULL) { - fclose (webui->req_file); + myfclose(webui->req_file); return MHD_NO; } retcd = MHD_queue_response (webui->connection, MHD_HTTP_OK, response); diff --git a/src/webu_html.cpp b/src/webu_html.cpp index 4a00fd0e..8ada12bb 100644 --- a/src/webu_html.cpp +++ b/src/webu_html.cpp @@ -1413,7 +1413,7 @@ void webu_html_user(struct ctx_webui *webui) char response[PATH_MAX]; FILE *fp = NULL; - fp = fopen(webui->motapp->cam_list[0]->conf->webcontrol_html.c_str(), "r"); + fp = myfopen(webui->motapp->cam_list[0]->conf->webcontrol_html.c_str(), "re"); if (fp == NULL) { MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO