Fix D-Bus timeout handling
[platform/upstream/connman.git] / scripts / udhcpc-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 UDHCPC_INTF  "net.busybox.udhcpc"
33 #define UDHCPC_PATH  "/net/busybox/udhcpc"
34
35 int main(int argc, char *argv[])
36 {
37         DBusConnection *conn;
38         DBusError error;
39         DBusMessage *msg;
40         char *busname, *interface, *address, *netmask, *broadcast;
41         char *gateway, *dns;
42
43         if (argc < 2)
44                 return 0;
45
46         if (strcmp(argv[1], "bound") != 0 && strcmp(argv[1], "renew") != 0)
47                 return 0;
48
49         busname = "org.moblin.connman";
50
51         interface = getenv("interface");
52
53         address = getenv("ip");
54         if (address == NULL)
55                 address = "";
56
57         netmask = getenv("subnet");
58         if (netmask == NULL)
59                 netmask = "";
60
61         broadcast = getenv("broadcast");
62         if (broadcast == NULL)
63                 broadcast = "";
64
65         gateway = getenv("router");
66         if (gateway == NULL)
67                 gateway = "";
68
69         dns = getenv("dns");
70         if (dns == NULL)
71                 dns = "";
72
73         dbus_error_init(&error);
74
75         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
76         if (conn == NULL) {
77                 if (dbus_error_is_set(&error) == TRUE) {
78                         fprintf(stderr, "%s\n", error.message);
79                         dbus_error_free(&error);
80                 } else
81                         fprintf(stderr, "Failed to get on system bus\n");
82                 return 0;
83         }
84
85         msg = dbus_message_new_method_call(busname, UDHCPC_PATH,
86                                                 UDHCPC_INTF, argv[1]);
87         if (msg == NULL) {
88                 dbus_connection_unref(conn);
89                 fprintf(stderr, "Failed to allocate method call\n");
90                 return 0;
91         }
92
93         dbus_message_set_no_reply(msg, TRUE);
94
95         dbus_message_append_args(msg, DBUS_TYPE_STRING, &interface,
96                                         DBUS_TYPE_STRING, &address,
97                                         DBUS_TYPE_STRING, &netmask,
98                                         DBUS_TYPE_STRING, &broadcast,
99                                         DBUS_TYPE_STRING, &gateway,
100                                         DBUS_TYPE_STRING, &dns,
101                                                         DBUS_TYPE_INVALID);
102
103         if (dbus_connection_send(conn, msg, NULL) == FALSE)
104                 fprintf(stderr, "Failed to send message\n");
105
106         dbus_connection_flush(conn);
107
108         dbus_message_unref(msg);
109
110         dbus_connection_unref(conn);
111
112         return 0;
113 }