Skip to content

Commit

Permalink
Make the C code multi-instance friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Sep 4, 2008
1 parent 7ea712d commit 17bddf4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
13 changes: 5 additions & 8 deletions nph-offload.c
Expand Up @@ -90,7 +90,7 @@
#include <sys/mman.h>
#include <netdb.h>

#define GVERSION "1.1.0"
#define GVERSION "1.1.1"
#define GSERVERSTRING "nph-offload.c/" GVERSION

#include "offload_server_config.h"
Expand Down Expand Up @@ -187,18 +187,16 @@ static void debugEcho(const char *fmt, ...)

static void *createSemaphore(const int initialVal)
{
char semname[64];
void *retval = NULL;
const int value = initialVal ? 0 : 1;
int created = 1;

snprintf(semname, sizeof (semname), "MOD-OFFLOAD-%d", (int) getuid());
retval = sem_open(semname, O_CREAT | O_EXCL, 0600, value);
retval = sem_open("SEM-" SHM_NAME, O_CREAT | O_EXCL, 0600, value);
if ((retval == (void *) SEM_FAILED) && (errno == EEXIST))
{
created = 0;
debugEcho("(semaphore already exists, just opening existing one.)");
retval = sem_open(semname, 0);
retval = sem_open("SEM-" SHM_NAME, 0);
} // if

if (retval == (void *) SEM_FAILED)
Expand Down Expand Up @@ -300,7 +298,6 @@ static void setDownloadRecord()
Sha1 sha1data;
uint8 sha1[20];
DownloadRecord *downloads = NULL;
const char *fname = "/mod-offload";
const size_t maplen = sizeof (DownloadRecord) * MAX_DOWNLOAD_RECORDS;
char *remoteAddr = getenv("REMOTE_ADDR");
if (remoteAddr == NULL)
Expand All @@ -310,10 +307,10 @@ static void setDownloadRecord()

getSemaphore();

fd = shm_open(fname, (O_CREAT|O_EXCL|O_RDWR), (S_IREAD|S_IWRITE));
fd = shm_open("/" SHM_NAME, (O_CREAT|O_EXCL|O_RDWR), (S_IREAD|S_IWRITE));
if (fd < 0)
{
fd = shm_open(fname, (O_CREAT|O_RDWR),(S_IREAD|S_IWRITE));
fd = shm_open("/" SHM_NAME, (O_CREAT|O_RDWR),(S_IREAD|S_IWRITE));
if (fd < 0)
{
putSemaphore();
Expand Down
9 changes: 9 additions & 0 deletions offload_server_config.h
Expand Up @@ -51,5 +51,14 @@
#endif
#endif

// Pick a unique name, letters and dashes.
// This should be unique for each cache, or you may have odd problems. So
// if you have two separate offload servers with their own unique caches on
// the same machine, you want to keep them separated.
// If you just want one offload server, the default is fine.
#ifndef SHM_NAME
#define SHM_NAME "mod-offload"
#endif

// end of offload_server_config.h ...

0 comments on commit 17bddf4

Please sign in to comment.