<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[22999] trunk/launchd/src/launchd_core_logic.c</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.macosforge.org/projects/launchd/changeset/22999">22999</a></dd>
<dt>Author</dt> <dd>zarzycki@apple.com</dd>
<dt>Date</dt> <dd>2007-01-02 14:35:13 -0800 (Tue, 02 Jan 2007)</dd>
</dl>

<h3>Log Message</h3>
<pre>&lt;rdar://problem/4696825&gt; reflect stderr of launchd jobs to ASL (&quot;console logging&quot;)</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunklaunchdsrclaunchd_core_logicc">trunk/launchd/src/launchd_core_logic.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunklaunchdsrclaunchd_core_logicc"></a>
<div class="modfile"><h4>Modified: trunk/launchd/src/launchd_core_logic.c (22998 => 22999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/launchd/src/launchd_core_logic.c        2007-01-02 18:32:46 UTC (rev 22998)
+++ trunk/launchd/src/launchd_core_logic.c        2007-01-02 22:35:13 UTC (rev 22999)
</span><span class="lines">@@ -257,6 +257,7 @@
</span><span class="cx">         int argc;
</span><span class="cx">         int last_exit_status;
</span><span class="cx">         int forkfd;
</span><ins>+        int log_redirect_fd;
</ins><span class="cx">         int nice;
</span><span class="cx">         int timeout;
</span><span class="cx">         int stdout_err_fd;
</span><span class="lines">@@ -609,6 +610,10 @@
</span><span class="cx">                 job_assumes(j, close(j-&gt;forkfd) != -1);
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        if (j-&gt;log_redirect_fd) {
+                job_assumes(j, close(j-&gt;log_redirect_fd) != -1);
+        }
+
</ins><span class="cx">         if (j-&gt;j_port) {
</span><span class="cx">                 job_assumes(j, launchd_mport_close_recv(j-&gt;j_port) == KERN_SUCCESS);
</span><span class="cx">         }
</span><span class="lines">@@ -1586,6 +1591,11 @@
</span><span class="cx"> 
</span><span class="cx">         job_log(j, LOG_DEBUG, &quot;Reaping&quot;);
</span><span class="cx"> 
</span><ins>+        if (j-&gt;log_redirect_fd) {
+                job_assumes(j, close(j-&gt;log_redirect_fd) != -1);
+                j-&gt;log_redirect_fd = 0;
+        }
+
</ins><span class="cx">         if (j-&gt;forkfd) {
</span><span class="cx">                 job_assumes(j, close(j-&gt;forkfd) != -1);
</span><span class="cx">                 j-&gt;forkfd = 0;
</span><span class="lines">@@ -1705,7 +1715,30 @@
</span><span class="cx">                 watchpath_callback(j, kev);
</span><span class="cx">                 break;
</span><span class="cx">         case EVFILT_READ:
</span><del>-                socketgroup_callback(j, kev);
</del><ins>+                if (kev-&gt;ident == (uintptr_t)j-&gt;log_redirect_fd) {
+                        char buf[4001];
+                        ssize_t rsz;
+
+                        rsz = read(j-&gt;log_redirect_fd, buf, sizeof(buf) - 1);
+
+                        if (rsz == 0) {
+                                job_assumes(j, close(j-&gt;log_redirect_fd) != -1);
+                                j-&gt;log_redirect_fd = 0;
+                        } else if (job_assumes(j, rsz != -1)) {
+                                buf[rsz] = '\0';
+                                switch (buf[0]) {
+                                case '\n':
+                                case '\r':
+                                case '\0':
+                                        break;
+                                default:
+                                        job_log(j, LOG_NOTICE, &quot;Standard Out/Error: %s&quot;, buf);
+                                        break;
+                                }
+                        }
+                } else {
+                        socketgroup_callback(j, kev);
+                }
</ins><span class="cx">                 break;
</span><span class="cx">         case EVFILT_MACHPORT:
</span><span class="cx">                 job_dispatch(j, true);
</span><span class="lines">@@ -1721,6 +1754,7 @@
</span><span class="cx"> {
</span><span class="cx">         int spair[2];
</span><span class="cx">         int execspair[2];
</span><ins>+        int oepair[2];
</ins><span class="cx">         char nbuf[64];
</span><span class="cx">         pid_t c;
</span><span class="cx">         bool sipc = false;
</span><span class="lines">@@ -1759,11 +1793,17 @@
</span><span class="cx">         j-&gt;checkedin = false;
</span><span class="cx"> 
</span><span class="cx">         if (sipc) {
</span><del>-                socketpair(AF_UNIX, SOCK_STREAM, 0, spair);
</del><ins>+                job_assumes(j, socketpair(AF_UNIX, SOCK_STREAM, 0, spair) != -1);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        socketpair(AF_UNIX, SOCK_STREAM, 0, execspair);
</del><ins>+        job_assumes(j, socketpair(AF_UNIX, SOCK_STREAM, 0, execspair) != -1);
</ins><span class="cx"> 
</span><ins>+        if (job_assumes(j, pipe(oepair) != -1)) {
+                j-&gt;log_redirect_fd = _fd(oepair[0]);
+                job_assumes(j, fcntl(j-&gt;log_redirect_fd, F_SETFL, O_NONBLOCK) != -1);
+                job_assumes(j, kevent_mod(j-&gt;log_redirect_fd, EVFILT_READ, EV_ADD, 0, 0, j) != -1);
+        }
+
</ins><span class="cx">         time(&amp;j-&gt;start_time);
</span><span class="cx"> 
</span><span class="cx">         switch (c = jobmgr_fork(j-&gt;mgr)) {
</span><span class="lines">@@ -1777,6 +1817,9 @@
</span><span class="cx">                 }
</span><span class="cx">                 break;
</span><span class="cx">         case 0:
</span><ins>+                job_assumes(j, dup2(oepair[1], STDOUT_FILENO) != -1);
+                job_assumes(j, dup2(oepair[1], STDERR_FILENO) != -1);
+                job_assumes(j, close(oepair[1]) != -1);
</ins><span class="cx">                 job_assumes(j, close(execspair[0]) == 0);
</span><span class="cx">                 /* wait for our parent to say they've attached a kevent to us */
</span><span class="cx">                 read(_fd(execspair[1]), &amp;c, sizeof(c));
</span><span class="lines">@@ -1797,6 +1840,7 @@
</span><span class="cx">                 job_start_child(j);
</span><span class="cx">                 break;
</span><span class="cx">         default:
</span><ins>+                job_assumes(j, close(oepair[1]) != -1);
</ins><span class="cx">                 j-&gt;p = c;
</span><span class="cx">                 j-&gt;forkfd = _fd(execspair[0]);
</span><span class="cx">                 job_assumes(j, close(execspair[1]) == 0);
</span><span class="lines">@@ -2544,8 +2588,9 @@
</span><span class="cx"> 
</span><span class="cx">         job_log(j, LOG_DEBUG, &quot;Ignoring Sockets:%s&quot;, buf);
</span><span class="cx"> 
</span><del>-        for (i = 0; i &lt; sg-&gt;fd_cnt; i++)
</del><ins>+        for (i = 0; i &lt; sg-&gt;fd_cnt; i++) {
</ins><span class="cx">                 job_assumes(j, kevent_mod(sg-&gt;fds[i], EVFILT_READ, EV_DELETE, 0, 0, NULL) != -1);
</span><ins>+        }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void
</span><span class="lines">@@ -2563,8 +2608,9 @@
</span><span class="cx"> 
</span><span class="cx">         job_log(j, LOG_DEBUG, &quot;Watching sockets:%s&quot;, buf);
</span><span class="cx"> 
</span><del>-        for (i = 0; i &lt; sg-&gt;fd_cnt; i++)
</del><ins>+        for (i = 0; i &lt; sg-&gt;fd_cnt; i++) {
</ins><span class="cx">                 job_assumes(j, kevent_mod(sg-&gt;fds[i], EVFILT_READ, EV_ADD, 0, 0, j) != -1);
</span><ins>+        }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void
</span></span></pre>
</div>
</div>

</body>
</html>