1 | /* Implementation of the Data Store. */
|
---|
2 |
|
---|
3 | #include "inc.h"
|
---|
4 |
|
---|
5 | /* Allocate space for the data store. */
|
---|
6 | PRIVATE struct data_store ds_store[NR_DS_KEYS];
|
---|
7 | PRIVATE int nr_in_use;
|
---|
8 |
|
---|
9 | PRIVATE _PROTOTYPE(int find_key, (int key, struct data_store **dsp));
|
---|
10 | PRIVATE _PROTOTYPE(int set_owner, (struct data_store *dsp, void *auth_ptr));
|
---|
11 | PRIVATE _PROTOTYPE(int is_authorized, (struct data_store *dsp, void *auth_ptr));
|
---|
12 |
|
---|
13 |
|
---|
14 | PRIVATE int set_owner(dsp, ap)
|
---|
15 | struct data_store *dsp; /* data store structure */
|
---|
16 | void *ap; /* authorization pointer */
|
---|
17 | {
|
---|
18 | /* Authorize the caller. */
|
---|
19 | return(TRUE);
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | PRIVATE int is_authorized(dsp, ap)
|
---|
24 | struct data_store *dsp; /* data store structure */
|
---|
25 | void *ap; /* authorization pointer */
|
---|
26 | {
|
---|
27 | /* Authorize the caller. */
|
---|
28 | return(TRUE);
|
---|
29 | }
|
---|
30 |
|
---|
31 |
|
---|
32 | PRIVATE int find_key(key, dsp)
|
---|
33 | int key; /* key to look up */
|
---|
34 | struct data_store **dsp; /* store pointer here */
|
---|
35 | {
|
---|
36 | register int i;
|
---|
37 |
|
---|
38 | *dsp = NULL;
|
---|
39 | for (i=0; i<NR_DS_KEYS; i++) {
|
---|
40 | if ((ds_store[i].ds_flags & DS_IN_USE) && ds_store[i].ds_key == key) {
|
---|
41 | *dsp = &ds_store[i];
|
---|
42 | return(TRUE); /* report success */
|
---|
43 | }
|
---|
44 | }
|
---|
45 | return(FALSE); /* report not found */
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | PUBLIC int do_publish(m_ptr)
|
---|
50 | message *m_ptr; /* request message */
|
---|
51 | {
|
---|
52 | struct data_store *dsp;
|
---|
53 |
|
---|
54 | /* Store (key,value)-pair. First see if key already exists. If so,
|
---|
55 | * check if the caller is allowed to overwrite the value. Otherwise
|
---|
56 | * find a new slot and store the new value.
|
---|
57 | */
|
---|
58 | if (find_key(m_ptr->DS_KEY, &dsp)) { /* look up key */
|
---|
59 | if (! is_authorized(dsp,m_ptr->DS_AUTH)) { /* check if owner */
|
---|
60 | return(EPERM);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | else { /* find a new slot */
|
---|
64 | if (nr_in_use >= NR_DS_KEYS) {
|
---|
65 | return(EAGAIN); /* store is full */
|
---|
66 | } else {
|
---|
67 | dsp = &ds_store[nr_in_use]; /* new slot found */
|
---|
68 | dsp->ds_key = m_ptr->DS_KEY;
|
---|
69 | if (! set_owner(dsp,m_ptr->DS_AUTH)) { /* associate owner */
|
---|
70 | return(EINVAL);
|
---|
71 | }
|
---|
72 | dsp->ds_nr_subs = 0; /* nr of subscribers */
|
---|
73 | dsp->ds_flags = DS_IN_USE; /* initialize slot */
|
---|
74 | nr_in_use ++;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /* At this point we have a data store pointer and know the caller is
|
---|
79 | * authorize to write to it. Set all fields as requested.
|
---|
80 | */
|
---|
81 | dsp->ds_val_l1 = m_ptr->DS_VAL_L1; /* store all data */
|
---|
82 | dsp->ds_val_l2 = m_ptr->DS_VAL_L2;
|
---|
83 |
|
---|
84 | /* If the data is public. Check if there are any subscribers to this key.
|
---|
85 | * If so, notify all subscribers so that they can retrieve the data, if
|
---|
86 | * they're still interested.
|
---|
87 | */
|
---|
88 | if ((dsp->ds_flags & DS_PUBLIC) && dsp->ds_nr_subs > 0) {
|
---|
89 |
|
---|
90 | /* Subscriptions are not yet implemented. */
|
---|
91 | }
|
---|
92 |
|
---|
93 | return(OK);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | PUBLIC int do_retrieve(m_ptr)
|
---|
98 | message *m_ptr; /* request message */
|
---|
99 | {
|
---|
100 | struct data_store *dsp;
|
---|
101 |
|
---|
102 | /* Retrieve data. Look up the key in the data store. Return an error if it
|
---|
103 | * is not found. If this data is private, only the owner may retrieve it.
|
---|
104 | */
|
---|
105 | if (find_key(m_ptr->DS_KEY, &dsp)) { /* look up key */
|
---|
106 |
|
---|
107 | /* If the data is not public, the caller must be authorized. */
|
---|
108 | if (! dsp->ds_flags & DS_PUBLIC) { /* check if private */
|
---|
109 | if (! is_authorized(dsp,m_ptr->DS_AUTH)) { /* authorize call */
|
---|
110 | return(EPERM); /* not allowed */
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Data is public or the caller is authorized to retrieve it. */
|
---|
115 | printf("DS retrieves data: key %d (found %d), l1 %u, l2 %u\n",
|
---|
116 | m_ptr->DS_KEY, dsp->ds_key, dsp->ds_val_l1, dsp->ds_val_l2);
|
---|
117 | m_ptr->DS_VAL_L1 = dsp->ds_val_l1; /* return value */
|
---|
118 | m_ptr->DS_VAL_L2 = dsp->ds_val_l2; /* return value */
|
---|
119 | return(OK); /* report success */
|
---|
120 | }
|
---|
121 | return(ESRCH); /* key not found */
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | PUBLIC int do_subscribe(m_ptr)
|
---|
126 | message *m_ptr; /* request message */
|
---|
127 | {
|
---|
128 | /* Subscribe to a key of interest. Only existing and public keys can be
|
---|
129 | * subscribed to. All updates to the key will cause a notification message
|
---|
130 | * to be sent to the subscribed. On success, directly return a copy of the
|
---|
131 | * data for the given key.
|
---|
132 | */
|
---|
133 | return(ENOSYS);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /*===========================================================================*
|
---|
138 | * do_getsysinfo *
|
---|
139 | *===========================================================================*/
|
---|
140 | PUBLIC int do_getsysinfo(m_ptr)
|
---|
141 | message *m_ptr;
|
---|
142 | {
|
---|
143 | vir_bytes src_addr, dst_addr;
|
---|
144 | int dst_proc;
|
---|
145 | size_t len;
|
---|
146 | int s;
|
---|
147 |
|
---|
148 | switch(m_ptr->m1_i1) {
|
---|
149 | case SI_DATA_STORE:
|
---|
150 | src_addr = (vir_bytes) ds_store;
|
---|
151 | len = sizeof(struct data_store) * NR_DS_KEYS;
|
---|
152 | break;
|
---|
153 | default:
|
---|
154 | return(EINVAL);
|
---|
155 | }
|
---|
156 |
|
---|
157 | dst_proc = m_ptr->m_source;
|
---|
158 | dst_addr = (vir_bytes) m_ptr->m1_p1;
|
---|
159 | if (OK != (s=sys_datacopy(SELF, src_addr, dst_proc, dst_addr, len)))
|
---|
160 | return(s);
|
---|
161 | return(OK);
|
---|
162 | }
|
---|
163 |
|
---|