Skip to content

Commit

Permalink
Touch the pid file when frames are processed from RTSP, this allows a…
Browse files Browse the repository at this point in the history
…n external process to detect that motion has hung so it can be shot in the face and restarted
  • Loading branch information
dren-dk committed Jan 13, 2015
1 parent a7f3c54 commit ea820e1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
25 changes: 25 additions & 0 deletions motion.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "event.h"
#include "picture.h"
#include "rotate.h"
#include <utime.h>

/* Forward declarations */
static int motion_init(struct context *cnt);
Expand Down Expand Up @@ -396,6 +397,30 @@ static void motion_remove_pid(void)

}

/**
* motion_touch_pid
*
* This function touches the pid file, to let an external process detect that motion hasn't hung itself.
*/
time_t last_pid_touch = 0;
void motion_touch_pid(void)
{

time_t now = time(NULL);
if (now-last_pid_touch < 2) { // Limit the frequency of file touching, because it could end up being expensive.
return;
}
last_pid_touch = now;

//MOTION_LOG(WRN, TYPE_ALL, NO_ERRNO, "%s: Touching (%s).", cnt_list[0]->conf.pid_file);

if ((cnt_list[0]->daemon) && (cnt_list[0]->conf.pid_file)) {
struct utimbuf new_times;
new_times.actime = new_times.modtime = time(NULL);
utime(cnt_list[0]->conf.pid_file, &new_times);
}
}

/**
* motion_detected
*
Expand Down
3 changes: 3 additions & 0 deletions motion.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,7 @@ FILE * myfopen(const char *, const char *, size_t);
int myfclose(FILE *);
size_t mystrftime(const struct context *, char *, size_t, const char *, const struct tm *, const char *, int, unsigned long long);
int create_path(const char *);

void motion_touch_pid();

#endif /* _INCLUDE_MOTION_H */
5 changes: 3 additions & 2 deletions netcam_rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ static int netcam_read_rtsp_image(netcam_context_ptr netcam)
if (size_decoded != usual_size_decoded) {
MOTION_LOG(WRN, TYPE_NETCAM, SHOW_ERRNO, "%s: unusual frame size of %d!", size_decoded);
usual_size_decoded = size_decoded;
} else {
MOTION_LOG(DBG, TYPE_NETCAM, SHOW_ERRNO, "%s: usual frame size of %d!", size_decoded);
}

// at this point, we are finished with the packet and frame, so free them.
Expand Down Expand Up @@ -357,6 +355,9 @@ static int netcam_read_rtsp_image(netcam_context_ptr netcam)

pthread_mutex_unlock(&netcam->mutex);

// We got a frame, so let the watchdog know that we're still alive
motion_touch_pid();

return 0;
}

Expand Down

1 comment on commit ea820e1

@tosiara
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked https://github.com/Mr-Dave/motion ? Some RTSP work has been made there, and I rememeber auth also added, wasn't it? (so we all are on the same track)

Please sign in to comment.