GUAC-236: Acquire write lock on output file for in-progress screen recordings.
Project: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/commit/0361dd23 Tree: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/tree/0361dd23 Diff: http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/diff/0361dd23 Branch: refs/heads/master Commit: 0361dd23921f36b961a8d01b0e033463efc1cdb3 Parents: c50561e Author: Michael Jumper <[email protected]> Authored: Tue Mar 15 17:06:52 2016 -0700 Committer: Michael Jumper <[email protected]> Committed: Tue Mar 15 17:06:52 2016 -0700 ---------------------------------------------------------------------- src/common/guac_recording.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-guacamole-server/blob/0361dd23/src/common/guac_recording.c ---------------------------------------------------------------------- diff --git a/src/common/guac_recording.c b/src/common/guac_recording.c index 1b8d513..461b80e 100644 --- a/src/common/guac_recording.c +++ b/src/common/guac_recording.c @@ -25,13 +25,14 @@ #include <guacamole/client.h> #include <guacamole/socket.h> +#include <sys/stat.h> +#include <sys/types.h> #include <errno.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <string.h> -#include <sys/stat.h> -#include <sys/types.h> +#include <unistd.h> /** * Attempts to open a new recording within the given path and having the given @@ -105,6 +106,21 @@ static int guac_common_recording_open(const char* path, } /* end if open succeeded */ + /* Lock entire output file for writing by the current process */ + struct flock file_lock = { + .l_type = F_WRLCK, + .l_whence = SEEK_SET, + .l_start = 0, + .l_len = 0, + .l_pid = getpid() + }; + + /* Abort if file cannot be locked for reading */ + if (fcntl(fd, F_SETLK, &file_lock) == -1) { + close(fd); + return -1; + } + return fd; }
