test-udev.c revision 220893b3cbdbf8932f95c44811b169a8f0d33939
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering/*
39cff5961ab5d08c9685d5c2c43152aafe2b7285Kay Sievers * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
39cff5961ab5d08c9685d5c2c43152aafe2b7285Kay Sievers * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
39cff5961ab5d08c9685d5c2c43152aafe2b7285Kay Sievers *
7c66aeba0f28cb82027d6015405ed71afa3b6059Kay Sievers * This program is free software: you can redistribute it and/or modify
7c66aeba0f28cb82027d6015405ed71afa3b6059Kay Sievers * it under the terms of the GNU General Public License as published by
c904f64d84db8c4eebedf210ba10893f19ba05edLennart Poettering * the Free Software Foundation, either version 2 of the License, or
c904f64d84db8c4eebedf210ba10893f19ba05edLennart Poettering * (at your option) any later version.
05677bb78079c3fa0283101aac2c07581f4873f1Lennart Poettering *
73090dc815390f4fca4e3ed8a7e1d3806605daaaLennart Poettering * This program is distributed in the hope that it will be useful,
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * but WITHOUT ANY WARRANTY; without even the implied warranty of
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers * GNU General Public License for more details.
f957632b960a0a42999b38ded7089fa602b41745Kay Sievers *
9a36607584bbd1d78775353e022a51794b4e27b1Lennart Poettering * You should have received a copy of the GNU General Public License
9a36607584bbd1d78775353e022a51794b4e27b1Lennart Poettering * along with this program. If not, see <http://www.gnu.org/licenses/>.
a40593a0d0d740efa387e35411e1e456a6c5aba7Lennart Poettering */
20ffc4c4a9226b0e45cc02ad9c0108981626c0bbKay Sievers
4ce849853c46b1e857df3c6c9e7752159a58ddf1Lennart Poettering#include <stdio.h>
4ce849853c46b1e857df3c6c9e7752159a58ddf1Lennart Poettering#include <stddef.h>
c3090674833c8bd34fbdb0e743f1c47d85dd14fbLennart Poettering#include <stdlib.h>
c3090674833c8bd34fbdb0e743f1c47d85dd14fbLennart Poettering#include <string.h>
71092d70af35567dd154d3de2ce04ce62e157a7cLennart Poettering#include <fcntl.h>
822e5dd1d6a1e9b549234281dc3a746768e7e13dLennart Poettering#include <ctype.h>
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers#include <errno.h>
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers#include <unistd.h>
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers#include <syslog.h>
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers#include <grp.h>
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers#include <sys/signalfd.h>
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers#include "udev.h"
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sieversvoid udev_main_log(struct udev *udev, int priority,
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers const char *file, int line, const char *fn,
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers const char *format, va_list args) {}
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sieversint main(int argc, char *argv[])
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers{
d2e83c23f5f0cdd3b6ec05c5c40209708721e704Kay Sievers struct udev *udev;
c55b1b59b837dfd924b704d457ed77c55f8bfeabLennart Poettering struct udev_event *event = NULL;
822e5dd1d6a1e9b549234281dc3a746768e7e13dLennart Poettering struct udev_device *dev = NULL;
822e5dd1d6a1e9b549234281dc3a746768e7e13dLennart Poettering struct udev_rules *rules = NULL;
822e5dd1d6a1e9b549234281dc3a746768e7e13dLennart Poettering char syspath[UTIL_PATH_SIZE];
822e5dd1d6a1e9b549234281dc3a746768e7e13dLennart Poettering const char *devpath;
6c1703cc35b3a5f93ad3cc813fea10cb9a636102Kay Sievers const char *action;
6c1703cc35b3a5f93ad3cc813fea10cb9a636102Kay Sievers sigset_t mask, sigmask_orig;
6c1703cc35b3a5f93ad3cc813fea10cb9a636102Kay Sievers int err = -EINVAL;
6c1703cc35b3a5f93ad3cc813fea10cb9a636102Kay Sievers
6c1703cc35b3a5f93ad3cc813fea10cb9a636102Kay Sievers udev = udev_new();
08f9588885c5d65694b324846b0ed19211d2c178Lennart Poettering if (udev == NULL)
9ec82de1725ddaab333149171b790d62c47ae133Lennart Poettering exit(1);
9ec82de1725ddaab333149171b790d62c47ae133Lennart Poettering info(udev, "version %s\n", VERSION);
9ec82de1725ddaab333149171b790d62c47ae133Lennart Poettering udev_selinux_init(udev);
9ec82de1725ddaab333149171b790d62c47ae133Lennart Poettering
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering sigprocmask(SIG_SETMASK, NULL, &sigmask_orig);
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering action = argv[1];
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering if (action == NULL) {
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering err(udev, "action missing\n");
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering goto out;
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering }
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering devpath = argv[2];
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering if (devpath == NULL) {
e707c49485b8f4f2ec040d3da232d39153e650b9Lennart Poettering err(udev, "devpath missing\n");
220369cc0c3e167af2eee8bdac95a6157e0e2b62Lennart Poettering goto out;
220369cc0c3e167af2eee8bdac95a6157e0e2b62Lennart Poettering }
220369cc0c3e167af2eee8bdac95a6157e0e2b62Lennart Poettering
220369cc0c3e167af2eee8bdac95a6157e0e2b62Lennart Poettering rules = udev_rules_new(udev, 1);
a73d88fa024b5668ed7dde681e99547d41e6a864Lennart Poettering
a73d88fa024b5668ed7dde681e99547d41e6a864Lennart Poettering util_strscpyl(syspath, sizeof(syspath), udev_get_sys_path(udev), devpath, NULL);
f2d433e178df7df01a836e95775261e1d85ec60dZbigniew Jędrzejewski-Szmek dev = udev_device_new_from_syspath(udev, syspath);
f2d433e178df7df01a836e95775261e1d85ec60dZbigniew Jędrzejewski-Szmek if (dev == NULL) {
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering info(udev, "unknown device '%s'\n", devpath);
f2d433e178df7df01a836e95775261e1d85ec60dZbigniew Jędrzejewski-Szmek goto out;
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering }
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering udev_device_set_action(dev, action);
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering event = udev_event_new(dev);
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering sigfillset(&mask);
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering sigprocmask(SIG_SETMASK, &mask, &sigmask_orig);
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering if (event->fd_signal < 0) {
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering fprintf(stderr, "error creating signalfd\n");
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering goto out;
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering }
a19554ed92a7460b4e709cc40c558cde827ab85bLennart Poettering
1cb88f2c61f590083847d65cd5a518e834da87d3Lennart Poettering /* do what devtmpfs usually provides us */
1cb88f2c61f590083847d65cd5a518e834da87d3Lennart Poettering if (udev_device_get_devnode(dev) != NULL) {
1cb88f2c61f590083847d65cd5a518e834da87d3Lennart Poettering mode_t mode;
1cb88f2c61f590083847d65cd5a518e834da87d3Lennart Poettering
1cb88f2c61f590083847d65cd5a518e834da87d3Lennart Poettering if (strcmp(udev_device_get_subsystem(dev), "block") == 0)
603cd8fe07cb03e8b11722d1a732e569e5a46347Lennart Poettering mode |= S_IFBLK;
06bf461193b4e7f9936abf7582e8b82e39e187c8Lennart Poettering else
06bf461193b4e7f9936abf7582e8b82e39e187c8Lennart Poettering mode |= S_IFCHR;
936d6fcb6c4fc8839d28f8585af6ba733a7e1a1aLennart Poettering
936d6fcb6c4fc8839d28f8585af6ba733a7e1a1aLennart Poettering if (strcmp(action, "remove") != 0) {
936d6fcb6c4fc8839d28f8585af6ba733a7e1a1aLennart Poettering util_create_path(udev, udev_device_get_devnode(dev));
936d6fcb6c4fc8839d28f8585af6ba733a7e1a1aLennart Poettering mknod(udev_device_get_devnode(dev), mode, udev_device_get_devnum(dev));
936d6fcb6c4fc8839d28f8585af6ba733a7e1a1aLennart Poettering } else {
936d6fcb6c4fc8839d28f8585af6ba733a7e1a1aLennart Poettering unlink(udev_device_get_devnode(dev));
ea117d4fde8b8d0b52f9d32ebd4bc09a5bd2ca8bLennart Poettering util_delete_path(udev, udev_device_get_devnode(dev));
ea117d4fde8b8d0b52f9d32ebd4bc09a5bd2ca8bLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering err = udev_event_execute_rules(event, rules, &sigmask_orig);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (err == 0)
a7a3f28be404875eff20443a0fa8088bcc4c18dfLennart Poettering udev_event_execute_run(event, NULL);
a7a3f28be404875eff20443a0fa8088bcc4c18dfLennart Poetteringout:
9a526a06bd22ccaf6641d11323fb04a0512a0e49Lennart Poettering if (event != NULL && event->fd_signal >= 0)
9a526a06bd22ccaf6641d11323fb04a0512a0e49Lennart Poettering close(event->fd_signal);
a8985ba3c2ad428bf572c636f9d64c4ce52bfbe7Lennart Poettering udev_event_unref(event);
a8985ba3c2ad428bf572c636f9d64c4ce52bfbe7Lennart Poettering udev_device_unref(dev);
9b27910bb0c23e5225fc1177176e4f9bf9bf787bLennart Poettering udev_rules_unref(rules);
9b27910bb0c23e5225fc1177176e4f9bf9bf787bLennart Poettering udev_selinux_exit(udev);
b03bfa212dd23397871e36ea32c35cdd8fe43506Lennart Poettering udev_unref(udev);
b03bfa212dd23397871e36ea32c35cdd8fe43506Lennart Poettering if (err != 0)
935fb723ba7370abaf793914fb5a722f7f5e41e1Lennart Poettering return 1;
b03bfa212dd23397871e36ea32c35cdd8fe43506Lennart Poettering return 0;
b03bfa212dd23397871e36ea32c35cdd8fe43506Lennart Poettering}
b03bfa212dd23397871e36ea32c35cdd8fe43506Lennart Poettering