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, '=');
58 * We clean the environment before invoking openconnect/vpnc,
59 * but might as well still filter out the few things that get
60 * added that we're not interested in
62 if (!strcmp(key, "PWD") || !strcmp(key, "_") ||
63 !strcmp(key, "SHLVL") || !strcmp(key, "connman_busname") ||
64 !strcmp(key, "connman_network"))
67 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
70 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
72 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &value);
74 dbus_message_iter_close_container(dict, &entry);
77 int main(int argc, char *argv[])
82 DBusMessageIter iter, dict;
83 char **envp, *busname, *reason, *interface, *path;
86 openlog(basename(argv[0]), LOG_NDELAY | LOG_PID, LOG_DAEMON);
88 busname = getenv("CONNMAN_BUSNAME");
89 interface = getenv("CONNMAN_INTERFACE");
90 path = getenv("CONNMAN_PATH");
92 reason = getenv("reason");
94 if (!busname || !interface || !path || !reason) {
95 print("Required environment variables not set");
100 if (strcmp(reason, "pre-init") == 0)
103 dbus_error_init(&error);
105 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
107 if (dbus_error_is_set(&error)) {
108 print("%s", error.message);
109 dbus_error_free(&error);
111 print("Failed to get on system bus");
116 msg = dbus_message_new_method_call(busname, path,
117 interface, "notify");
119 dbus_connection_unref(conn);
120 print("Failed to allocate method call");
124 dbus_message_set_no_reply(msg, TRUE);
126 dbus_message_append_args(msg,
127 DBUS_TYPE_STRING, &reason,
130 dbus_message_iter_init_append(msg, &iter);
132 dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
133 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
134 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING
135 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
137 for (envp = environ; envp && *envp; envp++)
138 append(&dict, *envp);
140 dbus_message_iter_close_container(&iter, &dict);
142 if (!dbus_connection_send(conn, msg, NULL)) {
143 print("Failed to send message");
147 dbus_connection_flush(conn);
149 dbus_message_unref(msg);
151 dbus_connection_unref(conn);