diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-04-16 01:13:54 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-04-16 01:13:54 +0200 |
commit | 02e1879da14cd64ba2181516808fdc617aa11e14 (patch) | |
tree | 22107cc402b3ac89b4bd7b0e7b6487481c1bc887 /src | |
parent | remove unused variable (diff) | |
download | bus-02e1879da14cd64ba2181516808fdc617aa11e14.tar.gz bus-02e1879da14cd64ba2181516808fdc617aa11e14.tar.bz2 bus-02e1879da14cd64ba2181516808fdc617aa11e14.tar.xz |
use a file for keys
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/cmdline.c | 86 |
1 files changed, 57 insertions, 29 deletions
diff --git a/src/cmdline.c b/src/cmdline.c index a5af8f1..7ee8c0c 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -1,3 +1,4 @@ +#define _XOPEN_SOURCE 700 #include <stdlib.h> #include <stdio.h> #include <time.h> @@ -28,7 +29,7 @@ union semun { #define MEMORY_SIZE (2 * 1024) -#define open_semaphore(key) ((sem_id = semget(key, SEMAPHORES, 0600)) == -1 ? -1 : 0) +#define open_semaphore() ((sem_id = semget(key_sem, SEMAPHORES, 0600)) == -1 ? -1 : 0) #define acquire_semaphore(semaphore, delta, undo) semaphore_op(semaphore, -delta, undo) #define release_semaphore(semaphore, delta, undo) semaphore_op(semaphore, +delta, undo) #define zero_semaphore(semaphore) semaphore_op(semaphore, 0, 0) @@ -41,13 +42,41 @@ union semun { char *argv0; static int sem_id = -1; +static key_t key_sem = -1; +static key_t key_shm = -1; static int +get_keys(void) +{ + int saved_errno; + char *line; + size_t len; + + line = NULL, len = 0; + t(getline(&line, &len, stdin)); + t(key_sem = (key_t)atoll(line)); + free(line); + + line = NULL, len = 0; + t(getline(&line, &len, stdin)); + t(key_sem = (key_t)atoll(line)); + free(line); + + return 0; + +fail: + saved_errno = errno; + free(line); + errno = saved_errno; + return -1; +} + + +static int create_semaphores(void) { - key_t key; int id = -1, saved_errno; union semun values; @@ -58,10 +87,10 @@ create_semaphores(void) double r = (double)rand(); r /= (double)RAND_MAX + 1; r *= (1 << (8 * sizeof(key_t) - 2)) - 1; - key = (key_t)r + 1; - if (key == IPC_PRIVATE) + key_sem = (key_t)r + 1; + if (key_sem == IPC_PRIVATE) continue; - id = semget(key, SEMAPHORES, IPC_CREAT | IPC_EXCL | 0600); + id = semget(key_sem, SEMAPHORES, IPC_CREAT | IPC_EXCL | 0600); if (id != -1) break; if ((errno != EEXIST) && (errno != EINTR)) @@ -78,7 +107,7 @@ create_semaphores(void) free(values.array); values.array = NULL; - printf("key:%zi(x%zx), id:%i\n", (ssize_t)key, (size_t)key, id); + printf("%zi\n", (ssize_t)key_sem); return 0; fail: @@ -94,7 +123,6 @@ fail: static int create_shared_memory(void) { - key_t key; int id = -1, saved_errno; struct shmid_ds _info; @@ -103,17 +131,17 @@ create_shared_memory(void) double r = (double)rand(); r /= (double)RAND_MAX + 1; r *= (1 << (8 * sizeof(key_t) - 2)) - 1; - key = (key_t)r + 1; - if (key == IPC_PRIVATE) + key_shm = (key_t)r + 1; + if (key_shm == IPC_PRIVATE) continue; - id = shmget(key, MEMORY_SIZE, IPC_CREAT | IPC_EXCL | 0600); + id = shmget(key_shm, MEMORY_SIZE, IPC_CREAT | IPC_EXCL | 0600); if (id != -1) break; if ((errno != EEXIST) && (errno != EINTR)) goto fail; } - printf("key:%zi(x%zx), id:%i\n", (ssize_t)key, (size_t)key, id); + printf("%zi\n", (ssize_t)key_shm); return 0; fail: @@ -126,18 +154,18 @@ fail: static int -remove_semaphores(key_t key) +remove_semaphores(void) { - int id = semget(key, SEMAPHORES, 0600); + int id = semget(key_shm, SEMAPHORES, 0600); return ((id == -1) || (semctl(id, 0, IPC_RMID) == -1)) ? -1 : 0; } static int -remove_shared_memory(key_t key) +remove_shared_memory(void) { struct shmid_ds _info; - int id = shmget(key, MEMORY_SIZE, 0600); + int id = shmget(key_shm, MEMORY_SIZE, 0600); return ((id == -1) || (shmctl(sem_id, IPC_RMID, &_info) == -1)) ? -1 : 0; } @@ -164,12 +192,12 @@ write_semaphore(int semaphore, int value) static int -read_shared_memory(key_t key, char *message) +read_shared_memory(char *message) { int id, saved_errno; void *address = NULL; - t(shmget(key, MEMORY_SIZE, 0600)); + t(shmget(key_shm, MEMORY_SIZE, 0600)); address = shmat(id, NULL, SHM_RDONLY); if ((address == (void *)-1) || !address) goto fail; @@ -187,12 +215,12 @@ fail: static int -write_shared_memory(key_t key, const char *message) +write_shared_memory(const char *message) { int id, saved_errno; void *address = NULL; - t(id = shmget(key, MEMORY_SIZE, 0600)); + t(id = shmget(key_shm, MEMORY_SIZE, 0600)); address = shmat(id, NULL, 0); if ((address == (void *)-1) || !address) goto fail; @@ -212,10 +240,7 @@ fail: int main(int argc, char *argv[]) { - int saved_errno; - key_t key_sem = argc > 2 ? (key_t)atoll(argv[2]) : 0; - key_t key_shm = argc > 3 ? (key_t)atoll(argv[3]) : 0; - const char *message = argc > 4 ? argv[4] : "default message"; + const char *message = argc > 2 ? argv[2] : "default message"; char read_message[MEMORY_SIZE]; argv0 = *argv; @@ -226,16 +251,18 @@ main(int argc, char *argv[]) t(create_shared_memory()); } else if (!strcmp(argv[1], "remove")) { - t(remove_semaphores(key_sem)); - t(remove_shared_memory(key_shm)); + t(get_keys()); + t(remove_semaphores()); + t(remove_shared_memory()); } else if (!strcmp(argv[1], "listen")) { - t(open_semaphore(key_sem)); + t(get_keys()); + t(open_semaphore()); t(release_semaphore(S, 1, 1)); for (;;) { t(release_semaphore(Q, 1, 0)); t(zero_semaphore(Q)); - t(read_shared_memory(key_shm, read_message)); + t(read_shared_memory(read_message)); printf("%s\n", read_message); t(release_semaphore(W, 1, 1)); t(acquire_semaphore(S, 1, 1)); @@ -245,10 +272,11 @@ main(int argc, char *argv[]) } } else if (!strcmp(argv[1], "broadcast")) { - t(open_semaphore(key_sem)); + t(get_keys()); + t(open_semaphore()); t(acquire_semaphore(X, 1, 1)); t(zero_semaphore(W)); - t(write_shared_memory(key_shm, message)); + t(write_shared_memory(message)); t(write_semaphore(Q, 0)); t(zero_semaphore(S)); t(release_semaphore(X, 1, 1)); |