-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2015-7550.cpp
132 lines (108 loc) · 2.63 KB
/
2015-7550.cpp
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
129
130
131
132
#include <iostream>
#include "pthread.h"
#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
#include <time.h>
// #define TEST_TIME
#define EOPNOTSUPP 45
#define ENOKEY 132
#define EKEYEXPIRED 133
#define EKEYREVOKED 134
#define KEY_FLAG_DEAD 1
#define KEY_FLAG_REVOKED 2
#define KEY_FLAG_INVALIDATED 7
struct assoc_array
{
unsigned long nr_leaves_on_tree;
};
struct key
{
unsigned long flags;
pthread_mutex_t sem;
struct assoc_array *keys;
};
int key_validate(const struct key *key)
{
unsigned long flags = key->flags;
printf("flags = %ld\n", flags);
if (flags & (1 << KEY_FLAG_INVALIDATED))
return -ENOKEY;
if (flags & ((1 << KEY_FLAG_REVOKED) |
(1 << KEY_FLAG_DEAD)))
return -EKEYREVOKED;
return 0;
}
static long keyring_read(const struct key* keyring)
{
unsigned long nr_keys;
nr_keys = keyring->keys->nr_leaves_on_tree;
printf("nr_keys = %ld\n", nr_keys);
return nr_keys;
}
long keyctl_read_key(struct key *key)
{
long ret = key_validate(key);
if (ret == 0)
{
ret = -EOPNOTSUPP;
pthread_mutex_lock(&key->sem);
ret = keyring_read(key);
pthread_mutex_unlock(&key->sem);
}
return ret;
}
void keyring_revoke(struct key* keyring)
{
keyring->keys = NULL;
}
void key_revoke(struct key *key)
{
pthread_mutex_lock(&key->sem);
key->flags = KEY_FLAG_REVOKED;
puts("revoke key");
keyring_revoke(key);
pthread_mutex_unlock(&key->sem);
}
void* thread1(void* arg)
{
puts("thread 1");
struct key *key = (struct key*)arg;
key_revoke(key);
return NULL;
}
void* thread2(void* arg)
{
puts("thread 2");
struct key *key = (struct key*)arg;
keyctl_read_key(key);
return NULL;
}
int main()
{
#ifdef TEST_TIME
static double run_time_begin;
static double run_time_end;
static double run_time_total;
run_time_begin = clock();
#endif
struct key* key = (struct key*)malloc(sizeof(struct key));
key->flags = 0;
pthread_mutex_init(&(key->sem), NULL);
key->keys = (struct assoc_array*)malloc(sizeof(struct assoc_array));
key->keys->nr_leaves_on_tree = 1;
/*struct key key;
key.flags = 0;*/
pthread_t t1, t2;
pthread_create(&t2, NULL, thread2, key);
pthread_create(&t1, NULL, thread1, key);
pthread_join(t2, NULL);
pthread_join(t1, NULL);
printf("\nprogram-successful-exit\n");
#ifdef TEST_TIME
run_time_end = clock();
run_time_total = run_time_end - run_time_begin;
printf("test-the-total-time: %.3lf\n", (double)(run_time_total/CLOCKS_PER_SEC)*1000);
#endif
return 0;
}