Print this page
5910 libnisdb won't build with modern GCC
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libnisdb/yptol/ttl_utils.c
+++ new/usr/src/lib/libnisdb/yptol/ttl_utils.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 + * Copyright 2015 Gary Mills
23 24 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 25 * Use is subject to license terms.
25 26 */
26 27
27 -#pragma ident "%Z%%M% %I% %E% SMI"
28 -
29 28 /*
30 29 * DESCRIPTION: Contains utilities relating to TTL calculation.
31 30 */
32 31 #include <unistd.h>
33 32 #include <syslog.h>
34 33 #include <errno.h>
35 34 #include <strings.h>
36 35 #include <ndbm.h>
37 36 #include "ypsym.h"
38 37 #include "ypdefs.h"
39 38 #include "shim.h"
40 39 #include "yptol.h"
41 40 #include "../ldap_util.h"
42 41
43 42 /*
44 43 * Constants used in time calculations
45 44 */
46 45 #define MILLION 1000000
47 46
48 47 /*
49 48 * Decs
50 49 */
51 50 suc_code is_greater_timeval(struct timeval *, struct timeval *);
52 51 suc_code add_to_timeval(struct timeval *, int);
53 52
54 53 /*
55 54 * FUNCTION: has_entry_expired()
56 55 *
57 56 * DESCRIPTION: Determines if an individual entry has expired.
58 57 *
59 58 * INPUTS: Map control structure for an open map
60 59 * Entry key
61 60 *
62 61 * OUTPUTS: TRUE = Entry has expired or cannot be found this will cause
63 62 * missing entries to be pulled out of the DIT.
64 63 * FALSE = Entry has not expired
65 64 *
66 65 */
67 66 bool_t
68 67 has_entry_expired(map_ctrl *map, datum *key)
69 68 {
70 69 datum ttl;
71 70 struct timeval now;
72 71 struct timeval old_time;
73 72 char *key_name;
74 73 char *myself = "has_entry_expired";
75 74
76 75 if ((map == NULL) || (map->ttl == NULL))
77 76 return (FALSE);
78 77
79 78 /* Get expiry time entry for key */
80 79 ttl = dbm_fetch(map->ttl, *key);
81 80
82 81 if (NULL == ttl.dptr) {
83 82 /*
84 83 * If we failed to get a map expiry key, which must always be
85 84 * present, then something is seriously wrong. Try to recreate
86 85 * the map.
87 86 */
88 87 if ((key->dsize == strlen(MAP_EXPIRY_KEY)) &&
89 88 (0 == strncmp(key->dptr, MAP_EXPIRY_KEY, key->dsize))) {
90 89 logmsg(MSG_NOTIMECHECK, LOG_ERR, "Cannot find %s TTL "
91 90 "for map %s. Will attempt to recreate map",
92 91 MAP_EXPIRY_KEY, map->map_name);
93 92 return (TRUE);
94 93 }
95 94
96 95 /*
97 96 * Not a problem just no TTL entry for this entry. Maybe it has
98 97 * not yet been downloaded. Maybe it will be handled by a
99 98 * service other than NIS. Check if the entire map has expired.
100 99 * This prevents repeated LDAP reads when requests are made for
101 100 * nonexistant entries.
102 101 */
103 102 if (has_map_expired(map)) {
104 103 /* Kick of a map update */
105 104 update_map_if_required(map, FALSE);
106 105 }
107 106
108 107 /* Don't update the entry */
109 108 return (FALSE);
110 109 }
111 110
112 111 if (ttl.dsize != sizeof (struct timeval)) {
113 112 /*
114 113 * Need to malloc some memory before can syslog the key name
115 114 * but this may fail. Solution log a simple message first THEn
116 115 * a more detailed one if it works.
117 116 */
118 117 logmsg(MSG_NOTIMECHECK, LOG_ERR,
119 118 "Invalid TTL key in map %s. error %d",
120 119 map->map_name, dbm_error(map->ttl));
121 120
122 121 /* Log the key name */
123 122 key_name = (char *)am(myself, key->dsize + 1);
124 123 if (NULL == key_name) {
125 124 logmsg(MSG_NOMEM, LOG_ERR,
126 125 "Could not alloc memory for keyname");
127 126 } else {
128 127 strncpy(key_name, key->dptr, key->dsize);
129 128 key_name[key->dsize] = '\0';
130 129 logmsg(MSG_NOTIMECHECK, LOG_ERR,
131 130 "Key name was %s", key_name);
132 131 sfree(key_name);
133 132 }
134 133 /* Update it Anyway */
135 134 return (TRUE);
136 135 }
137 136
138 137 /* Get current time */
139 138 gettimeofday(&now, NULL);
140 139
141 140 /*
142 141 * Because dptr may not be int aligned need to build an int
143 142 * out of what it points to or will get a bus error
144 143 */
145 144 bcopy(ttl.dptr, &old_time, sizeof (struct timeval));
146 145
147 146 return (is_greater_timeval(&now, &old_time));
148 147 }
149 148
150 149 /*
151 150 * FUNCTION: has_map_expired()
152 151 *
153 152 * DESCRIPTION: Determines if an entire map has expire
154 153 *
155 154 * INPUTS: Map control structure for an open map
156 155 *
157 156 * OUTPUTS: TRUE = Map has expired
158 157 * FALSE Map has not expired
159 158 *
160 159 */
161 160 bool_t
162 161 has_map_expired(map_ctrl *map)
163 162 {
164 163 datum key;
165 164
166 165 /* Set up datum with magic expiry key */
167 166 key.dsize = strlen(MAP_EXPIRY_KEY);
168 167 key.dptr = MAP_EXPIRY_KEY;
169 168
170 169 /* Call has_entry_expired() with magic map expiry key */
171 170 return (has_entry_expired(map, &key));
172 171 }
173 172
174 173 /*
175 174 * FUNCTION: update_entry_ttl()
176 175 *
177 176 * DESCRIPTION: Updates the TTL for one map entry
178 177 *
179 178 * INPUTS: Map control structure for an open map
180 179 * Entry key
181 180 * Flag indication if TTL should be max, min or random
182 181 *
183 182 * OUTPUTS: SUCCESS = TTL updated
184 183 * FAILURE = TTL not updated
185 184 *
186 185 */
187 186
188 187 suc_code
189 188 update_entry_ttl(map_ctrl *map, datum *key, TTL_TYPE type)
190 189 {
191 190 datum expire;
192 191 struct timeval now;
193 192 int ttl;
194 193
195 194 /* Get current time */
196 195 gettimeofday(&now, NULL);
197 196
198 197 /* Get TTL from mapping file */
199 198 ttl = get_ttl_value(map, type);
200 199
201 200 if (FAILURE == add_to_timeval(&now, ttl))
202 201 return (FAILURE);
203 202
204 203 /* Convert time into a datum */
205 204 expire.dsize = sizeof (struct timeval);
206 205 expire.dptr = (char *)&now;
207 206
208 207 /* Set expiry time entry for key */
209 208 errno = 0;
210 209 if (0 > dbm_store(map->ttl, *key, expire, DBM_REPLACE)) {
211 210 logmsg(MSG_NOTIMECHECK, LOG_ERR, "Could not write TTL entry "
212 211 "(errno=%d)", errno);
213 212 return (FAILURE);
214 213 }
215 214
216 215 return (SUCCESS);
217 216 }
218 217
219 218 /*
220 219 * FUNCTION: update_map_ttl()
221 220 *
222 221 * DESCRIPTION: Updates the TTL for entire map. This can be called either with
223 222 * the map open (map_ctrl DBM pointer set up) or the map closed
224 223 * (map_ctrl DBM pointers not set). The latter case will occur
225 224 * when we have just created a new map.
226 225 *
227 226 * This function must open the TTL map but, in either case, must
228 227 * return with the map_ctrl in it's original state.
229 228 *
230 229 * INPUTS: Map control structure for an open map
231 230 *
232 231 * OUTPUTS: SUCCESS = TTL updated
233 232 * FAILURE = TTL not updated
234 233 *
235 234 */
236 235 suc_code
237 236 update_map_ttl(map_ctrl *map)
238 237 {
239 238 datum key;
240 239 bool_t map_was_open = TRUE;
241 240 suc_code ret;
242 241
243 242 /* Set up datum with magic expiry key */
244 243 key.dsize = strlen(MAP_EXPIRY_KEY);
245 244 key.dptr = MAP_EXPIRY_KEY;
246 245
247 246 /* If TTL not open open it */
248 247 if (NULL == map->ttl) {
249 248 map->ttl = dbm_open(map->ttl_path, O_RDWR, 0644);
250 249 if (NULL == map->ttl)
251 250 return (FAILURE);
252 251 map_was_open = FALSE;
253 252 }
254 253
255 254 /* Call update_entry_ttl() with magic map expiry key */
256 255 ret = update_entry_ttl(map, &key, TTL_MIN);
257 256
258 257 /* If we had to open TTL file close it */
259 258 if (!map_was_open) {
260 259 dbm_close(map->ttl);
261 260 map->ttl_path = NULL;
262 261 }
263 262
264 263 return (ret);
265 264 }
266 265
267 266 /*
268 267 * FUNCTION: add_to_timeval()
269 268 *
270 269 * DESCRIPTION: Adds an int to a timeval
271 270 *
272 271 * NOTE : Seems strange that there is not a library function to do this
273 272 * if one exists then this function can be removed.
274 273 *
275 274 * NOTE : Does not handle UNIX clock wrap round but this is a much bigger
276 275 * problem.
277 276 *
↓ open down ↓ |
239 lines elided |
↑ open up ↑ |
278 277 * INPUTS: Time value to add to
279 278 * Time value to add in seconds
280 279 *
281 280 * OUTPUTS: SUCCESS = Addition successful
282 281 * FAILURE = Addition failed (probably wrapped)
283 282 *
284 283 */
285 284 suc_code
286 285 add_to_timeval(struct timeval *t1, int t2)
287 286 {
288 - long usec;
289 287 struct timeval oldval;
290 288
291 289 oldval.tv_sec = t1->tv_sec;
292 290
293 291 /* Add seconds part */
294 292 t1->tv_sec += t2;
295 293
296 294 /* Check for clock wrap */
297 295 if (!(t1->tv_sec >= oldval.tv_sec)) {
298 296 logmsg(MSG_NOTIMECHECK, LOG_ERR,
299 297 "Wrap when adding %d to %d", t2, oldval.tv_sec);
300 298 return (FAILURE);
301 299 }
302 300
303 301 return (SUCCESS);
304 302 }
305 303
306 304 /*
307 305 * FUNCTION: is_greater_timeval()
308 306 *
309 307 * DESCRIPTION: Compares two timevals
310 308 *
311 309 * NOTE : Seems strange that there is not a library function to do this
312 310 * if one exists then this function can be removed.
313 311 *
314 312 * INPUTS: First time value
315 313 * Time value to compare it with
316 314 *
317 315 * OUTPUTS: TRUE t1 > t2
318 316 * FALSE t1 <= t2
319 317 *
320 318 */
321 319 suc_code
322 320 is_greater_timeval(struct timeval *t1, struct timeval *t2)
323 321 {
324 322 if (t1->tv_sec > t2->tv_sec)
325 323 return (TRUE);
326 324
327 325 if (t1->tv_sec == t2->tv_sec) {
328 326 if (t1->tv_usec > t2->tv_usec)
329 327 return (TRUE);
330 328 else
331 329 return (FALSE);
332 330 }
333 331
334 332 return (FALSE);
335 333 }
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX