Created
May 22, 2015 19:29
-
-
Save stassats/3d537eb630df1fba671a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <dbus/dbus.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#ifdef LOG_ERRORS | |
#include <syslog.h> | |
#endif | |
/* gcc -Os `pkg-config --cflags --libs dbus-1` mtu-dbus.c -o mtu-dbus */ | |
static void terminateOnError(const char* msg, | |
const DBusError* error) { | |
if (dbus_error_is_set(error)) { | |
#ifdef LOG_ERRORS | |
syslog (LOG_ERR, error->name); | |
syslog(LOG_ERR, error->message); | |
#endif | |
exit(EXIT_FAILURE); | |
} | |
} | |
int wifi_connection = 0; | |
void change_mtu() { | |
system("ifconfig wlan0 mtu 1492 > /dev/null"); | |
} | |
DBusHandlerResult signal_filter(DBusConnection *connection, DBusMessage * message , | |
void *user_data) | |
{ | |
DBusError error; | |
dbus_error_init(&error); | |
DBusMessageIter iter; | |
char *key, * value; | |
dbus_message_iter_init (message, &iter); | |
int current_type; | |
while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID) { | |
if (current_type == DBUS_TYPE_STRING) { | |
dbus_message_iter_get_basic (&iter, &key); | |
} | |
else if (current_type == DBUS_TYPE_VARIANT) { | |
DBusMessageIter sub_iter; | |
dbus_message_iter_recurse(&iter, &sub_iter); | |
while ((current_type = dbus_message_iter_get_arg_type (&sub_iter)) != DBUS_TYPE_INVALID) { | |
if (current_type == DBUS_TYPE_STRING) { | |
dbus_message_iter_get_basic (&sub_iter, &value); | |
} | |
dbus_message_iter_next (&sub_iter); | |
} | |
} | |
dbus_message_iter_next (&iter); | |
} | |
if (!strcmp(key, "DefaultTechnology")) { | |
wifi_connection = !strcmp(value, "wifi"); | |
} else if (wifi_connection && | |
!strcmp(key, "ConnectionState") && !strcmp(value, "online")) { | |
change_mtu(); | |
} | |
dbus_error_free(&error); | |
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
} | |
static void daemonize() | |
{ | |
pid_t pid; | |
if ((pid = fork()) < 0) | |
exit(EXIT_FAILURE); | |
if (pid > 0) | |
exit(EXIT_SUCCESS); | |
if (setsid() < 0) | |
exit(EXIT_FAILURE); | |
umask(0); | |
chdir("/"); | |
close(STDIN_FILENO); | |
close(STDOUT_FILENO); | |
close(STDERR_FILENO); | |
} | |
int main () | |
{ | |
daemonize(); | |
DBusConnection* bus = NULL; | |
DBusMessage* msg = NULL; | |
DBusError error; | |
dbus_error_init(&error); | |
#ifdef LOG_ERRORS | |
openlog ("mtu-dbus", LOG_PID, LOG_DAEMON); | |
#endif | |
change_mtu(); | |
bus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); | |
terminateOnError("Failed to open Session bus\n", &error); | |
char *rule = "type='signal',interface='org.chromium.flimflam.Manager',member='PropertyChanged'"; | |
dbus_bus_add_match(bus, rule, &error); | |
terminateOnError("adding rule", &error); | |
dbus_connection_add_filter(bus, signal_filter, NULL, NULL); | |
while (dbus_connection_read_write_dispatch (bus, -1)) | |
; | |
#ifdef LOG_ERRORS | |
closelog(); | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment