<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[926] branches/PR-8817822/darwintrace/darwintrace.c</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.macosforge.org/projects/darwinbuild/changeset/926">926</a></dd>
<dt>Author</dt> <dd>wsiegrist@apple.com</dd>
<dt>Date</dt> <dd>2011-02-08 11:30:55 -0800 (Tue, 08 Feb 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>More cleanup, remove __s</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branchesPR8817822darwintracedarwintracec">branches/PR-8817822/darwintrace/darwintrace.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branchesPR8817822darwintracedarwintracec"></a>
<div class="modfile"><h4>Modified: branches/PR-8817822/darwintrace/darwintrace.c (925 => 926)</h4>
<pre class="diff"><span>
<span class="info">--- branches/PR-8817822/darwintrace/darwintrace.c    2011-02-08 19:06:43 UTC (rev 925)
+++ branches/PR-8817822/darwintrace/darwintrace.c       2011-02-08 19:30:55 UTC (rev 926)
</span><span class="lines">@@ -46,8 +46,8 @@
</span><span class="cx"> 
</span><span class="cx"> #define DARWINTRACE_LOG_FULL_PATH 1
</span><span class="cx"> #define DARWINTRACE_DEBUG_OUTPUT 0
</span><del>-#define START_FD 81
-#define BUFFER_SIZE    1024
</del><ins>+#define DARWINTRACE_START_FD 81
+#define DARWINTRACE_BUFFER_SIZE        1024
</ins><span class="cx"> 
</span><span class="cx"> #if DARWINTRACE_DEBUG_OUTPUT
</span><span class="cx"> #define dprintf(...) fprintf(stderr, __VA_ARGS__)
</span><span class="lines">@@ -59,16 +59,16 @@
</span><span class="cx"> __attribute__((used)) static struct{ const void* replacement; const void* replacee; } _interpose_##_replacee \
</span><span class="cx"> __attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacement, (const void*)(unsigned long)&_replacee };
</span><span class="cx"> 
</span><del>-static int __darwintrace_fd = -2;
-static char __darwintrace_progname[BUFFER_SIZE];
-static pid_t __darwintrace_pid = -1;
</del><ins>+static int darwintrace_fd = -2;
+static char darwintrace_progname[DARWINTRACE_BUFFER_SIZE];
+static pid_t darwintrace_pid = -1;
</ins><span class="cx"> 
</span><span class="cx"> /**
</span><span class="cx">  * Redirect file access 
</span><span class="cx">  */
</span><del>-static char *__darwintrace_redirect = NULL; 
-static char *__darwintrace_buildroot = NULL;
-static const char *__redirect_exceptions[] = {
</del><ins>+static char *darwintrace_redirect = NULL; 
+static char *darwintrace_buildroot = NULL;
+static const char *darwintrace_exceptions[] = {
</ins><span class="cx">   "/Developer/Library/Private",
</span><span class="cx">   "/Developer/Library/Frameworks",
</span><span class="cx">   "/Developer/usr/bin/../../Library/Private",
</span><span class="lines">@@ -90,10 +90,11 @@
</span><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> /* check if str starts with one of the exceptions */
</span><del>-static inline bool __except(const char *str) {
-  size_t i, __exception_count = sizeof(__redirect_exceptions)/sizeof(*__redirect_exceptions); 
-  for (i = 0; i < __exception_count; i++) {                         
-    if (strncmp(__redirect_exceptions[i], str, strlen(__redirect_exceptions[i])) == 0) { 
</del><ins>+static inline bool darwintrace_except(const char *str) {
+  size_t c = sizeof(darwintrace_exceptions)/sizeof(*darwintrace_exceptions); 
+  size_t i;
+  for (i = 0; i < c; i++) {                         
+    if (strncmp(darwintrace_exceptions[i], str, strlen(darwintrace_exceptions[i])) == 0) { 
</ins><span class="cx">       return true;
</span><span class="cx">     }
</span><span class="cx">   }
</span><span class="lines">@@ -101,81 +102,77 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* apply redirection heuristic to path */
</span><del>-static inline char* __redirect_path(const char* path) {        
</del><ins>+static inline char* darwintrace_redirect_path(const char* path) {       
</ins><span class="cx">   char *redirpath;
</span><span class="cx">   redirpath = (char *)path;
</span><del>-  if (__darwintrace_redirect
</del><ins>+  if (darwintrace_redirect
</ins><span class="cx">       && path[0] == '/'
</span><del>-      && !__except(path)
-      && strncmp(__darwintrace_buildroot, path, strlen(__darwintrace_buildroot))!=0
-      && strncmp(__darwintrace_redirect, path, strlen(__darwintrace_redirect))!=0 ) {
-    asprintf(&redirpath, "%s%s%s", __darwintrace_redirect, (*path == '/' ? "" : "/"), path);
</del><ins>+      && !darwintrace_except(path)
+      && strncmp(darwintrace_buildroot, path, strlen(darwintrace_buildroot))!=0
+      && strncmp(darwintrace_redirect, path, strlen(darwintrace_redirect))!=0 ) {
+    asprintf(&redirpath, "%s%s%s", darwintrace_redirect, (*path == '/' ? "" : "/"), path);
</ins><span class="cx">     dprintf("darwintrace: redirect %s -> %s\n", path, redirpath);
</span><span class="cx">   }
</span><span class="cx">   return redirpath;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* free path if not the same as test */
</span><del>-static inline void __free_path(char* path, const char* test) {
</del><ins>+static inline void darwintrace_free_path(char* path, const char* test) {
</ins><span class="cx">   if (path != test) free(path);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-static inline void __darwintrace_setup() {
-       if (__darwintrace_fd == -2) {
</del><ins>+static inline void darwintrace_setup() {
+       if (darwintrace_fd == -2) {
</ins><span class="cx">     char* path = getenv("DARWINTRACE_LOG");
</span><span class="cx">    if (path != NULL) {
</span><del>-               int olderrno = errno;
-               int fd = open(path,
-                             O_CREAT | O_WRONLY | O_APPEND,
-                             DEFFILEMODE);
-               int newfd;
-               for(newfd = START_FD; newfd < START_FD + 21; newfd++) {
-                 if(-1 == write(newfd, "", 0) && errno == EBADF) {
-                   if(-1 != dup2(fd, newfd))
-                     __darwintrace_fd = newfd;
-                   close(fd);
-                   fcntl(__darwintrace_fd, F_SETFD, 1); /* close-on-exec */
-                   break;
-                 }
-               }
-               errno = olderrno;
</del><ins>+      int olderrno = errno;
+      int fd = open(path,
+                    O_CREAT | O_WRONLY | O_APPEND,
+                    DEFFILEMODE);
+      int newfd;
+      for(newfd = DARWINTRACE_START_FD; newfd < DARWINTRACE_START_FD + 21; newfd++) {
+        if(-1 == write(newfd, "", 0) && errno == EBADF) {
+          if(-1 != dup2(fd, newfd))
+            darwintrace_fd = newfd;
+          close(fd);
+          fcntl(darwintrace_fd, F_SETFD, 1); /* close-on-exec */
+          break;
+        }
+      }
+      errno = olderrno;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">    /* read env vars needed for redirection */
</span><del>-         __darwintrace_redirect = getenv("DARWINTRACE_REDIRECT");
-         __darwintrace_buildroot = getenv("DARWIN_BUILDROOT");
</del><ins>+          darwintrace_redirect = getenv("DARWINTRACE_REDIRECT");
+         darwintrace_buildroot = getenv("DARWIN_BUILDROOT");
</ins><span class="cx">   }
</span><span class="cx"> 
</span><del>-       if (__darwintrace_pid == -1) {
-               __darwintrace_pid = getpid();
</del><ins>+        if (darwintrace_pid == -1) {
+               darwintrace_pid = getpid();
</ins><span class="cx">           char** progname = _NSGetProgname();
</span><span class="cx">          if (progname && *progname) {
</span><del>-                 if (strlcpy(__darwintrace_progname, *progname, sizeof(__darwintrace_progname)) 
-                     >= sizeof(__darwintrace_progname)) {
</del><ins>+                  if (strlcpy(darwintrace_progname, *progname, sizeof(darwintrace_progname)) >= sizeof(darwintrace_progname)) {
</ins><span class="cx">               dprintf("darwintrace: progname too long to copy: %s\n", *progname);
</span><span class="cx">            }
</span><span class="cx">          }
</span><span class="cx">  }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-/* __darwintrace_setup must have been called already */
-static inline void __darwintrace_logpath(int fd, const char *procname, char *tag, const char *path) {
-  char __darwintrace_buf[BUFFER_SIZE];
-  int size;
-
-  size = snprintf(__darwintrace_buf, sizeof(__darwintrace_buf),
-                  "%s[%d]\t%s\t%s\n",
-                  procname ? procname : __darwintrace_progname, __darwintrace_pid,
-                  tag, path );
-  
-  write(fd, __darwintrace_buf, size);
</del><ins>+/* darwintrace_setup must have been called already */
+static inline void darwintrace_logpath(int fd, const char *procname, char *tag, const char *path) {
+  char darwintrace_buf[DARWINTRACE_BUFFER_SIZE];
+  int size = snprintf(darwintrace_buf, sizeof(darwintrace_buf),
+                      "%s[%d]\t%s\t%s\n",
+                      procname ? procname : darwintrace_progname, darwintrace_pid,
+                      tag, path);
+  write(fd, darwintrace_buf, size);
</ins><span class="cx">   fsync(fd);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /* remap resource fork access to the data fork.
</span><span class="cx">  * do a partial realpath(3) to fix "foo//bar" to "foo/bar"
</span><span class="cx">  */
</span><del>-static inline void __darwintrace_cleanup_path(char *path) {
</del><ins>+static inline void darwintrace_cleanup_path(char *path) {
</ins><span class="cx">   size_t pathlen, rsrclen;
</span><span class="cx">   size_t i, shiftamount;
</span><span class="cx">   enum { SAWSLASH, NOTHING } state = NOTHING;
</span><span class="lines">@@ -226,15 +223,15 @@
</span><span class="cx">  int result;
</span><span class="cx">  va_list args;
</span><span class="cx"> 
</span><del>-       char* redirpath = __redirect_path(path);
</del><ins>+        char* redirpath = darwintrace_redirect_path(path);
</ins><span class="cx"> 
</span><span class="cx">  va_start(args, flags);
</span><span class="cx">  mode = va_arg(args, int);
</span><span class="cx">  va_end(args);
</span><span class="cx">  result = open(redirpath, flags, mode);
</span><span class="cx">  if (result >= 0 && (flags & (O_CREAT | O_WRONLY /*O_RDWR*/)) == 0 ) {
</span><del>-         __darwintrace_setup();
-         if (__darwintrace_fd >= 0) {
</del><ins>+          darwintrace_setup();
+         if (darwintrace_fd >= 0) {
</ins><span class="cx">       char realpath[MAXPATHLEN];
</span><span class="cx"> #if DARWINTRACE_LOG_FULL_PATH
</span><span class="cx">      int usegetpath = 1;
</span><span class="lines">@@ -265,12 +262,12 @@
</span><span class="cx">         }
</span><span class="cx">       }
</span><span class="cx"> 
</span><del>-           __darwintrace_cleanup_path(realpath);
-           __darwintrace_logpath(__darwintrace_fd, NULL, "open", realpath);
</del><ins>+            darwintrace_cleanup_path(realpath);
+           darwintrace_logpath(darwintrace_fd, NULL, "open", realpath);
</ins><span class="cx">     }
</span><span class="cx">  }
</span><span class="cx"> 
</span><del>-       __free_path(redirpath, path);
</del><ins>+        darwintrace_free_path(redirpath, path);
</ins><span class="cx">   return result;
</span><span class="cx"> }
</span><span class="cx"> DARWINTRACE_INTERPOSE(darwintrace_open, open)
</span><span class="lines">@@ -282,11 +279,11 @@
</span><span class="cx"> ssize_t darwintrace_readlink(const char * path, char * buf, size_t bufsiz) {
</span><span class="cx">  ssize_t result;
</span><span class="cx"> 
</span><del>-       char* redirpath = __redirect_path(path);
</del><ins>+        char* redirpath = darwintrace_redirect_path(path);
</ins><span class="cx">   result = readlink(redirpath, buf, bufsiz);
</span><span class="cx">  if (result >= 0) {
</span><del>-         __darwintrace_setup();
-         if (__darwintrace_fd >= 0) {
</del><ins>+          darwintrace_setup();
+         if (darwintrace_fd >= 0) {
</ins><span class="cx">       char realpath[MAXPATHLEN];
</span><span class="cx"> 
</span><span class="cx">      dprintf("darwintrace: original readlink path is %s\n", redirpath);
</span><span class="lines">@@ -295,12 +292,12 @@
</span><span class="cx">        dprintf("darwintrace: in readlink: path too long to copy: %s\n", redirpath);
</span><span class="cx">      }
</span><span class="cx">      
</span><del>-           __darwintrace_cleanup_path(realpath);
-           __darwintrace_logpath(__darwintrace_fd, NULL, "readlink", realpath);
</del><ins>+            darwintrace_cleanup_path(realpath);
+           darwintrace_logpath(darwintrace_fd, NULL, "readlink", realpath);
</ins><span class="cx">     }
</span><span class="cx">  }
</span><span class="cx">   
</span><del>-       __free_path(redirpath, path);
</del><ins>+        darwintrace_free_path(redirpath, path);
</ins><span class="cx">   return result;
</span><span class="cx"> }
</span><span class="cx"> DARWINTRACE_INTERPOSE(darwintrace_readlink, readlink)
</span><span class="lines">@@ -309,9 +306,9 @@
</span><span class="cx"> int darwintrace_execve(const char* path, char* const argv[], char* const envp[]) {
</span><span class="cx">  int result;
</span><span class="cx">  
</span><del>-       char* redirpath = __redirect_path(path);
-       __darwintrace_setup();
-       if (__darwintrace_fd >= 0) {
</del><ins>+        char* redirpath = darwintrace_redirect_path(path);
+       darwintrace_setup();
+       if (darwintrace_fd >= 0) {
</ins><span class="cx">     struct stat sb;
</span><span class="cx">    char realpath[MAXPATHLEN];
</span><span class="cx">    int printorig = 0;
</span><span class="lines">@@ -350,8 +347,8 @@
</span><span class="cx">           dprintf("darwintrace: in execve: path too long to copy: %s\n", redirpath);
</span><span class="cx">        }
</span><span class="cx"> 
</span><del>-             __darwintrace_cleanup_path(realpath);
-             __darwintrace_logpath(__darwintrace_fd, NULL, "execve", realpath);
</del><ins>+              darwintrace_cleanup_path(realpath);
+             darwintrace_logpath(darwintrace_fd, NULL, "execve", realpath);
</ins><span class="cx">       }
</span><span class="cx">          
</span><span class="cx">      fd = open(redirpath, O_RDONLY, 0);
</span><span class="lines">@@ -378,8 +375,8 @@
</span><span class="cx">             }
</span><span class="cx">           }
</span><span class="cx">           
</span><del>-          __darwintrace_cleanup_path(realpath);
-          __darwintrace_logpath(__darwintrace_fd, NULL, "execve", realpath);
</del><ins>+          darwintrace_cleanup_path(realpath);
+          darwintrace_logpath(darwintrace_fd, NULL, "execve", realpath);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">        bzero(buffer, sizeof(buffer));
</span><span class="lines">@@ -418,8 +415,8 @@
</span><span class="cx">               procname++;
</span><span class="cx">             }
</span><span class="cx"> 
</span><del>-            __darwintrace_cleanup_path(interp);
-            __darwintrace_logpath(__darwintrace_fd, procname, "execve", interp);
</del><ins>+            darwintrace_cleanup_path(interp);
+            darwintrace_logpath(darwintrace_fd, procname, "execve", interp);
</ins><span class="cx">           }
</span><span class="cx">        }
</span><span class="cx">         
</span><span class="lines">@@ -429,7 +426,7 @@
</span><span class="cx">  }
</span><span class="cx">   
</span><span class="cx">  result = execve(redirpath, argv, envp);
</span><del>-       __free_path(redirpath, path);
</del><ins>+        darwintrace_free_path(redirpath, path);
</ins><span class="cx">   return result;
</span><span class="cx"> }
</span><span class="cx"> DARWINTRACE_INTERPOSE(darwintrace_execve, execve)
</span><span class="lines">@@ -440,7 +437,7 @@
</span><span class="cx">    attempts to close our file descriptor
</span><span class="cx"> */
</span><span class="cx"> int darwintrace_close(int fd) {
</span><del>-  if(__darwintrace_fd != -2 && fd == __darwintrace_fd) {
</del><ins>+  if(darwintrace_fd != -2 && fd == darwintrace_fd) {
</ins><span class="cx">     errno = EBADF;
</span><span class="cx">     return -1;
</span><span class="cx">   }
</span></span></pre>
</div>
</div>

</body>
</html>