#ifndef BSWAP_H #define BSWAP_H //#include "config-host.h" #include #if defined(__NetBSD__) #include #include #elif defined(__OpenBSD__) #include #define bswap_16(x) swap16(x) #define bswap_32(x) swap32(x) #define bswap_64(x) swap64(x) #else #ifdef HAVE_BYTESWAP_H #include #else #define bswap_16(x) \ ({ \ uint16_t __x = (x); \ ((uint16_t)( \ (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \ (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \ }) #define bswap_32(x) \ ({ \ uint32_t __x = (x); \ ((uint32_t)( \ (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ }) #define bswap_64(x) \ ({ \ uint64_t __x = (x); \ ((uint64_t)( \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \ }) #endif /* !HAVE_BYTESWAP_H */ static inline uint16_t bswap16(uint16_t x) { return bswap_16(x); } static inline uint32_t bswap32(uint32_t x) { return bswap_32(x); } static inline uint64_t bswap64(uint64_t x) { return bswap_64(x); } static inline void bswap16s(uint16_t *s) { *s = bswap16(*s); } static inline void bswap32s(uint32_t *s) { *s = bswap32(*s); } static inline void bswap64s(uint64_t *s) { *s = bswap64(*s); } #endif #if defined(WORDS_BIGENDIAN) #define be_bswap(v, size) (v) #define le_bswap(v, size) bswap ## size(v) #define be_bswaps(v, size) #define le_bswaps(p, size) *p = bswap ## size(*p); #else #define le_bswap(v, size) (v) #define be_bswap(v, size) bswap ## size(v) #define le_bswaps(v, size) #define be_bswaps(p, size) *p = bswap ## size(*p); #endif #define CPU_CONVERT(endian, size, type)\ static inline type endian ## size ## _to_cpu(type v)\ {\ return endian ## _bswap(v, size);\ }\ \ static inline type cpu_to_ ## endian ## size(type v)\ {\ return endian ## _bswap(v, size);\ }\ \ static inline void endian ## size ## _to_cpus(type *p)\ {\ endian ## _bswaps(p, size)\ }\ \ static inline void cpu_to_ ## endian ## size ## s(type *p)\ {\ endian ## _bswaps(p, size)\ }\ \ static inline type endian ## size ## _to_cpup(const type *p)\ {\ return endian ## size ## _to_cpu(*p);\ }\ \ static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\ {\ *p = cpu_to_ ## endian ## size(v);\ } CPU_CONVERT(be, 16, uint16_t) CPU_CONVERT(be, 32, uint32_t) CPU_CONVERT(be, 64, uint64_t) CPU_CONVERT(le, 16, uint16_t) CPU_CONVERT(le, 32, uint32_t) CPU_CONVERT(le, 64, uint64_t) /* unaligned versions (optimized for frequent unaligned accesses)*/ #if defined(__i386__) || defined(__powerpc__) #define cpu_to_le16wu(p, v) cpu_to_le16w(p, v) #define cpu_to_le32wu(p, v) cpu_to_le32w(p, v) #define le16_to_cpupu(p) le16_to_cpup(p) #define le32_to_cpupu(p) le32_to_cpup(p) #define cpu_to_be16wu(p, v) cpu_to_be16w(p, v) #define cpu_to_be32wu(p, v) cpu_to_be32w(p, v) #else static inline void cpu_to_le16wu(uint16_t *p, uint16_t v) { uint8_t *p1 = (uint8_t *)p; p1[0] = v; p1[1] = v >> 8; } static inline void cpu_to_le32wu(uint32_t *p, uint32_t v) { uint8_t *p1 = (uint8_t *)p; p1[0] = v; p1[1] = v >> 8; p1[2] = v >> 16; p1[3] = v >> 24; } static inline uint16_t le16_to_cpupu(const uint16_t *p) { const uint8_t *p1 = (const uint8_t *)p; return p1[0] | (p1[1] << 8); } static inline uint32_t le32_to_cpupu(const uint32_t *p) { const uint8_t *p1 = (const uint8_t *)p; return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24); } static inline void cpu_to_be16wu(uint16_t *p, uint16_t v) { uint8_t *p1 = (uint8_t *)p; p1[0] = v >> 8; p1[1] = v; } static inline void cpu_to_be32wu(uint32_t *p, uint32_t v) { uint8_t *p1 = (uint8_t *)p; p1[0] = v >> 24; p1[1] = v >> 16; p1[2] = v >> 8; p1[3] = v; } #endif #ifdef WORDS_BIGENDIAN #define cpu_to_32wu cpu_to_be32wu #else #define cpu_to_32wu cpu_to_le32wu #endif #undef le_bswap #undef be_bswap #undef le_bswaps #undef be_bswaps #endif /* BSWAP_H */ a> 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/*
 * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
 * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
 *
 *	This program is free software; you can redistribute it and/or modify it
 *	under the terms of the GNU General Public License as published by the
 *	Free Software Foundation version 2 of the License.
 * 
 *	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, write to the Free Software Foundation, Inc.,
 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/types.h>

#define PATH_SIZE 512

static int verbose;
static int dry_run;

void log_message(int priority, const char *format, ...)
{
	va_list args;

	va_start(args, format);
	vsyslog(priority, format, args);
	va_end(args);
}

#undef err
#define err(format, arg...)                         \
    do {                                    \
        log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg);  \
    } while (0)

#undef info
#define info(format, arg...)                            \
    do {                                    \
        log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
    } while (0)

#ifdef DEBUG
#undef dbg
#define dbg(format, arg...)                         \
    do {                                    \
        log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg);    \
    } while (0)
#else
#define dbg(...) do {} while(0)
#endif


static void trigger_uevent(const char *devpath)
{
	char filename[PATH_SIZE];
	int fd;

	strlcpy(filename, "/sys", sizeof(filename));
	strlcat(filename, devpath, sizeof(filename));
	strlcat(filename, "/uevent", sizeof(filename));

	if (verbose)
		printf("%s\n", devpath);

	if (dry_run)
		return;

	fd = open(filename, O_WRONLY);
	if (fd < 0) {
		dbg("error on opening %s: %s\n", filename, strerror(errno));
		return;
	}

	if (write(fd, "add", 3) < 0)
		info("error on triggering %s: %s\n", filename, strerror(errno));

	close(fd);
}

static int sysfs_resolve_link(char *devpath, size_t size)
{
	char link_path[PATH_SIZE];
	char link_target[PATH_SIZE];
	int len;
	int i;
	int back;

	strlcpy(link_path, "/sys", sizeof(link_path));
	strlcat(link_path, devpath, sizeof(link_path));
	len = readlink(link_path, link_target, sizeof(link_target));
	if (len <= 0)
		return -1;
	link_target[len] = '\0';
	dbg("path link '%s' points to '%s'", devpath, link_target);

	for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
		;
	dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
	for (i = 0; i <= back; i++) {
		char *pos = strrchr(devpath, '/');

		if (pos == NULL)
			return -1;
		pos[0] = '\0';
	}
	dbg("after moving back '%s'", devpath);
	strlcat(devpath, "/", size);
	strlcat(devpath, &link_target[back * 3], size);
	return 0;
}


static int device_list_insert(const char *path)
{
	char filename[PATH_SIZE];
	char devpath[PATH_SIZE];
	struct stat statbuf;

	dbg("add '%s'" , path);

	/* we only have a device, if we have an uevent file */
	strlcpy(filename, path, sizeof(filename));
	strlcat(filename, "/uevent", sizeof(filename));
	if (stat(filename, &statbuf) < 0)
		return -1;
	if (!(statbuf.st_mode & S_IWUSR))
		return -1;

	strlcpy(devpath, &path[4], sizeof(devpath));

	/* resolve possible link to real target */
	if (lstat(path, &statbuf) < 0)
		return -1;
	if (S_ISLNK(statbuf.st_mode))
		if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
			return -1;

	trigger_uevent(devpath);
	return 0;
}


static void scan_subsystem(const char *subsys)
{
	char base[PATH_SIZE];
	DIR *dir;
	struct dirent *dent;

	strlcpy(base, "/sys/", sizeof(base));
	strlcat(base, subsys, sizeof(base));

	dir = opendir(base);
	if (dir != NULL) {
		for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
			char dirname[PATH_SIZE];
			DIR *dir2;
			struct dirent *dent2;

			if (dent->d_name[0] == '.')
				continue;

			strlcpy(dirname, base, sizeof(dirname));
			strlcat(dirname, "/", sizeof(dirname));
			strlcat(dirname, dent->d_name, sizeof(dirname));
			strlcat(dirname, "/devices", sizeof(dirname));

			/* look for devices */
			dir2 = opendir(dirname);
			if (dir2 != NULL) {
				for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
					char dirname2[PATH_SIZE];

					if (dent2->d_name[0] == '.')
						continue;

					strlcpy(dirname2, dirname, sizeof(dirname2));
					strlcat(dirname2, "/", sizeof(dirname2));
					strlcat(dirname2, dent2->d_name, sizeof(dirname2));
					device_list_insert(dirname2);
				}
				closedir(dir2);
			}
		}
		closedir(dir);
	}
}

static void scan_block(void)
{
	char base[PATH_SIZE];
	DIR *dir;
	struct dirent *dent;

	strlcpy(base, "/sys/block", sizeof(base));

	dir = opendir(base);
	if (dir != NULL) {
		for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
			char dirname[PATH_SIZE];
			DIR *dir2;
			struct dirent *dent2;

			if (dent->d_name[0] == '.')
				continue;

			strlcpy(dirname, base, sizeof(dirname));
			strlcat(dirname, "/", sizeof(dirname));
			strlcat(dirname, dent->d_name, sizeof(dirname));
			if (device_list_insert(dirname) != 0)
				continue;

			/* look for partitions */
			dir2 = opendir(dirname);
			if (dir2 != NULL) {
				for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
					char dirname2[PATH_SIZE];

					if (dent2->d_name[0] == '.')
						continue;

					if (!strcmp(dent2->d_name,"device"))
						continue;

					strlcpy(dirname2, dirname, sizeof(dirname2));
					strlcat(dirname2, "/", sizeof(dirname2));
					strlcat(dirname2, dent2->d_name, sizeof(dirname2));
					device_list_insert(dirname2);
				}
				closedir(dir2);
			}
		}
		closedir(dir);
	}
}

static void scan_class(void)
{
	char base[PATH_SIZE];
	DIR *dir;
	struct dirent *dent;

	strlcpy(base, "/sys/class", sizeof(base));

	dir = opendir(base);
	if (dir != NULL) {
		for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
			char dirname[PATH_SIZE];
			DIR *dir2;
			struct dirent *dent2;

			if (dent->d_name[0] == '.')
				continue;

			strlcpy(dirname, base, sizeof(dirname));
			strlcat(dirname, "/", sizeof(dirname));
			strlcat(dirname, dent->d_name, sizeof(dirname));
			dir2 = opendir(dirname);
			if (dir2 != NULL) {
				for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
					char dirname2[PATH_SIZE];

					if (dent2->d_name[0] == '.')
						continue;

					if (!strcmp(dent2->d_name, "device"))
						continue;

					strlcpy(dirname2, dirname, sizeof(dirname2));
					strlcat(dirname2, "/", sizeof(dirname2));
					strlcat(dirname2, dent2->d_name, sizeof(dirname2));
					device_list_insert(dirname2);
				}
				closedir(dir2);
			}
		}
		closedir(dir);
	}
}

int main(int argc, char *argv[], char *envp[])
{
	char base[PATH_SIZE];
	struct stat statbuf;
	int failed = 0;
	int option;

	openlog("udevtrigger", LOG_PID | LOG_CONS, LOG_DAEMON);

	while (1) {
		option = getopt(argc, argv, "vnh");
		if (option == -1)
			break;

		switch (option) {
		case 'v':
			verbose = 1;
			break;
		case 'n':
			dry_run = 1;
			break;
		case 'h':
			printf("Usage: udevtrigger OPTIONS\n"
			       "  -v                     print the list of devices while running\n"
			       "  -n                     do not actually trigger the events\n"
			       "  -h                     print this text\n"
			       "\n");
			goto exit;
		default:
			goto exit;
		}
	}


	/* if we have /sys/subsystem, forget all the old stuff */
	scan_subsystem("bus");
	scan_class();

	/* scan "block" if it isn't a "class" */
	strlcpy(base, "/sys/class/block", sizeof(base));
	if (stat(base, &statbuf) != 0)
		scan_block();

exit:

	closelog();
	return 0;
}