summaryrefslogtreecommitdiff
path: root/demo/utils.c
blob: e9682a4b731f6c6c6cc9ad2131c3ad779a954e09 (plain)
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
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <semaphore.h>
#include <string.h>

#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, sem_t *pSemaphore, int live_dangerously) {
    int rc = 0;
    char s[1024];
    
    say(pName, "Releasing the semaphore.");
    
    if (!live_dangerously) {
        rc = sem_post(pSemaphore);
        if (rc) {
            sprintf(s, "Releasing the semaphore failed; errno is %d\n", errno);
            say(pName, s);
        }
    }
    
    return rc;
}


int acquire_semaphore(const char *pName, sem_t *pSemaphore, int live_dangerously) {
    int rc = 0;
    char s[1024];

    say(pName, "Waiting to acquire the semaphore.");

    if (!live_dangerously) {
        rc = sem_wait(pSemaphore);
        if (rc) {
            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];
    char value[1024];
    
    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]=%s\n", name, value);
        
            // printf("name = %s, value = %d\n", name, value);

            if (!strcmp(name, "ITERATIONS"))
                params->iterations = atoi(value);
            if (!strcmp(name, "LIVE_DANGEROUSLY"))
                params->live_dangerously = atoi(value);
            if (!strcmp(name, "SEMAPHORE_NAME"))
                strcpy(params->semaphore_name, value);
            if (!strcmp(name, "SHARED_MEMORY_NAME"))
                strcpy(params->shared_memory_name, value);
            if (!strcmp(name, "PERMISSIONS"))
                params->permissions = (int)strtol(value, NULL, 8);
            if (!strcmp(name, "SHM_SIZE"))
                params->size = atoi(value);
                
            name[0] = '\0';
            value[0] = '\0';
        }
    }
    
    printf("iterations = %d\n", params->iterations);
    printf("danger = %d\n", params->live_dangerously);
    printf("semaphore name = %s\n", params->semaphore_name);
    printf("shared memory name = %s\n", params->shared_memory_name);
    printf("permissions = %o\n", params->permissions);
    printf("size = %d\n", params->size);
}