[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Comments on the psmisc patch?



Hi,

patch attached for review. This is a little messy, I admit that. And
there are open issues.

Another question:

What is the purpose of:
...
  * Somehow the realpath(3) glibc function call, nevertheless
  * it avoids lstat(2) system calls.
  */
 static char real[PATH_MAX+1];

 char *curr;
...
 curr = &real[0];
...

The changes to pstree.c will follow tomorrow.

Thanks!
diff -ur psmisc-22.14/src/fuser.c psmisc-22.14.modified/src/fuser.c
--- psmisc-22.14/src/fuser.c	2011-06-20 13:41:13.000000000 +0200
+++ psmisc-22.14.modified/src/fuser.c	2011-09-15 00:37:39.000000000 +0200
@@ -1951,23 +1951,36 @@
  * Somehow the realpath(3) glibc function call, nevertheless
  * it avoids lstat(2) system calls.
  */
+#ifndef __GNU__
 static char real[PATH_MAX+1];
+#endif
 char* expandpath(const char * path)
 {
+#ifndef __GNU__
 	char tmpbuf[PATH_MAX+1];
+#else
+	char *tmpbuf;
+#endif
 	const char *start, *end;
-	char *curr, *dest;
+	char *curr=NULL, *dest;
 	int deep = MAXSYMLINKS;
 
 	if (!path || *path == '\0')
 		return (char*)0;
 
+#ifndef __GNU__
 	curr = &real[0];
 
 	if (*path != '/') {
 		if (!getcwd(curr, PATH_MAX))
 			return (char*)0;
 		dest = rawmemchr(curr, '\0');
+#else
+	if (*path != '/') {
+		if (!(curr = get_current_dir_name()))
+			return (char*)0;
+		dest = rawmemchr(curr, '\0');
+#endif
 	} else {
 		*curr = '/';
 		dest = curr + 1;
@@ -1990,18 +2003,25 @@
 				while ((--dest)[-1] != '/')
 					;
 		} else {
+#ifndef __GNU__
 			char lnkbuf[PATH_MAX+1];
+#else
+			char *lnkbuf;
+#endif
 			size_t len;
 			ssize_t n;
 
 			if (dest[-1] != '/')
 				*dest++ = '/';
 
+#ifndef __GNU__
 			if (dest + (end - start) > curr + PATH_MAX) {
 				errno = ENAMETOOLONG;
 				return (char*)0;
 			}
-
+#else
+			/* Not sure what to enere here */
+#endif
 			dest = mempcpy(dest, start, end - start);
 			*dest = '\0';
 
@@ -2011,6 +2031,7 @@
 			}
 
 			errno = 0;
+#ifndef __GNU__
 			if ((n = readlink(curr, lnkbuf, PATH_MAX)) < 0) {
 				deep = MAXSYMLINKS;
 				if (errno == EINVAL)
@@ -2025,6 +2046,18 @@
 				return (char*)0;
 			}
 
+#else
+			lnkbuf = canonicalize_file_name(curr);
+			if (!lnkbuf) {
+				deep = MAXSYMLINKS;
+				if (errno == EINVAL)
+					continue;	/* Not a symlink */
+				return (char*)0;
+			}
+			len = strlen(end);
+			n = strlen(lnkbuf) - 1;
+			tmpbuf = malloc(len + 1 + 1);
+#endif
 			memmove(&tmpbuf[n], end, len + 1);
 			path = end = memcpy(tmpbuf, lnkbuf, n);
 
@@ -2034,6 +2067,10 @@
 			    while ((--dest)[-1] != '/');
 			
 		}
+#ifdef __GNU__
+	free(lnkbuf);
+	free(tmpbuf);
+#endif
 	}
 
 	if (dest > curr + 1 && dest[-1] == '/')
diff -ur psmisc-22.14/src/fuser.h psmisc-22.14.modified/src/fuser.h
--- psmisc-22.14/src/fuser.h	2011-02-09 23:53:26.000000000 +0100
+++ psmisc-22.14.modified/src/fuser.h	2011-09-14 23:46:49.000000000 +0200
@@ -86,7 +86,7 @@
 	struct mount_list *next;
 };
 
-#if defined (__GNUC__) && defined(__OPTIMIZE__)
+#if defined (__GNUC__) && defined(__OPTIMIZE__) && defined(__linux__)
 # include "lists.h"
 typedef struct mntinfo_s {
     list_t   this;
diff -ur psmisc-22.14/src/pstree.c psmisc-22.14.modified/src/pstree.c
--- psmisc-22.14/src/pstree.c	2011-02-22 09:56:11.000000000 +0100
+++ psmisc-22.14.modified/src/pstree.c	2011-09-14 23:09:09.000000000 +0200
@@ -49,6 +49,10 @@
 #include <selinux/selinux.h>
 #endif                                /*WITH_SELINUX */
 
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
 extern const char *__progname;
 
 #define PROC_BASE    "/proc"

Reply to: