Loading...
gen/_simple.c Libc-498 Libc-763.11
--- Libc/Libc-498/gen/_simple.c
+++ Libc/Libc-763.11/gen/_simple.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2006 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2005, 2006, 2009 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
@@ -23,21 +23,30 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#ifndef VARIANT_DYLD
 #include <setjmp.h>
+#endif /* !VARIANT_DYLD */
 #include <sys/types.h>
 #include <unistd.h>
 #include <mach/mach_init.h>
 #include <mach/vm_map.h>
 #include <asl.h>
+#include <fcntl.h>
+#include <sys/syslog.h>
 #include <sys/time.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <pthread.h>
 #include <errno.h>
+#include <servers/bootstrap.h>
+#include <asl_ipc.h>
 
 #include "_simple.h"
 
+#ifndef VM_PAGE_SIZE
 #define VM_PAGE_SIZE	4096
+#endif
+
 #define SBUF_SIZE(s)	(((SBUF *)(s))->b.end - ((SBUF *)(s))->b.buf + 1)
 /* we use a small buffer to minimize stack usage constraints */
 #define MYBUFSIZE	32
@@ -52,15 +61,18 @@
 
 typedef struct _SBUF {
     BUF b;
+#ifndef VARIANT_DYLD
     jmp_buf j;
+#endif /* !VARIANT_DYLD */
 } SBUF;
 
-static int asl_socket;
-static pthread_once_t asl_socket_once = PTHREAD_ONCE_INIT;
+#define ASL_SERVICE_NAME "com.apple.system.logger"
+
+static mach_port_t asl_port;
+static pthread_once_t asl_init_once = PTHREAD_ONCE_INIT;
 
 /* private extern exports from asl.c */
 const char *_asl_escape(unsigned char);
-int _asl_server_socket(int *, struct sockaddr_un *);
 
 /* flush the buffer */
 static void
@@ -107,7 +119,11 @@
     sold = SBUF_SIZE(b);
     snew = (sold + VM_PAGE_SIZE) & ~(VM_PAGE_SIZE - 1);
     if(vm_allocate(mach_task_self(), &new, snew, 1) != 0)
+#ifndef VARIANT_DYLD
 	longjmp(((SBUF *)b)->j, 1); /* out of memory */
+#else /* VARIANT_DYLD */
+	abort(); /* out of memory */
+#endif /* !VARIANT_DYLD */
     diff = new - (vm_address_t)b->buf;
     memcpy((void *)new, b->buf, sold);
     if((intptr_t)(b->buf) & (VM_PAGE_SIZE - 1)) {
@@ -498,8 +514,10 @@
 int
 _simple_vesprintf(_SIMPLE_STRING b, _esc_func esc, const char *fmt, va_list ap)
 {
+#ifndef VARIANT_DYLD
     if(setjmp(((SBUF *)b)->j))
 	return -1;
+#endif /* !VARIANT_DYLD */
     __simple_bprintf((BUF *)b, esc, fmt, ap);
     return 0;
 }
@@ -558,8 +576,10 @@
  */
 int _simple_esappend(_SIMPLE_STRING b, _esc_func esc, const char *str)
 {
+#ifndef VARIANT_DYLD
     if(setjmp(((SBUF *)b)->j))
 	return -1;
+#endif /* !VARIANT_DYLD */
     put_s((BUF *)b, esc, str);
     return 0;
 }
@@ -595,6 +615,7 @@
 {
     vm_size_t s;
 
+	if(b == NULL) return;
     if(((intptr_t)(((SBUF *)b)->b.buf) & (VM_PAGE_SIZE - 1)) == 0) {
 	vm_deallocate(mach_task_self(), (vm_address_t)((SBUF *)b)->b.buf, SBUF_SIZE(b));
 	s = VM_PAGE_SIZE;
@@ -607,83 +628,87 @@
  * Simplified ASL log interface; does not use malloc.  Unfortunately, this
  * requires knowledge of the format used by ASL.
  */
-static void
-socket_init(void)
-{
-    struct sockaddr_un server;
-    _asl_server_socket(&asl_socket, &server);
+
+static void
+_simple_asl_init(void)
+{
+	kern_return_t status;
+	char *str;
+
+	if (asl_port == MACH_PORT_NULL) 
+	{
+		str = getenv("ASL_DISABLE");
+		if ((str != NULL) && (!strcmp(str, "1"))) return;
+
+		status = bootstrap_look_up(bootstrap_port, ASL_SERVICE_NAME, &asl_port);
+		if (status != KERN_SUCCESS) asl_port = MACH_PORT_NULL;
+	}
+}
+
+void
+_simple_asl_log_prog(int level, const char *facility, const char *message, const char *prog)
+{
+    _SIMPLE_STRING b;
+
+    if (pthread_once(&asl_init_once, _simple_asl_init) != 0) return;
+	if (asl_port == MACH_PORT_NULL) return;
+
+    if ((b = _simple_salloc()) == NULL) return;
+
+    do
+	{
+		kern_return_t kstatus;
+		vm_address_t out;
+		int outlen;
+		char *cp;
+		struct timeval tv;
+
+		gettimeofday(&tv, NULL);
+
+		if (_simple_sprintf(b, "         0 [Time ", 0)) break;
+		if (_simple_esprintf(b, _asl_escape, "%lu", tv.tv_sec)) break;
+		if (_simple_sappend(b, "] [Sender ")) break;
+		if (_simple_esappend(b, _asl_escape, prog)) break;
+		if (_simple_sappend(b, "] [Level ")) break;
+		if (_simple_esprintf(b, _asl_escape, "%d", level)) break;
+		if (_simple_sappend(b, "] [Facility ")) break;
+		if (_simple_esappend(b, _asl_escape, facility)) break;
+		if (_simple_sappend(b, "] [Message ")) break;
+		if (_simple_esappend(b, _asl_escape, message)) break;
+
+		/* remove trailing (escaped) newlines */
+		cp = _simple_string(b);
+		cp += strlen(cp);
+		for (;;)
+		{
+			cp -= 2;
+			if (strcmp(cp, "\\n") != 0) break;
+			*cp = 0;
+		}
+
+		_simple_sresize(b);
+
+		if (_simple_sappend(b, "]\n")) break;
+
+		cp = _simple_string(b);
+
+		/*
+		 * The MIG defs for _asl_server_message specifies "dealloc",
+		 * so we copy the string into a new vm buffer and send that.
+		 */
+		outlen = strlen(cp);
+		kstatus = vm_allocate(mach_task_self(), &out, outlen, TRUE);
+		if (kstatus != KERN_SUCCESS) break;
+
+		memcpy((void *)out, cp, outlen);
+		_asl_server_message(asl_port, (caddr_t)out, outlen);
+    } while (0);
+
+   _simple_sfree(b);
 }
 
 void
 _simple_asl_log(int level, const char *facility, const char *message)
 {
-    _SIMPLE_STRING b;
-
-    if(pthread_once(&asl_socket_once, socket_init) != 0)
-	return;
-    if((b = _simple_salloc()) == NULL)
-	return;
-    do {
-	char *cp, *bp;
-	unsigned u;
-	struct timeval tv;
-
-	if(_simple_sprintf(b, "%10u [Time ", 0))
-	    break;
-	gettimeofday(&tv, NULL);
-	if(_simple_esprintf(b, _asl_escape, "%lu", tv.tv_sec))
-	    break;
-	if(_simple_sappend(b, "] [Host] [Sender "))
-	    break;
-	if(_simple_esappend(b, _asl_escape, getprogname()))
-	    break;
-	if(_simple_sappend(b, "] [PID "))
-	    break;
-	if(_simple_esprintf(b, _asl_escape, "%u", getpid()))
-	    break;
-	if(_simple_sappend(b, "] [UID "))
-	    break;
-	if(_simple_esprintf(b, _asl_escape, "%d", getuid()))
-	    break;
-	if(_simple_sappend(b, "] [GID "))
-	    break;
-	if(_simple_esprintf(b, _asl_escape, "%d", getgid()))
-	    break;
-	if(_simple_sappend(b, "] [Level "))
-	    break;
-	if(_simple_esprintf(b, _asl_escape, "%d", level))
-	    break;
-	if(_simple_sappend(b, "] [Message "))
-	    break;
-	if(_simple_esappend(b, _asl_escape, message))
-	    break;
-	/* remove trailing (escaped) newlines */
-	cp = _simple_string(b);
-	cp += strlen(cp);
-	for(;;) {
-	    cp -= 2;
-	    if(strcmp(cp, "\\n") != 0)
-		break;
-	    *cp = 0;
-	}
-	_simple_sresize(b);
-	if(_simple_sappend(b, "] [Facility "))
-	    break;
-	if(_simple_esappend(b, _asl_escape, facility))
-	    break;
-	if(_simple_sappend(b, "]\n"))
-	    break;
-	cp = _simple_string(b);
-	u = strlen(cp) - 10; // includes newline and null
-	bp = cp + 10;
-	if(u == 0)
-	    *--bp = '0';
-	else
-	    while(bp > cp && u) {
-		*--bp = u % 10 + '0';
-		u /= 10;
-	    }
-	write(asl_socket, cp, strlen(cp) + 1);
-    } while(0);
-    _simple_sfree(b);
-}
+    _simple_asl_log_prog(level, facility, message, getprogname());
+}