From 4e6b0b3afae51436af3d04d1aeda503c9ffa4563 Mon Sep 17 00:00:00 2001 From: MrDave Date: Sat, 13 Mar 2021 16:58:16 -0700 Subject: [PATCH] Move exec command to be a util function --- src/event.cpp | 59 ++++++++----------------------------------- src/track.cpp | 69 +++------------------------------------------------ src/util.cpp | 39 +++++++++++++++++++++++++++++ src/util.hpp | 2 ++ 4 files changed, 55 insertions(+), 114 deletions(-) diff --git a/src/event.cpp b/src/event.cpp index 68ea5f0d..16cf639b 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -68,45 +68,6 @@ const char *eventList[] = { "EVENT_LAST" }; -/** - * exec_command - * Execute 'command' with 'arg' as its argument. - * if !arg command is started with no arguments - * Before we call execl we need to close all the file handles - * that the fork inherited from the parent in order not to pass - * the open handles on to the shell - */ -static void exec_command(struct ctx_cam *cam, const char *command, char *filename, int filetype) -{ - char stamp[PATH_MAX]; - 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 */ - MOTION_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO - ,_("Unable to start external command '%s'"), stamp); - - exit(1); - } - - MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO - ,_("Executing external command '%s'"), stamp); -} - static void event_newfile(struct ctx_cam *cam, motion_event evnt ,struct ctx_image_data *img_data, char *fname, void *ftype, struct timespec *ts1) { @@ -155,10 +116,10 @@ static void on_picture_save_command(struct ctx_cam *cam, motion_event evnt (void)ts1; if ((filetype & FTYPE_IMAGE_ANY) != 0 && (cam->conf->on_picture_save != "")) - exec_command(cam, cam->conf->on_picture_save.c_str(), fname, filetype); + util_exec_command(cam, cam->conf->on_picture_save.c_str(), fname, filetype); if ((filetype & FTYPE_MPEG_ANY) != 0 && (cam->conf->on_movie_start != "")) - exec_command(cam, cam->conf->on_movie_start.c_str(), fname, filetype); + util_exec_command(cam, cam->conf->on_movie_start.c_str(), fname, filetype); } static void on_motion_detected_command(struct ctx_cam *cam, motion_event evnt @@ -172,7 +133,7 @@ static void on_motion_detected_command(struct ctx_cam *cam, motion_event evnt (void)ts1; if (cam->conf->on_motion_detected != "") - exec_command(cam, cam->conf->on_motion_detected.c_str(), NULL, 0); + util_exec_command(cam, cam->conf->on_motion_detected.c_str(), NULL, 0); } static void event_sqlfirstmotion(struct ctx_cam *cam, motion_event evnt @@ -240,7 +201,7 @@ static void on_area_command(struct ctx_cam *cam, motion_event evnt (void)ts1; if (cam->conf->on_area_detected != "") - exec_command(cam, cam->conf->on_area_detected.c_str(), NULL, 0); + util_exec_command(cam, cam->conf->on_area_detected.c_str(), NULL, 0); } static void on_event_start_command(struct ctx_cam *cam, motion_event evnt @@ -254,7 +215,7 @@ static void on_event_start_command(struct ctx_cam *cam, motion_event evnt (void)ts1; if (cam->conf->on_event_start != "") - exec_command(cam, cam->conf->on_event_start.c_str(), NULL, 0); + util_exec_command(cam, cam->conf->on_event_start.c_str(), NULL, 0); } static void on_event_end_command(struct ctx_cam *cam, motion_event evnt @@ -268,7 +229,7 @@ static void on_event_end_command(struct ctx_cam *cam, motion_event evnt (void)ts1; if (cam->conf->on_event_end != "") - exec_command(cam, cam->conf->on_event_end.c_str(), NULL, 0); + util_exec_command(cam, cam->conf->on_event_end.c_str(), NULL, 0); } static void event_stream_put(struct ctx_cam *cam, motion_event evnt @@ -479,7 +440,7 @@ static void event_camera_lost(struct ctx_cam *cam, motion_event evnt (void)ts1; if (cam->conf->on_camera_lost != "") - exec_command(cam, cam->conf->on_camera_lost.c_str(), NULL, 0); + util_exec_command(cam, cam->conf->on_camera_lost.c_str(), NULL, 0); } static void event_secondary_detect(struct ctx_cam *cam, motion_event evnt @@ -495,7 +456,7 @@ static void event_secondary_detect(struct ctx_cam *cam, motion_event evnt MOTION_LOG(NTC, TYPE_EVENTS, NO_ERRNO,_("Event secondary detect")); if (cam->conf->on_secondary_detect != "") - exec_command(cam, cam->conf->on_secondary_detect.c_str(), NULL, 0); + util_exec_command(cam, cam->conf->on_secondary_detect.c_str(), NULL, 0); } static void event_camera_found(struct ctx_cam *cam, motion_event evnt @@ -509,7 +470,7 @@ static void event_camera_found(struct ctx_cam *cam, motion_event evnt (void)ts1; if (cam->conf->on_camera_found != "") - exec_command(cam, cam->conf->on_camera_found.c_str(), NULL, 0); + util_exec_command(cam, cam->conf->on_camera_found.c_str(), NULL, 0); } static void on_movie_end_command(struct ctx_cam *cam, motion_event evnt @@ -523,7 +484,7 @@ static void on_movie_end_command(struct ctx_cam *cam, motion_event evnt (void)ts1; if ((filetype & FTYPE_MPEG_ANY) && (cam->conf->on_movie_end != "")) - exec_command(cam, cam->conf->on_movie_end.c_str(), fname, filetype); + util_exec_command(cam, cam->conf->on_movie_end.c_str(), fname, filetype); } static void event_extpipe_end(struct ctx_cam *cam, motion_event evnt diff --git a/src/track.cpp b/src/track.cpp index f4f17f12..41e6c4eb 100644 --- a/src/track.cpp +++ b/src/track.cpp @@ -409,74 +409,13 @@ static int uvc_move(struct ctx_cam *cam, int dev, struct ctx_coord *cent } /**Move the generic camera*/ -static int generic_move(struct ctx_cam *cam, enum track_action action, int manual - , int xoff, int yoff, struct ctx_coord *cent, struct ctx_images *imgs) +static int generic_move(struct ctx_cam *cam, struct ctx_coord *cent) { - char fmtcmd[PATH_MAX]; cam->track->posx += cent->x; cam->track->posy += cent->y; - mystrftime(cam, fmtcmd, sizeof(fmtcmd), cam->conf->track_generic_move.c_str() - , &cam->current_image->imgts, NULL, 0); - - if (!fork()) { - int i; - char buf[12]; - - /* Detach from parent */ - setsid(); - - /* Provides data as environment variables */ - if (manual) { - setenv("TRACK_MANUAL", "manual", 1); - } - switch (action) { - case TRACK_CENTER: - setenv("TRACK_ACTION", "center", 1); - sprintf(buf, "%d", xoff); setenv("TRACK_XOFF", buf, 1); - sprintf(buf, "%d", yoff); setenv("TRACK_YOFF", buf, 1); - break; - case TRACK_MOVE: - setenv("TRACK_ACTION", "move", 1); - if (cent) { - sprintf(buf, "%d", cent->x); setenv("TRACK_CENT_X", buf, 1); - sprintf(buf, "%d", cent->y); setenv("TRACK_CENT_Y", buf, 1); - sprintf(buf, "%d", cent->width); setenv("TRACK_CENT_WIDTH", buf, 1); - sprintf(buf, "%d", cent->height); setenv("TRACK_CENT_HEIGHT", buf, 1); - sprintf(buf, "%d", cent->minx); setenv("TRACK_CENT_MINX", buf, 1); - sprintf(buf, "%d", cent->maxx); setenv("TRACK_CENT_MAXX", buf, 1); - sprintf(buf, "%d", cent->miny); setenv("TRACK_CENT_MINY", buf, 1); - sprintf(buf, "%d", cent->maxy); setenv("TRACK_CENT_MAXY", buf, 1); - } - if (imgs) { - sprintf(buf, "%d", imgs->width); setenv("TRACK_IMGS_WIDTH", buf, 1); - sprintf(buf, "%d", imgs->height); setenv("TRACK_IMGS_HEIGHT", buf, 1); - sprintf(buf, "%d", imgs->motionsize); setenv("TRACK_IMGS_MOTIONSIZE", buf, 1); - } - } - - /* - * 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", fmtcmd, " &", (char *)NULL); - - /* if above function succeeds the program never reach here */ - MOTION_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO - ,_("Unable to start external command '%s'") - ,cam->conf->track_generic_move.c_str()); - - exit(1); - } - - MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO - ,_("Executing external command '%s'") - , fmtcmd); + util_exec_command(cam, cam->conf->track_generic_move.c_str(), NULL, 0); return cam->conf->track_move_wait; } @@ -520,7 +459,7 @@ int track_center(struct ctx_cam *cam, int dev, int manual, int xoff, int yoff) if (cam->conf->track_generic_move != "") { cent.x = -cam->track->posx; cent.y = -cam->track->posy; - return generic_move(cam, TRACK_CENTER, manual,0 ,0 ,¢ , NULL); + return generic_move(cam, ¢); } else { return 10; } @@ -544,7 +483,7 @@ int track_move(struct ctx_cam *cam, int dev, struct ctx_coord *cent return uvc_move(cam, dev, cent, imgs, manual); } else if (cam->conf->track_type == TRACK_TYPE_GENERIC) { if (cam->conf->track_generic_move != "") { - return generic_move(cam, TRACK_MOVE, manual, 0, 0, cent, imgs); + return generic_move(cam, cent); } else { return cam->conf->track_move_wait; } diff --git a/src/util.cpp b/src/util.cpp index 83a9601d..4bd26edd 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1174,3 +1174,42 @@ void util_parms_update(struct ctx_params *params, std::string &confline) return; } + +/** + * util_exec_command + * Execute 'command' with 'arg' as its argument. + * if !arg command is started with no arguments + * Before we call execl we need to close all the file handles + * that the fork inherited from the parent in order not to pass + * the open handles on to the shell + */ +void util_exec_command(struct ctx_cam *cam, const char *command, char *filename, int filetype) +{ + char stamp[PATH_MAX]; + 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 */ + MOTION_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO + ,_("Unable to start external command '%s'"), stamp); + + exit(1); + } + + MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO + ,_("Executing external command '%s'"), stamp); +} diff --git a/src/util.hpp b/src/util.hpp index a06e71b9..1b0b58ea 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -99,6 +99,7 @@ int myfclose(FILE *); size_t mystrftime(const struct ctx_cam *, char *, size_t, const char *, const struct timespec *, const char *, int); int mycreate_path(const char *); + void util_exec_command(struct ctx_cam *cam, const char *command, char *filename, int filetype); void mythreadname_set(const char *abbr, int threadnbr, const char *threadname); void mythreadname_get(char *threadname); @@ -132,4 +133,5 @@ void util_parms_free(struct ctx_params *params); void util_parms_update(struct ctx_params *params, std::string &confline); + #endif