Remove obsolete file.
[connman] / scripts / dhclient-script.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <dbus/dbus.h>
31
32 #define DHCLIENT_INTF "org.isc.dhclient"
33 #define DHCLIENT_PATH "/org/isc/dhclient"
34
35 extern char **environ;
36
37 static void append(DBusMessageIter *dict, const char *pattern)
38 {
39         DBusMessageIter entry;
40         const char *key, *value;
41         char *delim;
42
43         delim = strchr(pattern, '=');
44         *delim = '\0';
45
46         key = pattern;
47         value = delim + 1;
48
49         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
50                                                         NULL, &entry);
51
52         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
53
54         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
55
56         dbus_message_iter_close_container(dict, &entry);
57 }
58
59 int main(int argc, char *argv[])
60 {
61         DBusConnection *conn;
62         DBusError error;
63         DBusMessage *msg;
64         DBusMessageIter iter, dict;
65         dbus_uint32_t pid;
66         char **envp, *busname, *reason, *interface;
67
68         busname = getenv("BUSNAME");
69
70         pid = atoi(getenv("pid"));
71         reason = getenv("reason");
72         interface = getenv("interface");
73
74         dbus_error_init(&error);
75
76         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
77         if (conn == NULL) {
78                 if (dbus_error_is_set(&error) == TRUE) {
79                         fprintf(stderr, "%s\n", error.message);
80                         dbus_error_free(&error);
81                 } else
82                         fprintf(stderr, "Failed to get on system bus\n");
83                 return 0;
84         }
85
86         msg = dbus_message_new_method_call(busname, DHCLIENT_PATH,
87                                                 DHCLIENT_INTF, "notify");
88         if (msg == NULL) {
89                 dbus_connection_unref(conn);
90                 fprintf(stderr, "Failed to allocate method call\n");
91                 return 0;
92         }
93
94         dbus_message_set_no_reply(msg, TRUE);
95
96         dbus_message_append_args(msg, DBUS_TYPE_UINT32, &pid,
97                                 DBUS_TYPE_STRING, &reason, DBUS_TYPE_INVALID);
98
99         dbus_message_iter_init_append(msg, &iter);
100
101         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
102                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
103                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
104                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
105
106         for (envp = environ; envp && *envp; envp++) {
107                 if (strlen(*envp) < 5)
108                         continue;
109
110                 if (strncmp(*envp, "new_", 4) == 0 ||
111                                 strncmp(*envp, "old_", 4) == 0 ||
112                                         strncmp(*envp, "alia", 4) == 0)
113                         append(&dict, *envp);
114         }
115
116         dbus_message_iter_close_container(&iter, &dict);
117
118         if (dbus_connection_send(conn, msg, NULL) == FALSE)
119                 fprintf(stderr, "Failed to send message\n");
120
121         dbus_connection_flush(conn);
122
123         dbus_message_unref(msg);
124
125         dbus_connection_unref(conn);
126
127         return 0;
128 }