From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefanos Harhalakis Date: Sat, 09 Dec 2006 18:04:06 +0000 Subject: udev patch Message-Id: <200612092004.09587.v13@it.teithe.gr> MIME-Version: 1 Content-Type: multipart/mixed; boundary="===============0180211101==" List-Id: To: linux-hotplug@vger.kernel.org --===============0180211101== Content-Type: multipart/signed; boundary="nextPart1197224.5oMdZ8Wcxn"; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-Transfer-Encoding: 7bit --nextPart1197224.5oMdZ8Wcxn Content-Type: multipart/mixed; boundary="Boundary-01=_WqveFPtCdap57kz" Content-Transfer-Encoding: 7bit Content-Disposition: inline --Boundary-01=_WqveFPtCdap57kz Content-Type: text/plain; charset="iso-8859-7" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi there, Currently udev does not reread changed rule files when using symlinks.=20 Debian uses the symlink approach resulting to something like this: 020_permissions.rules -> ../permissions.rules 025_libgphoto2.rules -> ../libgphoto2.rules Because udev uses inotify on the rules directory it cannot detect changes= to=20 the destination file of a link. The attached patch solves this problem by=20 using inotify on the directory where each symlink destination resides. I've tried using inotify on the destination filenames but this doesn't wo= rk=20 as expected. For example vi will unlink and recreate the files. This means= =20 that the symlink will not point anywere for some time which is enough for=20 udev to loose the destination. After that the link needs to be rechecked=20 which is not possible (and efficient). Please consider the attached patch since it removes the need =20 of 'udevcontrol reload_rules' when working with symlinks.=20 If I have to set a license for the patch the it is GPLv2. Thanks in advance Harhalakis Stefanos p.s. CC replies p.s.2. Be gentle :-) --Boundary-01=_WqveFPtCdap57kz Content-Type: text/x-diff; charset="iso-8859-7"; name="patch-inotify_links.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patch-inotify_links.diff" diff -ru udev-103.orig/udevd.c udev-103/udevd.c =2D-- udev-103.orig/udevd.c 2006-10-20 15:43:35.000000000 +0300 +++ udev-103/udevd.c 2006-12-09 19:42:09.432522000 +0200 @@ -929,6 +929,100 @@ } } =20 +static void check_link(const char *filename) +{ + struct stat stats; + struct stat lstats; + char fn[PATH_SIZE+1]; + char fn2[PATH_SIZE*2+3]; + char *p; + ssize_t sz; + + if (lstat(filename, &lstats) !=3D 0) + { + err("could not read '%s': %s", filename, strerror(errno)); + return; + } + + /* Check if it is a link */ + if (!S_ISLNK(lstats.st_mode)) + return; + + /* Get the destination */ + dbg("found link: %s", filename); + sz=3Dreadlink(filename, fn, PATH_SIZE); + if (sz>=3DPATH_SIZE) + { + err("oversized link destination of '%s'", filename); + return; + } + + fn[sz]=3D0; + + /* Form the full destination path */ + if (fn[0]=3D=3D'/') + { + strcpy(fn2, fn); + } else + { + snprintf(fn2, PATH_SIZE*2+2, "%s/%s", udev_rules_filename, fn); + fn2[PATH_SIZE*2+2]=3D0; + } + + /* Check if destination if a file */ + stat(fn2, &stats); + if (!S_ISREG(stats.st_mode)) + return; + + /* Find the parent directory */ + p=3Dfn2+strlen(fn2); + + while (p>=3Dfn2) + { + if (*p=3D=3D'/') + { + *p=3D0; + break; + } + p--; + } + + if (p=3D=3Dfn2) + err("failed to get parrent directory of '%s'", fn2); + + dbg("adding notify for:'%s'", fn2); + inotify_add_watch(inotify_fd, fn2, IN_CREATE | IN_DELETE | IN_MOVE | IN_C= LOSE_WRITE); +} + +static void start_inotify(void) +{ + struct name_entry *name_loop, *name_tmp; + + /* watch rules directory */ + inotify_fd =3D inotify_init(); + if (inotify_fd >=3D 0) + { + inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELETE= | IN_MOVE | IN_CLOSE_WRITE); + } else if (errno =3D=3D ENOSYS) + { + err("the kernel does not support inotify, udevd can't monitor configurat= ion file changes"); + return; + } else + { + err("inotify_init failed: %s", strerror(errno)); + return; + } + + LIST_HEAD(name_list); + + add_matching_files(&name_list, udev_rules_filename, RULEFILE_SUFFIX); + list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) { + check_link(name_loop->name); + list_del(&name_loop->node); + free(name_loop); + } +} + int main(int argc, char *argv[], char *envp[]) { int retval; @@ -1101,14 +1195,7 @@ sigaction(SIGCHLD, &act, NULL); sigaction(SIGHUP, &act, NULL); =20 =2D /* watch rules directory */ =2D inotify_fd =3D inotify_init(); =2D if (inotify_fd >=3D 0) =2D inotify_add_watch(inotify_fd, udev_rules_filename, IN_CREATE | IN_DELE= TE | IN_MOVE | IN_CLOSE_WRITE); =2D else if (errno =3D=3D ENOSYS) =2D err("the kernel does not support inotify, udevd can't monitor configur= ation file changes"); =2D else =2D err("inotify_init failed: %s", strerror(errno)); + start_inotify(); =20 /* maximum limit of forked childs */ value =3D getenv("UDEVD_MAX_CHILDS"); @@ -1145,15 +1232,15 @@ if (debug_trace) putenv("DEBUG=3D1"); =20 =2D maxfd =3D udevd_sock; =2D maxfd =3D UDEV_MAX(maxfd, uevent_netlink_sock); =2D maxfd =3D UDEV_MAX(maxfd, signal_pipe[READ_END]); =2D maxfd =3D UDEV_MAX(maxfd, inotify_fd); =2D while (!udev_exit) { struct udevd_uevent_msg *msg; int fdcount; =20 + maxfd =3D udevd_sock; + maxfd =3D UDEV_MAX(maxfd, uevent_netlink_sock); + maxfd =3D UDEV_MAX(maxfd, signal_pipe[READ_END]); + maxfd =3D UDEV_MAX(maxfd, inotify_fd); + FD_ZERO(&readfds); FD_SET(signal_pipe[READ_END], &readfds); FD_SET(udevd_sock, &readfds); @@ -1209,6 +1296,12 @@ /* rules changed, set by inotify or a HUP signal */ if (reload_config) { reload_config =3D 0; + /* Restart inotify to handle possible file changes */ + if (inotify_fd>0) + { + close(inotify_fd); + start_inotify(); + } udev_rules_cleanup(&rules); udev_rules_init(&rules, 1); } --Boundary-01=_WqveFPtCdap57kz-- --nextPart1197224.5oMdZ8Wcxn Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iD8DBQBFevqZC3eCoT20km8RAqXvAJ9v7OGTdRHizhJzc7GcVfoRIXR1LgCeO2no zXcUoQN1UK+Mm5z67Hf/ke8= =S1nn -----END PGP SIGNATURE----- --nextPart1197224.5oMdZ8Wcxn-- --===============0180211101== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV --===============0180211101== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net Linux-hotplug-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel --===============0180211101==--