5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
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.
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.
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
32 #include <dbus/dbus.h>
34 extern char **environ;
36 static void print(const char *format, ...)
41 vsyslog(LOG_INFO, format, ap);
45 static void append(DBusMessageIter *dict, const char *pattern)
47 DBusMessageIter entry;
48 const char *key, *value;
51 delim = strchr(pattern, '=');
57 /* We clean the environment before invoking openconnect, but
58 might as well still filter out the few things that get
59 added that we're not interested in */
60 if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
61 !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") ||
62 !strcmp(key, "connman_network"))
65 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
68 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
70 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
72 dbus_message_iter_close_container(dict, &entry);
75 int main(int argc, char *argv[])
80 DBusMessageIter iter, dict;
81 char **envp, *busname, *reason, *interface, *path;
84 openlog(basename(argv[0]), LOG_NDELAY | LOG_PID, LOG_DAEMON);
86 busname = getenv("CONNMAN_BUSNAME");
87 interface = getenv("CONNMAN_INTERFACE");
88 path = getenv("CONNMAN_PATH");
90 reason = getenv("reason");
92 if (!busname || !interface || !path || !reason) {
93 print("Required environment variables not set");
98 if (strcmp(reason, "pre-init") == 0)
101 dbus_error_init(&error);
103 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
105 if (dbus_error_is_set(&error)) {
106 print("%s", error.message);
107 dbus_error_free(&error);
109 print("Failed to get on system bus");
114 msg = dbus_message_new_method_call(busname, path,
115 interface, "notify");
117 dbus_connection_unref(conn);
118 print("Failed to allocate method call");
122 dbus_message_set_no_reply(msg, TRUE);
124 dbus_message_append_args(msg,
125 DBUS_TYPE_STRING, &reason,
128 dbus_message_iter_init_append(msg, &iter);
130 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
131 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
132 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
133 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
135 for (envp = environ; envp && *envp; envp++)
136 append(&dict, *envp);
138 dbus_message_iter_close_container(&iter, &dict);
140 if (!dbus_connection_send(conn, msg, NULL)) {
141 print("Failed to send message");
145 dbus_connection_flush(conn);
147 dbus_message_unref(msg);
149 dbus_connection_unref(conn);