[Freeswitch-trunk] [commit] r6575 - in freeswitch/trunk/src: . include
Freeswitch SVN
mikej at freeswitch.org
Fri Dec 7 22:41:01 EST 2007
Author: mikej
Date: Fri Dec 7 22:41:00 2007
New Revision: 6575
Modified:
freeswitch/trunk/src/include/switch_apr.h
freeswitch/trunk/src/switch.c
freeswitch/trunk/src/switch_apr.c
Log:
use file locks to keep multiple concurrent copies of freeswitch from running at the same time (FSCORE-14)
Modified: freeswitch/trunk/src/include/switch_apr.h
==============================================================================
--- freeswitch/trunk/src/include/switch_apr.h (original)
+++ freeswitch/trunk/src/include/switch_apr.h Fri Dec 7 22:41:00 2007
@@ -644,6 +644,29 @@
#define SWITCH_FPROT_FILE_SOURCE_PERMS 0x1000 /**< Copy source file's permissions */
/** @} */
+/* File lock types/flags */
+/**
+ * @defgroup switch_file_lock_types File Lock Types
+ * @{
+ */
+
+#define SWITCH_FLOCK_SHARED 1 /**< Shared lock. More than one process
+ or thread can hold a shared lock
+ at any given time. Essentially,
+ this is a "read lock", preventing
+ writers from establishing an
+ exclusive lock. */
+#define SWITCH_FLOCK_EXCLUSIVE 2 /**< Exclusive lock. Only one process
+ may hold an exclusive lock at any
+ given time. This is analogous to
+ a "write lock". */
+
+#define SWITCH_FLOCK_TYPEMASK 0x000F /**< mask to extract lock type */
+#define SWITCH_FLOCK_NONBLOCK 0x0010 /**< do not block while acquiring the
+ file lock */
+
+ /** @} */
+
/**
* @defgroup switch_file_open_flags File Open Flags/Routines
* @ingroup switch_file_io
@@ -711,6 +734,8 @@
*/
SWITCH_DECLARE(switch_status_t) switch_file_close(switch_file_t * thefile);
+SWITCH_DECLARE(switch_status_t) switch_file_lock(switch_file_t * thefile, int type);
+
/**
* Delete the specified file.
* @param path The full path to the file (using / on all systems)
Modified: freeswitch/trunk/src/switch.c
==============================================================================
--- freeswitch/trunk/src/switch.c (original)
+++ freeswitch/trunk/src/switch.c Fri Dec 7 22:41:00 2007
@@ -38,6 +38,7 @@
#endif
#include <switch.h>
+#include "private/switch_core_pvt.h"
/* pid filename: Stores the process id of the freeswitch process */
#define PIDFILE "freeswitch.pid"
@@ -198,6 +199,8 @@
int main(int argc, char *argv[])
{
char pid_path[256] = ""; /* full path to the pid file */
+ char pid_buffer[32] = ""; /* pid string */
+ switch_size_t pid_len;
const char *err = NULL; /* error value for return from freeswitch initialization */
#ifndef WIN32
int nf = 0; /* TRUE if we are running in nofork mode */
@@ -205,7 +208,6 @@
char *runas_group = NULL;
#endif
int nc = 0; /* TRUE if we are running in noconsole mode */
- FILE *f; /* file handle to the pid file */
pid_t pid = 0;
int x;
int die = 0;
@@ -215,6 +217,9 @@
int high_prio = 0;
switch_core_flag_t flags = SCF_USE_SQL;
int status;
+ switch_file_t *fd;
+ switch_memory_pool_t *pool = NULL;
+
#ifdef WIN32
SERVICE_TABLE_ENTRY dispatchTable[] = {
@@ -417,25 +422,46 @@
}
#endif
- if (switch_core_init_and_modload(flags, nc ? SWITCH_FALSE : SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
- fprintf(stderr, "Cannot Initilize [%s]\n", err);
+ if (apr_initialize() != SWITCH_STATUS_SUCCESS) {
+ fprintf(stderr, "FATAL ERROR! Could not initilize APR\n");
return 255;
}
+ switch_core_set_globals();
+
+ pid = getpid();
+
snprintf(pid_path, sizeof(pid_path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
- if ((f = fopen(pid_path, "w")) == 0) {
+ snprintf(pid_buffer, sizeof(pid_buffer), "%d", pid);
+ pid_len = strlen(pid_buffer);
+
+ apr_pool_create(&pool, NULL);
+ if (switch_file_open(&fd,
+ pid_path,
+ SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE,
+ SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE,
+ pool) != SWITCH_STATUS_SUCCESS) {
fprintf(stderr, "Cannot open pid file %s.\n", pid_path);
return 255;
}
- fprintf(f, "%d", pid = getpid());
- fflush(f);
+ if (switch_file_lock(fd, SWITCH_FLOCK_EXCLUSIVE | SWITCH_FLOCK_NONBLOCK) != SWITCH_STATUS_SUCCESS) {
+ fprintf(stderr, "Cannot lock pid file %s.\n", pid_path);
+ return 255;
+ }
+
+ switch_file_write(fd, pid_buffer, &pid_len);
+
+ if (switch_core_init_and_modload(flags, nc ? SWITCH_FALSE : SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
+ fprintf(stderr, "Cannot Initilize [%s]\n", err);
+ return 255;
+ }
switch_core_runtime_loop(nc);
status = switch_core_destroy();
- fclose(f);
+ switch_file_close(fd);
unlink(pid_path);
return status;
Modified: freeswitch/trunk/src/switch_apr.c
==============================================================================
--- freeswitch/trunk/src/switch_apr.c (original)
+++ freeswitch/trunk/src/switch_apr.c Fri Dec 7 22:41:00 2007
@@ -328,6 +328,11 @@
return apr_file_close(thefile);
}
+SWITCH_DECLARE(switch_status_t) switch_file_lock(switch_file_t * thefile, int type)
+{
+ return apr_file_lock(thefile, type);
+}
+
SWITCH_DECLARE(switch_status_t) switch_file_rename(const char *from_path, const char *to_path, switch_memory_pool_t *pool)
{
return apr_file_rename(from_path, to_path, pool);
More information about the Freeswitch-trunk
mailing list