rfkill.c revision b5efdb8af40ea759a1ea584c1bc44ecc81dd00ce
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <poll.h>
#include "libudev.h"
#include "sd-daemon.h"
#include "alloc-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "mkdir.h"
#include "parse-util.h"
#include "proc-cmdline.h"
#include "string-table.h"
#include "string-util.h"
#include "udev-util.h"
#include "util.h"
static const char* const rfkill_type_table[NUM_RFKILL_TYPES] = {
[RFKILL_TYPE_ALL] = "all",
[RFKILL_TYPE_WLAN] = "wlan",
[RFKILL_TYPE_BLUETOOTH] = "bluetooth",
[RFKILL_TYPE_UWB] = "uwb",
[RFKILL_TYPE_WIMAX] = "wimax",
[RFKILL_TYPE_WWAN] = "wwan",
[RFKILL_TYPE_GPS] = "gps",
[RFKILL_TYPE_FM] = "fm",
[RFKILL_TYPE_NFC] = "nfc",
};
static int find_device(
const struct rfkill_event *event,
struct udev_device **ret) {
struct udev_device *device;
const char *name;
return log_oom();
if (!device)
if (!name) {
log_debug("Device has no name, ignoring.");
return -ENOENT;
}
return 0;
}
static int wait_for_initialized(
struct udev_device *device,
struct udev_device **ret) {
struct udev_device *d;
const char *sysname;
int watch_fd, r;
if (udev_device_get_is_initialized(device) != 0) {
return 0;
}
/* Wait until the device is initialized, so that we can get
* access to the ID_PATH property */
if (!monitor)
if (r < 0)
return log_error_errno(r, "Failed to add rfkill udev match to monitor: %m");
if (r < 0)
return log_error_errno(r, "Failed to enable udev receiving: %m");
if (watch_fd < 0)
/* Check again, maybe things changed */
if (!d)
if (udev_device_get_is_initialized(d) != 0) {
*ret = d;
return 0;
}
for (;;) {
if (r == -EINTR)
continue;
if (r < 0)
return log_error_errno(r, "Failed to watch udev monitor: %m");
if (!t)
continue;
*ret = udev_device_ref(t);
return 0;
}
}
}
static int determine_state_file(
const struct rfkill_event *event,
struct udev_device *d,
char **ret) {
char *state_file;
int r;
assert(d);
if (r < 0)
return r;
if (path_id) {
if (!escaped_path_id)
return log_oom();
} else
if (!state_file)
return log_oom();
*ret = state_file;
return 0;
}
static int load_state(
int rfkill_fd,
const struct rfkill_event *event) {
struct rfkill_event we;
ssize_t l;
int b, r;
if (!shall_restore_state())
return 0;
if (r < 0)
return r;
if (r < 0)
return r;
if (r == -ENOENT) {
/* No state file? Then save the current state */
r = write_string_file(state_file, one_zero(event->soft), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
if (r < 0)
return 0;
}
if (r < 0)
b = parse_boolean(value);
if (b < 0)
we = (struct rfkill_event) {
.op = RFKILL_OP_CHANGE,
.soft = b,
};
if (l < 0)
if (l != sizeof(we)) {
log_error("Couldn't write rfkill event structure, too short.");
return -EIO;
}
return 0;
}
static int save_state(
int rfkill_fd,
const struct rfkill_event *event) {
int r;
if (r < 0)
return r;
if (r < 0)
return r;
r = write_string_file(state_file, one_zero(event->soft), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
if (r < 0)
return 0;
}
bool ready = false;
int r, n;
if (argc > 1) {
log_error("This program requires no arguments.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
if (!udev) {
r = log_oom();
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to create rfkill directory: %m");
goto finish;
}
n = sd_listen_fds(false);
if (n < 0) {
r = log_error_errno(n, "Failed to determine whether we got any file descriptors passed: %m");
goto finish;
}
if (n > 1) {
log_error("Got too many file descriptors.");
r = -EINVAL;
goto finish;
}
if (n == 0) {
if (rfkill_fd < 0) {
r = 0;
goto finish;
}
goto finish;
}
} else {
if (r < 0) {
log_error_errno(r, "Failed to make /dev/rfkill socket non-blocking: %m");
goto finish;
}
}
for (;;) {
struct rfkill_event event;
const char *type;
ssize_t l;
if (l < 0) {
if (!ready) {
/* Notify manager that we are
* now finished with
* processing whatever was
* queued */
(void) sd_notify(false, "READY=1");
ready = true;
}
/* Hang around for a bit, maybe there's more coming */
if (r == -EINTR)
continue;
if (r < 0) {
log_error_errno(r, "Failed to poll() on device: %m");
goto finish;
}
if (r > 0)
continue;
log_debug("All events read and idle, exiting.");
break;
}
}
if (l != RFKILL_EVENT_SIZE_V1) {
log_error("Read event structure of invalid size.");
r = -EIO;
goto finish;
}
if (!type) {
continue;
}
case RFKILL_OP_ADD:
break;
case RFKILL_OP_DEL:
break;
case RFKILL_OP_CHANGE:
break;
default:
log_debug("Unknown event %i from /dev/rfkill for index %i and type %s, ignoring.", event.op, event.idx, type);
break;
}
}
r = 0;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}