2 * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <sys/types.h>
37 #define strlcpy(d,s,l) (strncpy(d,s,l), (d)[(l)-1] = '\0')
41 #define strlcat(d,s,l) strncat(d,s,(l)-strlen(d)-1)
47 static void log_message(int priority
, const char *format
, ...)
51 va_start(args
, format
);
52 vsyslog(priority
, format
, args
);
57 #define err(format, arg...) \
59 log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \
63 #define info(format, arg...) \
65 log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
70 #define dbg(format, arg...) \
72 log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg); \
75 #define dbg(...) do {} while(0)
79 static void trigger_uevent(const char *devpath
)
81 char filename
[PATH_SIZE
];
84 strlcpy(filename
, "/sys", sizeof(filename
));
85 strlcat(filename
, devpath
, sizeof(filename
));
86 strlcat(filename
, "/uevent", sizeof(filename
));
89 printf("%s\n", devpath
);
94 fd
= open(filename
, O_WRONLY
);
96 dbg("error on opening %s: %s\n", filename
, strerror(errno
));
100 if (write(fd
, "add", 3) < 0)
101 info("error on triggering %s: %s\n", filename
, strerror(errno
));
106 static int sysfs_resolve_link(char *devpath
, size_t size
)
108 char link_path
[PATH_SIZE
];
109 char link_target
[PATH_SIZE
];
114 strlcpy(link_path
, "/sys", sizeof(link_path
));
115 strlcat(link_path
, devpath
, sizeof(link_path
));
116 len
= readlink(link_path
, link_target
, sizeof(link_target
));
119 link_target
[len
] = '\0';
120 dbg("path link '%s' points to '%s'", devpath
, link_target
);
122 for (back
= 0; strncmp(&link_target
[back
* 3], "../", 3) == 0; back
++)
124 dbg("base '%s', tail '%s', back %i", devpath
, &link_target
[back
* 3], back
);
125 for (i
= 0; i
<= back
; i
++) {
126 char *pos
= strrchr(devpath
, '/');
132 dbg("after moving back '%s'", devpath
);
133 strlcat(devpath
, "/", size
);
134 strlcat(devpath
, &link_target
[back
* 3], size
);
139 static int device_list_insert(const char *path
)
141 char filename
[PATH_SIZE
];
142 char devpath
[PATH_SIZE
];
145 dbg("add '%s'" , path
);
147 /* we only have a device, if we have an uevent file */
148 strlcpy(filename
, path
, sizeof(filename
));
149 strlcat(filename
, "/uevent", sizeof(filename
));
150 if (stat(filename
, &statbuf
) < 0)
152 if (!(statbuf
.st_mode
& S_IWUSR
))
155 strlcpy(devpath
, &path
[4], sizeof(devpath
));
157 /* resolve possible link to real target */
158 if (lstat(path
, &statbuf
) < 0)
160 if (S_ISLNK(statbuf
.st_mode
))
161 if (sysfs_resolve_link(devpath
, sizeof(devpath
)) != 0)
164 trigger_uevent(devpath
);
169 static void scan_subsystem(const char *subsys
)
171 char base
[PATH_SIZE
];
175 strlcpy(base
, "/sys/", sizeof(base
));
176 strlcat(base
, subsys
, sizeof(base
));
180 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
181 char dirname
[PATH_SIZE
];
183 struct dirent
*dent2
;
185 if (dent
->d_name
[0] == '.')
188 strlcpy(dirname
, base
, sizeof(dirname
));
189 strlcat(dirname
, "/", sizeof(dirname
));
190 strlcat(dirname
, dent
->d_name
, sizeof(dirname
));
191 strlcat(dirname
, "/devices", sizeof(dirname
));
193 /* look for devices */
194 dir2
= opendir(dirname
);
196 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
197 char dirname2
[PATH_SIZE
];
199 if (dent2
->d_name
[0] == '.')
202 strlcpy(dirname2
, dirname
, sizeof(dirname2
));
203 strlcat(dirname2
, "/", sizeof(dirname2
));
204 strlcat(dirname2
, dent2
->d_name
, sizeof(dirname2
));
205 device_list_insert(dirname2
);
214 static void scan_block(void)
216 char base
[PATH_SIZE
];
220 strlcpy(base
, "/sys/block", sizeof(base
));
224 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
225 char dirname
[PATH_SIZE
];
227 struct dirent
*dent2
;
229 if (dent
->d_name
[0] == '.')
232 strlcpy(dirname
, base
, sizeof(dirname
));
233 strlcat(dirname
, "/", sizeof(dirname
));
234 strlcat(dirname
, dent
->d_name
, sizeof(dirname
));
235 if (device_list_insert(dirname
) != 0)
238 /* look for partitions */
239 dir2
= opendir(dirname
);
241 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
242 char dirname2
[PATH_SIZE
];
244 if (dent2
->d_name
[0] == '.')
247 if (!strcmp(dent2
->d_name
,"device"))
250 strlcpy(dirname2
, dirname
, sizeof(dirname2
));
251 strlcat(dirname2
, "/", sizeof(dirname2
));
252 strlcat(dirname2
, dent2
->d_name
, sizeof(dirname2
));
253 device_list_insert(dirname2
);
262 static void scan_class(void)
264 char base
[PATH_SIZE
];
268 strlcpy(base
, "/sys/class", sizeof(base
));
272 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
273 char dirname
[PATH_SIZE
];
275 struct dirent
*dent2
;
277 if (dent
->d_name
[0] == '.')
280 strlcpy(dirname
, base
, sizeof(dirname
));
281 strlcat(dirname
, "/", sizeof(dirname
));
282 strlcat(dirname
, dent
->d_name
, sizeof(dirname
));
283 dir2
= opendir(dirname
);
285 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
286 char dirname2
[PATH_SIZE
];
288 if (dent2
->d_name
[0] == '.')
291 if (!strcmp(dent2
->d_name
, "device"))
294 strlcpy(dirname2
, dirname
, sizeof(dirname2
));
295 strlcat(dirname2
, "/", sizeof(dirname2
));
296 strlcat(dirname2
, dent2
->d_name
, sizeof(dirname2
));
297 device_list_insert(dirname2
);
306 int main(int argc
, char *argv
[], char *envp
[])
308 char base
[PATH_SIZE
];
312 openlog("udevtrigger", LOG_PID
| LOG_CONS
, LOG_DAEMON
);
315 option
= getopt(argc
, argv
, "vnh");
327 printf("Usage: udevtrigger OPTIONS\n"
328 " -v print the list of devices while running\n"
329 " -n do not actually trigger the events\n"
330 " -h print this text\n"
339 /* if we have /sys/subsystem, forget all the old stuff */
340 scan_subsystem("bus");
343 /* scan "block" if it isn't a "class" */
344 strlcpy(base
, "/sys/class/block", sizeof(base
));
345 if (stat(base
, &statbuf
) != 0)