udevadm-control.c revision e4255f115330b949c1caeb561cbe0e39f5225f50
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering/*
b7ff1e47da4653d2ff1ec7b489e6067fed29d8c5Lennart Poettering * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
c904f64d84db8c4eebedf210ba10893f19ba05edLennart Poettering *
c904f64d84db8c4eebedf210ba10893f19ba05edLennart Poettering * This program is free software; you can redistribute it and/or modify it
f80781eaf9f927d7b4d5e66116e3f3a4242e6fa1Lennart Poettering * under the terms of the GNU General Public License as published by the
f80781eaf9f927d7b4d5e66116e3f3a4242e6fa1Lennart Poettering * Free Software Foundation version 2 of the License.
f80781eaf9f927d7b4d5e66116e3f3a4242e6fa1Lennart Poettering *
05677bb78079c3fa0283101aac2c07581f4873f1Lennart Poettering * This program is distributed in the hope that it will be useful, but
73090dc815390f4fca4e3ed8a7e1d3806605daaaLennart Poettering * WITHOUT ANY WARRANTY; without even the implied warranty of
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35c5c7a01daeb2c83c693deea07c8f2d0d6c83e2Lennart Poettering * General Public License for more details.
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering *
ddd88763921a1534081ed28e36f6712a85449005Lennart Poettering * You should have received a copy of the GNU General Public License along
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * with this program; if not, write to the Free Software Foundation, Inc.,
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers *
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers */
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering
7d441ddb5ca090b5a97f58ac4b4d97b3e84fa81eLennart Poettering#include "config.h"
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers#include <time.h>
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers#include <errno.h>
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers#include <stdio.h>
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers#include <stdlib.h>
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers#include <stddef.h>
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers#include <string.h>
a2f5666d06fe8233025738047115bb9e3959df3eLennart Poettering#include <unistd.h>
a2f5666d06fe8233025738047115bb9e3959df3eLennart Poettering#include <getopt.h>
ab7f148f81723ef18402b78ea34fb5e1bbe85ff0Lennart Poettering#include <sys/types.h>
ab7f148f81723ef18402b78ea34fb5e1bbe85ff0Lennart Poettering#include <sys/socket.h>
ad740100d108282d0244d5739d4dcc86fe4c5fdeLennart Poettering#include <sys/wait.h>
ad740100d108282d0244d5739d4dcc86fe4c5fdeLennart Poettering#include <sys/un.h>
c821bd28c2ecce8d35248d61949fe1c0c3030b6cLennart Poettering
c821bd28c2ecce8d35248d61949fe1c0c3030b6cLennart Poettering#include "udev.h"
7d441ddb5ca090b5a97f58ac4b4d97b3e84fa81eLennart Poettering#include "udevd.h"
7d441ddb5ca090b5a97f58ac4b4d97b3e84fa81eLennart Poettering
de6c78f8795743894431a099d26ec562a8acf3dfLennart Poetteringstatic int udev_log = 0;
7d441ddb5ca090b5a97f58ac4b4d97b3e84fa81eLennart Poettering
7d441ddb5ca090b5a97f58ac4b4d97b3e84fa81eLennart Poetteringstruct udev_ctrl;
7d441ddb5ca090b5a97f58ac4b4d97b3e84fa81eLennart Poetteringextern struct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path);
7d441ddb5ca090b5a97f58ac4b4d97b3e84fa81eLennart Poetteringextern void udev_ctrl_unref(struct udev_ctrl *uctrl);
14e639ae7a1dbf156273ce697d30fbc6c6594209Lennart Poetteringextern int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority);
14e639ae7a1dbf156273ce697d30fbc6c6594209Lennart Poetteringextern int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl);
b38bb4937972546dcc60ab5ea4a7d4b898349c1cKay Sieversextern int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl);
034a2a52ac0ec83e0229941d635d310b23eb04dfLennart Poetteringextern int udev_ctrl_reload_rules(struct udev_ctrl *uctrl);
ff01d048b4c1455241c894cf7982662c9d28fd34Lennart Poetteringextern int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key);
ff01d048b4c1455241c894cf7982662c9d28fd34Lennart Poetteringextern int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count);
72b9ed828bd22f3ddd74b6853c183eebf006d6d8Lennart Poetteringextern int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count);
72b9ed828bd22f3ddd74b6853c183eebf006d6d8Lennart Poettering
1d6702e8d3877c0bebf3ac817dc45ff72f5ecfa9Lennart Poetteringstruct udev_ctrl {
1d6702e8d3877c0bebf3ac817dc45ff72f5ecfa9Lennart Poettering int sock;
1d6702e8d3877c0bebf3ac817dc45ff72f5ecfa9Lennart Poettering struct sockaddr_un saddr;
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering socklen_t addrlen;
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering};
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering
0a55b298d930543c8065bb9e708dd112562b1736Lennart Poetteringstruct udev_ctrl *udev_ctrl_new_from_socket(const char *socket_path)
97f73ffb04947acf0a5854e3a7bdbb7a0105f6faLennart Poettering{
97f73ffb04947acf0a5854e3a7bdbb7a0105f6faLennart Poettering struct udev_ctrl *uctrl;
85f248b26653f5322c26735661d63d4e8460c30eLennart Poettering
85f248b26653f5322c26735661d63d4e8460c30eLennart Poettering uctrl = malloc(sizeof(struct udev_ctrl));
253ee27a0c7a410d27d490bb79ea97caed6a2b68Lennart Poettering if (uctrl == NULL)
85f248b26653f5322c26735661d63d4e8460c30eLennart Poettering return NULL;
1258097cd3cdbc5dd3d264850119e553a29c5068Lennart Poettering memset(uctrl, 0x00, sizeof(struct udev_ctrl));
1258097cd3cdbc5dd3d264850119e553a29c5068Lennart Poettering
1258097cd3cdbc5dd3d264850119e553a29c5068Lennart Poettering uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
a4c279f87451186b8beb1b8cc21c7cad561ecf4bLennart Poettering if (uctrl->sock < 0) {
a4c279f87451186b8beb1b8cc21c7cad561ecf4bLennart Poettering err("error getting socket: %s\n", strerror(errno));
a4c279f87451186b8beb1b8cc21c7cad561ecf4bLennart Poettering free(uctrl);
a4c279f87451186b8beb1b8cc21c7cad561ecf4bLennart Poettering return NULL;
d3fc81bd6a5a046b22600ac1204df220c93d2c15Lennart Poettering }
d3fc81bd6a5a046b22600ac1204df220c93d2c15Lennart Poettering
d3fc81bd6a5a046b22600ac1204df220c93d2c15Lennart Poettering uctrl->saddr.sun_family = AF_LOCAL;
d3fc81bd6a5a046b22600ac1204df220c93d2c15Lennart Poettering strcpy(uctrl->saddr.sun_path, socket_path);
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
253ee27a0c7a410d27d490bb79ea97caed6a2b68Lennart Poettering /* translate leading '@' to abstract namespace */
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering if (uctrl->saddr.sun_path[0] == '@')
8d0e38a2b966799af884e78a54fd6a2dffa44788Lennart Poettering uctrl->saddr.sun_path[0] = '\0';
f28f1daf754a9a07de90e6fc4ada581bf5de677dLennart Poettering return uctrl;
f28f1daf754a9a07de90e6fc4ada581bf5de677dLennart Poettering}
f28f1daf754a9a07de90e6fc4ada581bf5de677dLennart Poettering
f28f1daf754a9a07de90e6fc4ada581bf5de677dLennart Poetteringvoid udev_ctrl_unref(struct udev_ctrl *uctrl)
88a07670cfa974a605c7c7b520b8a3135fce37f9Lennart Poettering{
88a07670cfa974a605c7c7b520b8a3135fce37f9Lennart Poettering if (uctrl == NULL)
88a07670cfa974a605c7c7b520b8a3135fce37f9Lennart Poettering return;
916abb21d0a6653e0187b91591e492026886b0a4Lennart Poettering close(uctrl->sock);
916abb21d0a6653e0187b91591e492026886b0a4Lennart Poettering}
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering
916abb21d0a6653e0187b91591e492026886b0a4Lennart Poetteringstatic int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
4ecda405d1c26f82fb7afe96ea4c48ca1bd19e17Lennart Poettering{
4ecda405d1c26f82fb7afe96ea4c48ca1bd19e17Lennart Poettering struct udevd_ctrl_msg ctrl_msg;
b23de6af893c11da4286bc416455cd0926d1532eLennart Poettering int err;
b23de6af893c11da4286bc416455cd0926d1532eLennart Poettering
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
adf70c5e4e6673353b8d702baee4e1d21eb05dfeKay Sievers strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
21bdae12e11ae20460715475d8a0c991f15464acLennart Poettering ctrl_msg.type = type;
21bdae12e11ae20460715475d8a0c991f15464acLennart Poettering
9534ce54858c67363b841cdbdc315140437bfdb4Lennart Poettering if (buf != NULL)
9534ce54858c67363b841cdbdc315140437bfdb4Lennart Poettering strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
73090dc815390f4fca4e3ed8a7e1d3806605daaaLennart Poettering else
19bc719ec28e731159671b06254d9bfd49014894Lennart Poettering ctrl_msg.intval = intval;
19bc719ec28e731159671b06254d9bfd49014894Lennart Poettering
73090dc815390f4fca4e3ed8a7e1d3806605daaaLennart Poettering err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
c798c40ec957a35cd4b7d04a196a9e58880c6ff3Lennart Poettering if (err == -1) {
c798c40ec957a35cd4b7d04a196a9e58880c6ff3Lennart Poettering err("error sending message: %s\n", strerror(errno));
68c7d001f4117f0c3d0a4582e32cbb03ae5fac57Lennart Poettering }
68c7d001f4117f0c3d0a4582e32cbb03ae5fac57Lennart Poettering return err;
68c7d001f4117f0c3d0a4582e32cbb03ae5fac57Lennart Poettering}
7a2a0b907b5cc60f5d9a871997d7d6e7f62bf4d8Lennart Poettering
253ee27a0c7a410d27d490bb79ea97caed6a2b68Lennart Poetteringint udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
5d0fcd7c8d29340ac9425c309e8ac436a9af699cLennart Poettering{
5d0fcd7c8d29340ac9425c309e8ac436a9af699cLennart Poettering ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
5d0fcd7c8d29340ac9425c309e8ac436a9af699cLennart Poettering return 0;
0c85a4f3efa2883c414ed8ff59aea263b85b7687Lennart Poettering}
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers
f957632b960a0a42999b38ded7089fa602b41745Kay Sieversint udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
73090dc815390f4fca4e3ed8a7e1d3806605daaaLennart Poettering{
44143309dd0b37d61d7d842ca58f01a65646ec71Kay Sievers ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
3d57c6ab801f4437f12948e29589e3d00c3ad9dbLennart Poettering return 0;
b9a2a36b519ccd79c4198e7dda4e657d597a14adLennart Poettering}
b9a2a36b519ccd79c4198e7dda4e657d597a14adLennart Poettering
ba1a55152c50dfbcd3d4a64353b95f4a2f37985eLennart Poetteringint udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
9408a2d295a312a5472345090e28e0502570494bLennart Poettering{
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
3f7a8c4e9f1d3ce48919e24eb2c9d56dd6fd88d8Kay Sievers return 0;
f9276855a1d270b6c3f857cdaf2c4b49920c2228Lennart Poettering}
f9276855a1d270b6c3f857cdaf2c4b49920c2228Lennart Poettering
260abb780a135e4cae8c10715c7e85675efc345aLennart Poetteringint udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
260abb780a135e4cae8c10715c7e85675efc345aLennart Poettering{
260abb780a135e4cae8c10715c7e85675efc345aLennart Poettering ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
2791a8f8dc8764a9247cdba3562bd4c04010f144Lennart Poettering return 0;
a8f11321c209830a35edd0357e8def5d4437d854Lennart Poettering}
a8f11321c209830a35edd0357e8def5d4437d854Lennart Poettering
21bdae12e11ae20460715475d8a0c991f15464acLennart Poetteringint udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
21bdae12e11ae20460715475d8a0c991f15464acLennart Poettering{
c32e0c40f7e706e3ebcd101187d5ced96f083491Lennart Poettering ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
< Error!

 

There was an error!

null

java.lang.NullPointerException