annotate usr/src/cmd/hal/addons/network-devices/cache.c @ 4:1a15d5aaf794

synchronized with onnv_86 (6202) in onnv-gate
author Koji Uno <koji.uno@sun.com>
date Mon, 31 Aug 2009 14:38:03 +0900
parents c9caec207d52
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
1 /*
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
2 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
3 * Use is subject to license terms.
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
4 *
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
5 * Licensed under the Academic Free License version 2.1
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
6 */
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
7
4
1a15d5aaf794 synchronized with onnv_86 (6202) in onnv-gate
Koji Uno <koji.uno@sun.com>
parents: 0
diff changeset
8 #pragma ident "%Z%%M% %I% %E% SMI"
0
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
9
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
10 #include <stdio.h>
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
11 #include <stdlib.h>
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
12 #include <unistd.h>
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
13 #include <string.h>
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
14
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
15 #include <libhal.h>
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
16 #include <logger.h>
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
17
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
18 #include <glib.h>
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
19
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
20 #include "network-discovery.h"
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
21
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
22 /*
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
23 * The interfaces in this file comprise a means of keeping track of devices
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
24 * that we have already seen and those that have gone missing. This allows
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
25 * us to quickly determine if we need to probe the device and quickly search
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
26 * for devices that are no longer available.
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
27 */
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
28
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
29 typedef struct {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
30 LibHalContext *ctx;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
31 time_t timestamp;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
32 } removal_args_t;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
33
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
34 static GHashTable *seen = NULL;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
35
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
36 static gboolean
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
37 device_remove_if_stale(gpointer key, gpointer value, gpointer user_data)
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
38 {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
39 gboolean result = FALSE;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
40 removal_args_t *args = user_data;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
41 char *name = key;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
42 time_t *val = value;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
43
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
44 HAL_DEBUG(("test stale: %s (%d > %d)", name, args->timestamp, *val));
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
45 if (args->timestamp > *val) {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
46 DBusError error;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
47 char **udi = NULL;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
48 int num = 0;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
49
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
50 dbus_error_init(&error);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
51 udi = libhal_manager_find_device_string_match(args->ctx,
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
52 "network_device.address", name,
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
53 &num, &error);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
54
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
55 if (udi != NULL) {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
56 int i;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
57
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
58 for (i = 0; i < num; i++) {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
59 libhal_remove_device(args->ctx, udi[i], &error);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
60 HAL_DEBUG(("remove: %s (%s)", name, udi[i]));
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
61 }
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
62 libhal_free_string_array(udi);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
63 result = TRUE;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
64 }
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
65 if (dbus_error_is_set(&error))
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
66 dbus_error_free(&error);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
67 }
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
68
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
69 return (result);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
70 }
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
71
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
72 void
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
73 scan_for_stale_devices(LibHalContext *ctx, time_t timestamp)
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
74 {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
75 if (seen != NULL) {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
76 removal_args_t args[1];
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
77
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
78 args->ctx = ctx;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
79 args->timestamp = timestamp;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
80
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
81 g_hash_table_foreach_remove(seen, device_remove_if_stale, args);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
82 }
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
83 }
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
84
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
85 gboolean
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
86 device_seen(char *name)
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
87 {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
88 gboolean result;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
89 char *key;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
90 time_t *val;
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
91
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
92 if (seen == NULL)
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
93 seen = g_hash_table_new_full(g_str_hash, g_str_equal,
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
94 free, free);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
95
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
96 result = g_hash_table_lookup_extended(seen, name,
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
97 (gpointer)&key, (gpointer)&val);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
98
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
99 if ((result == FALSE) && ((val = calloc(1, sizeof (*val))) != NULL)) {
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
100 g_hash_table_insert(seen, strdup(name), val);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
101 }
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
102 (void) time(val);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
103 HAL_DEBUG(("seen: %s (%d)", name, *val));
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
104
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
105 return (result);
c9caec207d52 Initial porting based on b86
Koji Uno <koji.uno@sun.com>
parents:
diff changeset
106 }