Skip to content

Commit

Permalink
Add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ddcc committed Aug 30, 2016
1 parent 520e9ee commit 027b1c0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ $(TARGET): $(OBJECTS)
$(CC) -c $(CFLAGS) $< -o $@

clean:
rm -f *.o libnvram.so
rm -f *.o libnvram.so test

.PHONY: clean
2 changes: 2 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#define LIST_MAGIC 0xdeadbeef
// Identifier value used to generate IPC key in ftok()
#define IPC_KEY 'A'
// Timeout for the semaphore
#define IPC_TIMEOUT 1000
// Mount point of the base NVRAM implementation.
#define MOUNT_POINT "/firmadyne/libnvram/"
// Location of NVRAM override values that are copied into the base NVRAM implementation.
Expand Down
12 changes: 7 additions & 5 deletions nvram.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static char temp[BUFFER_SIZE];

static int sem_get() {
int key, semid = 0;
unsigned int timeout = 0;
struct semid_ds seminfo;
union semun {
int val;
Expand Down Expand Up @@ -74,8 +75,7 @@ static int sem_get() {
semctl(semid, 0, IPC_RMID);
semid = -1;
}
}
else if (errno == EEXIST) {
} else if (errno == EEXIST) {
// Get the semaphore in non-exclusive mode
if ((semid = semget(key, 1, 0)) < 0) {
PRINT_MSG("%s\n", "Unable to get semaphore non-exclusively!");
Expand All @@ -84,18 +84,20 @@ static int sem_get() {

semun.buf = &seminfo;
// Wait for the semaphore to be initialized
while (1) {
while (timeout++ < IPC_TIMEOUT) {
semctl(semid, 0, IPC_STAT, semun);

if (semun.buf && semun.buf->sem_otime != 0) {
break;
}

PRINT_MSG("Waiting for semaphore initialization (Key: %x, Semaphore: %x)...\n", key, semid);
if (!(timeout % 100)) {
PRINT_MSG("Waiting for semaphore initialization (Key: %x, Semaphore: %x)...\n", key, semid);
}
}
}

return semid;
return (timeout < IPC_TIMEOUT) ? semid : -1;
}

static void sem_lock() {
Expand Down
56 changes: 56 additions & 0 deletions test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>

#include "nvram.h"

// Prototypes from "alias.c"
char *nvram_nget(const char *fmt, ...);
int nvram_nset(const char *val, const char *fmt, ...);
int foreach_nvram_from(const char *file, void (*fp)(const char *, const char *, void *), void *data);


void test(const char *key, const char*val, void* data) {
printf("%s = %s, %s\n", key, val, (char *) data);
}

int main(int argc, char** argv) {
char buf[256], *ptr;
int tmp;

nvram_init();

nvram_clear();

nvram_set_default();

nvram_getall(buf, 256);

nvram_set("str", "test");

nvram_set_int("int", 2048);

ptr = nvram_get("str");

tmp = nvram_get_int("int");

nvram_commit();

nvram_getall(buf, 256);

nvram_reset();

ptr = nvram_get("is_default");

nvram_close();

nvram_unset("is_default");

ptr = nvram_get("is_default");

nvram_nset("test", "%s_file", "a");

ptr = nvram_nget("%s_file", "a");

foreach_nvram_from("/tmp/test.ini", test, "5");

return 0;
}

0 comments on commit 027b1c0

Please sign in to comment.