Sophie

Sophie

distrib > Mandriva > 2006.0 > i586 > media > main-src > by-pkgid > 1355c2af6f8e938b349fa9081d34358d > files > 28

udev-068-34mdk.src.rpm

/*
 * A simple agent helper program.
 *
 * Olivier Blin (oblin@mandriva.com)
 * Copyright 2005 Mandriva
 *
 * This software may be freely redistributed under the terms of the GNU
 * public license.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <libgen.h>

#define AGENT_PATH "/etc/udev/agents.d"

int main(void) {
	char *devpath, *end, *subsystem, *p, *driver;
	char path[256];
	struct stat stats;
	char *argv[] = { NULL };

	devpath = getenv("DEVPATH");
	if (!devpath) return 0;
	end = devpath + strlen(devpath);
	subsystem = devpath + strlen("/bus/");
	if (subsystem >= end) return 0;

	p = index(subsystem, '/');
	if (!p) return 0;
	*p = '\0';
	driver = p + strlen("/drivers/");

	if (driver >= end) return 0;
	snprintf(path, sizeof(path), "%s/%s/%s", AGENT_PATH, subsystem, driver);

	if (!stat(path, &stats) && (stats.st_mode & S_IXUSR)) {
		return execv(path, argv);
	}

	return 0;
}