~ubuntu-branches/debian/squeeze/hal/squeeze

« back to all changes in this revision

Viewing changes to tools/linux/hal-ipw-killswitch-linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2008-05-15 17:15:26 UTC
  • Revision ID: james.westby@ubuntu.com-20080515171526-wj087paax2bmuxwr
Tags: upstream-0.5.11
ImportĀ upstreamĀ versionĀ 0.5.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 *
22
22
 **************************************************************************/
23
23
 
 
24
#include <errno.h>
24
25
#include <stdio.h>
25
26
#include <string.h>
 
27
#include <syslog.h>
26
28
#include <glib.h>
27
29
#include <stdlib.h>
28
30
 
37
39
        char *udi;
38
40
        char *parent;
39
41
        char *iface;
40
 
        int i;
41
 
        char kill_status;
 
42
        int i, kill_status;
42
43
        char **udis;
43
44
        int num_udis;
44
45
        FILE *fd;
45
46
        char *path;
46
47
        int ret = -1;
47
48
 
48
 
        if(argc == 1) return -1;        
 
49
        if (argc == 1) 
 
50
                return -1;      
49
51
 
50
52
        dbus_error_init (&error);
51
53
        
52
 
        if((udi = getenv ("HAL_PROP_INFO_UDI")) == NULL) return -1;
 
54
        if ((udi = getenv ("HAL_PROP_INFO_UDI")) == NULL) return -1;
53
55
 
54
56
        if ((hal_ctx = libhal_ctx_new ()) == NULL) {
55
 
                fprintf (stderr, "error: libhal_ctx_new\n");
 
57
                syslog (LOG_INFO, "error: libhal_ctx_new\n");
56
58
                return -1;
57
59
        }
58
60
 
59
61
        if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) {
60
 
                fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
 
62
                syslog (LOG_INFO, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message);
61
63
                LIBHAL_FREE_DBUS_ERROR (&error);
62
64
                return -1;
63
65
        }
64
66
 
65
67
        if (!libhal_ctx_init (hal_ctx, &error)) {
66
 
                fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
 
68
                syslog (LOG_INFO, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
67
69
                LIBHAL_FREE_DBUS_ERROR (&error);
68
70
                return -1;
69
71
        }
71
73
 
72
74
        parent = libhal_device_get_property_string (hal_ctx, udi, "info.parent", &error);
73
75
        udis = libhal_manager_find_device_string_match (hal_ctx, "info.parent", parent, &num_udis, &error);
 
76
        
 
77
        if( argc==2 && strcmp("getrfkill",argv[1])==0) {
 
78
 
 
79
                for (i = 0; i < num_udis; i++) {
 
80
                        char buf[64];
 
81
 
 
82
                        if (strcmp (udis[i], udi) == 0) 
 
83
                                continue;
 
84
 
 
85
                        iface = libhal_device_get_property_string (hal_ctx, udis[i], "net.interface", &error);
 
86
                        if (iface != NULL) {
 
87
                                path = g_strdup_printf ("/sys/class/net/%s/device/rf_kill", iface);
 
88
 
 
89
                                if ((fd = fopen (path, "r")) == NULL) {
 
90
                                        return -1;
 
91
                                }
 
92
                                if (fgets (buf, sizeof (buf), fd) == NULL) {
 
93
                                        return -1;
 
94
                                }
 
95
 
 
96
                                errno = 0;
 
97
                                kill_status = strtol (buf, NULL, 10);
 
98
                                if (errno == 0) {
 
99
                                        /* syslog (LOG_INFO, "'%s' returned %d", path, kill_status); */
 
100
                                        
 
101
                                        switch(kill_status) {
 
102
                                                case 0:
 
103
                                                        ret = 0;
 
104
                                                        break;
 
105
                                                case 1:
 
106
                                                case 2:
 
107
                                                case 3:
 
108
                                                        ret = 1;
 
109
                                                        break;
 
110
                                                default:
 
111
                                                        break;
 
112
                                        }
 
113
                                }
 
114
 
 
115
                                fclose (fd);
 
116
                                g_free (path);
 
117
                                libhal_free_string (iface);
 
118
                        }
 
119
                }
 
120
        }
 
121
 
 
122
        if (argc == 3 && strcmp ("setrfkill", argv[1]) == 0 && (atoi (argv[2]) == 0 || atoi(argv[2]) == 1)) {
 
123
 
 
124
                for (i = 0; i < num_udis; i++) {
 
125
                        if (strcmp (udis[i], udi) == 0) 
 
126
                                continue;
 
127
 
 
128
                        iface = libhal_device_get_property_string (hal_ctx, udis[i], "net.interface", &error);
 
129
                        if (iface != NULL) {
 
130
                                path = g_strdup_printf ("/sys/class/net/%s/device/rf_kill", iface);
 
131
                                
 
132
                                if ((fd = fopen (path, "w")) == NULL) {
 
133
                                        return -1;
 
134
                                }
 
135
 
 
136
                                fputc (argv[2][0], fd);
 
137
                                fclose (fd);
 
138
 
 
139
                                g_free (path);
 
140
                                libhal_free_string (iface);
 
141
                        }
 
142
                }
 
143
        
 
144
                ret = 0;
 
145
        }
 
146
        
74
147
        libhal_free_string (parent);
75
 
        
76
 
        if(argc==2 && strcmp("getrfkill",argv[1])==0) {
77
 
 
78
 
                for (i = 0; i < num_udis; i++) {
79
 
 
80
 
                        if(strcmp (udis[i], udi) == 0) continue;
81
 
                        iface = libhal_device_get_property_string (hal_ctx, udis[i], "net.interface", &error);
82
 
                        path = g_strdup_printf ("/sys/class/net/%s/device/rf_kill", iface);
83
 
                        if((fd=fopen (path, "r")) == NULL) return -1;
84
 
                        kill_status = fgetc (fd);
85
 
                        fclose (fd);
86
 
                        g_free (path);
87
 
 
88
 
                        switch(kill_status) {
89
 
                                case '0':
90
 
                                        ret = 0;
91
 
                                break;
92
 
                                case '1':
93
 
                                case '2':
94
 
                                case '3':
95
 
                                        ret = 1;
96
 
                                break;
97
 
                        }
98
 
 
99
 
                        libhal_free_string (iface);
100
 
                }
101
 
        
102
 
                libhal_free_string_array (udis);
103
 
        }
104
 
 
105
 
        if(argc == 3 && strcmp ("setrfkill", argv[1]) == 0 && (atoi (argv[2]) == 0 || atoi(argv[2]) == 1)) {
106
 
 
107
 
                for (i = 0; i < num_udis; i++) {
108
 
 
109
 
                        if(strcmp (udis[i], udi)==0) continue;
110
 
                        iface = libhal_device_get_property_string (hal_ctx, udis[i], "net.interface", &error);
111
 
                        path = g_strdup_printf ("/sys/class/net/%s/device/rf_kill", iface);
112
 
                        if((fd=fopen (path, "w")) == NULL) return -1;
113
 
                        fputc (argv[2][0], fd);
114
 
                        fclose (fd);
115
 
                        g_free (path);
116
 
                        libhal_free_string (iface);
117
 
                }
118
 
        
119
 
                libhal_free_string_array (udis);        
120
 
 
121
 
                ret = 0;
122
 
        }
123
 
        
 
148
        libhal_free_string_array (udis);
124
149
        libhal_ctx_free (hal_ctx);
125
150
 
126
151
        if (dbus_error_is_set (&error)) {
127
 
                fprintf (stderr, "error: %s: %s\n", error.name, error.message);
 
152
                syslog (LOG_INFO, "error: %s: %s\n", error.name, error.message);
128
153
                dbus_error_free (&error);
129
154
                return -1;
130
155
        }