1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <semaphore.h>
#include <string.h>
#include <sys/ipc.h> /* for system's IPC_xxx definitions */
#include <sys/shm.h> /* for shmget, shmat, shmdt, shmctl */
#include <sys/sem.h> /* for semget, semctl, semop */
#include "utils.h"
#include "md5.h"
void md5ify(char *inString, char *outString) {
md5_state_t state;
md5_byte_t digest[16];
int i;
md5_init(&state);
md5_append(&state, (const md5_byte_t *)inString, strlen(inString));
md5_finish(&state, digest);
for (i = 0; i < 16; i++)
sprintf(&outString[i * 2], "%02x", digest[i]);
}
void say(const char *pName, char *pMessage) {
time_t the_time;
struct tm *the_localtime;
char timestamp[256];
the_time = time(NULL);
the_localtime = localtime(&the_time);
strftime(timestamp, 255, "%H:%M:%S", the_localtime);
printf("%s @ %s: %s\n", pName, timestamp, pMessage);
}
int release_semaphore(const char *pName, int sem_id, int live_dangerously) {
int rc = 0;
struct sembuf op[1];
char s[1024];
say(pName, "Releasing the semaphore.");
if (!live_dangerously) {
op[0].sem_num = 0;
op[0].sem_op = 1;
op[0].sem_flg = 0;
if (-1 == semop(sem_id, op, (size_t)1)) {
sprintf(s, "Releasing the semaphore failed; errno is %d\n", errno);
say(pName, s);
}
}
return rc;
}
int acquire_semaphore(const char *pName, int sem_id, int live_dangerously) {
int rc = 0;
struct sembuf op[1];
char s[1024];
say(pName, "Waiting to acquire the semaphore.");
if (!live_dangerously) {
op[0].sem_num = 0;
op[0].sem_op = -1;
op[0].sem_flg = 0;
if (-1 == semop(sem_id, op, (size_t)1)) {
sprintf(s, "Acquiring the semaphore failed; errno is %d\n", errno);
say(pName, s);
}
}
return rc;
}
void read_params(struct param_struct *params) {
char line[1024];
char name[1024];
int value = 0;
FILE *fp;
fp = fopen("params.txt", "r");
while (fgets(line, 1024, fp)) {
if (strlen(line) && ('#' == line[0]))
; // comment in input, ignore
else {
sscanf(line, "%[ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghjiklmnopqrstuvwxyz]=%i\n", name, &value);
// printf("name = %s, value = %d\n", name, value);
if (!strcmp(name, "ITERATIONS"))
params->iterations = value;
if (!strcmp(name, "LIVE_DANGEROUSLY"))
params->live_dangerously = value;
if (!strcmp(name, "KEY"))
params->key = value;
if (!strcmp(name, "PERMISSIONS"))
params->permissions = value;
if (!strcmp(name, "SHM_SIZE"))
params->size = value;
name[0] = '\0';
value = 0;
}
}
// printf("iterations = %d\n", params->iterations);
// printf("danger = %d\n", params->live_dangerously);
// printf("key = %d\n", params->key);
// printf("permissions = %o\n", params->permissions);
// printf("size = %d\n", params->size);
}
|