libudev-monitor.c revision 8753fadf2a6ecead372e71b8bf9336cf29f9c958
/*
* libudev - interface to udev device information
*
* Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include "libudev.h"
#include "libudev-private.h"
struct udev_monitor {
int refcount;
int sock;
struct sockaddr_nl snl;
struct sockaddr_un sun;
};
/**
* udev_monitor_new_from_socket:
* @udev: udev library context
* @socket_path: unix socket path
*
* Create new udev monitor, setup and connect to a specified socket. The
* path to a socket can point to an existing socket file, or it will be
* created if needed. If neccessary, the permissions adjustment as well as
* the later cleanup of the socket file, needs to be done by the caller.
* If the socket path starts with a '@' character, an abstract namespace
* socket will be used.
*
* The initial refcount is 1, and needs to be decremented to
* release the ressources of the udev monitor.
*
* Returns: a new udev monitor, or #NULL, in case of an error
**/
{
struct udev_monitor *udev_monitor;
return NULL;
if (socket_path == NULL)
return NULL;
if (udev_monitor == NULL)
return NULL;
udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(udev_monitor->sun.sun_path);
/* translate leading '@' to abstract namespace */
return NULL;
}
return udev_monitor;
}
{
struct udev_monitor *udev_monitor;
return NULL;
if (udev_monitor == NULL)
return NULL;
return NULL;
}
return udev_monitor;
}
{
int err;
const int on = 1;
if (err < 0) {
return err;
}
if (err < 0) {
return err;
}
/* enable receiving of the sender credentials */
}
return 0;
}
/**
* udev_monitor_ref:
* @udev_monitor: udev monitor
*
* Take a reference of a udev monitor.
*
* Returns: the passed udev monitor
**/
{
if (udev_monitor == NULL)
return NULL;
udev_monitor->refcount++;
return udev_monitor;
}
/**
* udev_monitor_unref:
* @udev_monitor: udev monitor
*
* Drop a reference ofa udev monitor. If the refcount reaches zero,
* the bound socket will be closed, and the ressources of the monitor
* will be released.
*
**/
{
if (udev_monitor == NULL)
return;
udev_monitor->refcount--;
if (udev_monitor->refcount > 0)
return;
if (udev_monitor->sock >= 0)
}
/**
* udev_monitor_get_udev:
* @udev_monitor: udev monitor
*
* Retrieve the udev library context the monitor was created with.
*
* Returns: the udev library context
**/
{
if (udev_monitor == NULL)
return NULL;
return udev_monitor->udev;
}
/**
* udev_monitor_get_fd:
* @udev_monitor: udev monitor
*
* Retrieve the socket file descriptor associated with the monitor.
*
* Returns: the socket file descriptor
**/
{
if (udev_monitor == NULL)
return -1;
return udev_monitor->sock;
}
/**
* udev_monitor_receive_device:
* @udev_monitor: udev monitor
*
* Receive data from the udev monitor socket, allocate a new udev
* device, fill in the received data, and return the device.
*
* Only socket connections with uid=0 are accepted. The caller
* needs to make sure, that there is data to read from the socket,
* the call will block until the socket becomes readable.
*
* The initial refcount is 1, and needs to be decremented to
* release the ressources of the udev device.
*
* Returns: a new udev device, or #NULL, in case of an error
**/
{
struct udev_device *udev_device;
char buf[4096];
int maj = 0;
int min = 0;
if (udev_monitor == NULL)
return NULL;
return NULL;
}
return NULL;
}
return NULL;
}
}
/* skip header */
return NULL;
}
/* check message header */
return NULL;
}
if (udev_device == NULL) {
return NULL;
}
char *key;
if (keylen == 0)
break;
char path[UTIL_PATH_SIZE];
next[0] = '\0';
}
if (slink[0] != '\0')
}
continue;
}
return udev_device;
}