Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
create <CFG>/.lock file to protect sharing config directory
  • Loading branch information
perexg committed Dec 4, 2014
1 parent 89baf88 commit 6391e7b
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Expand Up @@ -153,7 +153,8 @@ SRCS = src/version.c \
src/esfilter.c \
src/intlconv.c \
src/profile.c \
src/bouquet.c
src/bouquet.c \
src/lock.c

SRCS-${CONFIG_UPNP} += \
src/upnp.c
Expand Down
10 changes: 10 additions & 0 deletions src/config.c
Expand Up @@ -25,12 +25,15 @@
#include "uuid.h"
#include "htsbuf.h"
#include "spawn.h"
#include "lock.h"

/* *************************************************************************
* Global data
* ************************************************************************/

static htsmsg_t *config;
static char config_lock[PATH_MAX];
static int config_lock_fd;

/* *************************************************************************
* Config migration
Expand Down Expand Up @@ -1329,6 +1332,11 @@ config_init ( const char *path, int backup )
/* Configure settings routines */
hts_settings_init(path);

/* Lock it */
hts_settings_buildpath(config_lock, sizeof(config_lock), ".lock");
if ((config_lock_fd = file_lock(config_lock, 3)) < 0)
exit(78); /* config error */

/* Load global settings */
config = hts_settings_load("config");
if (!config) {
Expand All @@ -1351,7 +1359,9 @@ config_init ( const char *path, int backup )

void config_done ( void )
{
/* note: tvhlog is inactive !!! */
htsmsg_destroy(config);
file_unlock(config_lock, config_lock_fd);
}

void config_save ( void )
Expand Down
128 changes: 128 additions & 0 deletions src/lock.c
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2014 Jaroslav Kysela <perex@perex.cz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/file.h>

#include "tvheadend.h"
#include "lock.h"

static
int state_lock_(const char *lfile, int lock, int timeout, int _fd)
{
int fd = -1, err = 0;
struct flock lck;
struct stat st;
char lcktxt[12];

lck.l_type = lock ? F_WRLCK : F_UNLCK;
lck.l_whence = SEEK_SET;
lck.l_start = 0;
lck.l_len = 11;
lck.l_pid = 0;
if (lock) {
snprintf(lcktxt, sizeof(lcktxt), "%10li\n", (long)getpid());
} else {
snprintf(lcktxt, sizeof(lcktxt), "%10s\n", "");
fd = _fd;
}
while (fd < 0 && timeout-- > 0) {
fd = open(lfile, O_RDWR);
if (!lock && fd < 0) {
err = -EIO;
goto out;
}
if (fd < 0) {
fd = open(lfile, O_RDWR|O_CREAT|O_EXCL, 0644);
if (fd < 0) {
if (errno == EBUSY || errno == EAGAIN) {
sleep(1);
timeout--;
} else {
err = -errno;
goto out;
}
}
}
}
if (fd < 0 && timeout <= 0) {
err = -EBUSY;
goto out;
}
if (fstat(fd, &st) < 0) {
err = -errno;
goto out;
}
if (st.st_size != 11 || !lock) {
if (write(fd, lcktxt, 11) != 11) {
err = -EIO;
goto out;
}
if (lock && lseek(fd, 0, SEEK_SET)) {
err = -errno;
goto out;
}
}
while (timeout > 0) {
if (fcntl(fd, F_SETLK, &lck) < 0) {
sleep(1);
timeout--;
} else {
break;
}
}
if (timeout <= 0) {
err = -EBUSY;
goto out;
}
if (lock) {
if (write(fd, lcktxt, 11) != 11) {
err = -EIO;
goto out;
}
return fd;
}
err = 0;

out:
if (fd >= 0)
close(fd);
return err;
}

int file_lock(const char *lfile, int timeout)
{
int err;

err = state_lock_(lfile, 1, timeout, -1);
if (err < 0)
tvherror("lock", "file %s lock error: %s", lfile, strerror(-err));
return err;
}

int file_unlock(const char *lfile, int _fd)
{
int err;

err = state_lock_(lfile, 0, 10, _fd);
if (err < 0)
tvherror("lock", "file %s unlock error: %s", lfile, strerror(-err));
return err;
}
25 changes: 25 additions & 0 deletions src/lock.h
@@ -0,0 +1,25 @@
/*
* File locking
* Copyright (C) 2014 Jaroslav Kysela
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TVH_LOCK_H
#define TVH_LOCK_H

int file_lock(const char *lfile, int timeout);
int file_unlock(const char *lfile, int _fd);

#endif /* TVH_LOCK_H */
3 changes: 2 additions & 1 deletion src/main.c
Expand Up @@ -939,7 +939,6 @@ main(int argc, char **argv)
tvhftrace("main", imagecache_done);
tvhftrace("main", lang_code_done);
tvhftrace("main", api_done);
tvhftrace("main", config_done);
tvhftrace("main", hts_settings_done);
tvhftrace("main", dvb_done);
tvhftrace("main", lang_str_done);
Expand All @@ -953,6 +952,8 @@ main(int argc, char **argv)
tvhlog(LOG_NOTICE, "STOP", "Exiting HTS Tvheadend");
tvhlog_end();

tvhftrace("main", config_done);

if(opt_fork)
unlink(opt_pidpath);

Expand Down

0 comments on commit 6391e7b

Please sign in to comment.