xserver-multidpi/hw/xquartz/console_redirect.c
Jeremy Huddleston 5324557c7b XQuartz: Source formatting cleanup
indent butchered Objective-C formatting.  This patch was created by:

1) Reverting the indent changes in hw/xquartz
2) Editing X11Application.m and chaning some #ifdef logic to work
   with uncrustify
3) Hand edited some (c) notifications
4) Opened all XQuartz sources in XCode and re-indented (^I)
5) Ran uncrustify with this configuration
   (as close to the indent rules as I could get):

tok_split_gte=false
utf8_byte=true
utf8_force=true
indent_cmt_with_tabs=false
indent_align_string=false
indent_braces=false
indent_braces_no_func=false
indent_braces_no_class=false
indent_braces_no_struct=false
indent_brace_parent=false
indent_namespace=false
indent_extern=false
indent_class=false
indent_class_colon=false
indent_else_if=false
indent_var_def_cont=false
indent_func_call_param=false
indent_func_def_param=false
indent_func_proto_param=false
indent_func_class_param=false
indent_func_ctor_var_param=false
indent_template_param=false
indent_func_param_double=false
indent_relative_single_line_comments=false
indent_col1_comment=false
indent_access_spec_body=false
indent_paren_nl=false
indent_comma_paren=false
indent_bool_paren=false
indent_first_bool_expr=false
indent_square_nl=false
indent_preserve_sql=false
indent_align_assign=true
sp_balance_nested_parens=false
align_keep_tabs=false
align_with_tabs=false
align_on_tabstop=false
align_number_left=false
align_func_params=false
align_same_func_call_params=false
align_var_def_colon=true
align_var_def_attribute=true
align_var_def_inline=true
align_right_cmt_mix=false
align_on_operator=false
align_mix_var_proto=false
align_single_line_func=false
align_single_line_brace=false
align_nl_cont=false
align_left_shift=true
align_oc_decl_colon=true
nl_collapse_empty_body=true
nl_assign_leave_one_liners=true
nl_class_leave_one_liners=true
nl_enum_leave_one_liners=true
nl_getset_leave_one_liners=true
nl_func_leave_one_liners=true
nl_if_leave_one_liners=true
nl_multi_line_cond=false
nl_multi_line_define=false
nl_before_case=true
nl_after_case=true
nl_after_return=false
nl_after_semicolon=true
nl_after_brace_open=true
nl_after_brace_open_cmt=false
nl_after_vbrace_open=false
nl_after_vbrace_open_empty=false
nl_after_brace_close=false
nl_after_vbrace_close=false
nl_define_macro=false
nl_squeeze_ifdef=false
nl_ds_struct_enum_cmt=false
nl_ds_struct_enum_close_brace=false
nl_create_if_one_liner=false
nl_create_for_one_liner=false
nl_create_while_one_liner=false
ls_for_split_full=false
ls_func_split_full=false
nl_after_multiline_comment=false
eat_blanks_after_open_brace=false
eat_blanks_before_close_brace=false
mod_full_brace_if_chain=false
mod_pawn_semicolon=false
mod_full_paren_if_bool=false
mod_remove_extra_semicolon=false
mod_sort_import=false
mod_sort_using=false
mod_sort_include=false
mod_move_case_break=false
mod_remove_empty_return=false
cmt_indent_multi=true
cmt_c_group=false
cmt_c_nl_start=false
cmt_c_nl_end=false
cmt_cpp_group=false
cmt_cpp_nl_start=false
cmt_cpp_nl_end=false
cmt_cpp_to_c=false
cmt_star_cont=false
cmt_multi_check_last=true
cmt_insert_before_preproc=false
pp_indent_at_level=false
pp_region_indent_code=false
pp_if_indent_code=false
pp_define_at_level=false
indent_columns=4
indent_brace=0
indent_switch_case=0
align_struct_init_span=2
align_pp_define_gap=0
align_pp_define_span=2
align_oc_msg_colon_span=16
nl_end_of_file_min=1
nl_func_var_def_blk=0
code_width=78
nl_max=2
newlines=auto
indent_with_tabs=0
sp_arith=force
sp_assign=force
sp_assign_default=force
sp_before_assign=force
sp_after_assign=force
sp_enum_assign=force
sp_enum_before_assign=force
sp_enum_after_assign=force
sp_pp_stringify=add
sp_bool=force
sp_compare=force
sp_inside_paren=remove
sp_paren_paren=remove
sp_paren_brace=force
sp_before_ptr_star=ignore
sp_before_unnamed_ptr_star=force
sp_before_byref=force
sp_before_unnamed_byref=force
sp_after_byref=remove
sp_after_type=force
sp_before_sparen=force
sp_inside_sparen=remove
sp_inside_sparen_close=remove
sp_after_sparen=force
sp_sparen_brace=force
sp_special_semi=force
sp_before_semi=remove
sp_after_semi=force
sp_after_semi_for=force
sp_after_semi_for_empty=remove
sp_before_square=remove
sp_inside_square=remove
sp_after_comma=force
sp_before_comma=remove
sp_paren_comma=force
sp_before_ellipsis=force
sp_after_class_colon=force
sp_before_class_colon=force
sp_before_case_colon=remove
sp_after_cast=remove
sp_inside_paren_cast=remove
sp_sizeof_paren=remove
sp_inside_braces_enum=force
sp_inside_braces_struct=force
sp_inside_braces=force
sp_inside_braces_empty=remove
sp_func_proto_paren=remove
sp_func_def_paren=remove
sp_inside_fparens=remove
sp_inside_fparen=remove
sp_square_fparen=remove
sp_fparen_brace=force
sp_func_call_paren=remove
sp_func_call_paren_empty=remove
sp_return_paren=force
sp_attribute_paren=remove
sp_defined_paren=remove
sp_macro=force
sp_macro_func=force
sp_else_brace=force
sp_brace_else=force
sp_brace_typedef=force
sp_not=remove
sp_inv=remove
nl_start_of_file=remove
nl_end_of_file=force
nl_assign_square=remove
nl_after_square_assign=remove
nl_fcall_brace=remove
nl_enum_brace=remove
nl_struct_brace=remove
nl_union_brace=remove
nl_if_brace=remove
nl_brace_else=force
nl_elseif_brace=remove
nl_else_brace=remove
nl_else_if=remove
nl_for_brace=remove
nl_do_brace=remove
nl_brace_while=remove
nl_switch_brace=remove
nl_case_colon_brace=force
nl_func_type_name=force
nl_func_type_name_class=force
nl_func_proto_type_name=force
nl_func_paren=remove
nl_func_def_paren=remove
nl_func_decl_start=remove
nl_func_def_start=remove
nl_func_decl_args=remove
nl_func_decl_end=remove
nl_func_def_end=remove
nl_func_decl_end_single=remove
nl_func_def_end_single=remove
nl_func_decl_empty=remove
nl_func_def_empty=remove
nl_fdef_brace=force
nl_return_expr=remove
nl_before_if=ignore
nl_after_if=ignore
nl_before_for=ignore
nl_after_for=ignore
nl_before_while=ignore
nl_after_while=ignore
nl_before_switch=ignore
nl_after_switch=ignore
nl_before_do=ignore
nl_after_do=ignore
pp_space=remove

Signed-off-by: Jeremy Huddleston <jeremyhu@apple.com>
2012-03-24 01:07:06 -07:00

463 lines
14 KiB
C

/*
* Copyright (c) 2011-2012 Apple Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
* HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name(s) of the above
* copyright holders shall not be used in advertising or otherwise to
* promote the sale, use or other dealings in this Software without
* prior written authorization.
*/
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#else
#define DEBUG_CONSOLE_REDIRECT 1
#define HAVE_LIBDISPATCH 1
#endif
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/event.h>
#include <asl.h>
#include <errno.h>
#include <fcntl.h>
#include "console_redirect.h"
#define BUF_SIZE 512
#ifdef HAVE_LIBDISPATCH
#include <dispatch/dispatch.h>
static dispatch_queue_t redirect_serial_q;
static dispatch_group_t read_source_group;
#else
#include <pthread.h>
static pthread_t redirect_pthread;
static pthread_mutex_t redirect_fds_lock = PTHREAD_MUTEX_INITIALIZER;
static int kq;
/* Notifications to our reader thread */
#define ASL_REDIRECT_TERMINATE ((void *)(uintptr_t)1)
#endif
typedef struct {
int level;
aslclient asl;
aslmsg msg;
/* Buffered reading */
char *buf;
char *w;
#ifdef HAVE_LIBDISPATCH
dispatch_source_t read_source;
#endif
} asl_redirect;
static asl_redirect *redirect_fds = NULL;
static int n_redirect_fds = 0;
/* Read from the FD until there is no more to read and redirect to ASL.
* Preconditions:
* 1: pthread_mutex_lock lock is held (pthreads) or called
* from the appropriate serial queue for operating on
* redirect_fds
* 2: fd corresponds to a valid entry in redirect_fds
*
* Return values:
* If the pipe is closed, EOF is returned regardless of how many bytes
* were processed. If the pipe is still open, the number of read bytes
* is returned.
*/
static inline int
_read_redirect(int fd, int flush)
{
int total_read = 0;
int nbytes;
asl_redirect *aslr = &redirect_fds[fd];
while ((nbytes =
read(fd, aslr->w,
BUF_SIZE - (aslr->w - aslr->buf) - 1)) > 0) {
char *s, *p;
/* Increment our returned number read */
total_read += nbytes;
nbytes += (aslr->w - aslr->buf);
aslr->buf[nbytes] = '\0';
/* One line at a time */
for (p = aslr->buf; *p && (p - aslr->buf) < nbytes; p = s + 1) {
// Find null or \n
for (s = p; *s && *s != '\n'; s++) ;
if (*s == '\n') {
*s = '\0';
asl_log(aslr->asl, aslr->msg, aslr->level, "%s", p);
}
else if (aslr->buf != p) {
memmove(aslr->buf, p, BUF_SIZE - (p - aslr->buf));
aslr->w = aslr->buf + (s - p);
break;
}
else if (nbytes == BUF_SIZE - 1) {
asl_log(aslr->asl, aslr->msg, aslr->level, "%s", p);
aslr->w = aslr->buf;
break;
}
}
}
/* Flush if requested or we're at EOF */
if (flush || nbytes == 0) {
if (aslr->w > aslr->buf) {
*aslr->w = '\0';
asl_log(aslr->asl, aslr->msg, aslr->level, "%s", aslr->buf);
}
}
if (nbytes == 0)
return EOF;
return total_read;
}
#ifdef HAVE_LIBDISPATCH
static void
read_from_source(void *_source)
{
dispatch_source_t source = (dispatch_source_t)_source;
int fd = dispatch_source_get_handle(source);
if (_read_redirect(fd, 0) == EOF) {
dispatch_source_cancel(source);
}
}
static void
cancel_source(void *_source)
{
dispatch_source_t source = (dispatch_source_t)_source;
int fd = dispatch_source_get_handle(source);
asl_redirect *aslr = &redirect_fds[fd];
/* Flush the buffer */
_read_redirect(fd, 1);
close(fd);
free(aslr->buf);
memset(aslr, 0, sizeof(*aslr));
dispatch_release(source);
dispatch_group_leave(read_source_group);
}
#else /* !HAVE_LIBDISPATCH */
static void *
redirect_thread(void *ctx __unused)
{
struct kevent ev;
int n;
while (1) {
n = kevent(kq, NULL, 0, &ev, 1, NULL);
/* Bail on errors */
if (n < 0) {
asl_log(NULL, NULL, ASL_LEVEL_ERR, "kevent failure: %s",
strerror(errno));
break;
}
/* This should not happen */
if (n == 0)
continue;
switch (ev.filter) {
case EVFILT_READ:
pthread_mutex_lock(&redirect_fds_lock);
{
int fd = ev.ident;
int close_fd = 0;
asl_redirect *aslr = &redirect_fds[fd];
if (fd < 0 || fd >= n_redirect_fds || aslr->buf == NULL) {
asl_log(NULL, NULL, ASL_LEVEL_ERR,
"Unexpected file descriptor: %d",
fd);
goto next;
}
if (ev.flags & EV_EOF) {
close_fd = 1;
if (EOF != _read_redirect(fd, 1)) {
asl_log(
NULL, NULL, ASL_LEVEL_ERR,
"kevent reported EOF on %d, but read doesn't concur.",
fd);
}
}
else {
close_fd = (EOF == _read_redirect(fd, 0));
}
if (close_fd) {
EV_SET(&ev, fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
kevent(kq, &ev, 1, NULL, 0, NULL);
close(fd);
free(aslr->buf);
memset(aslr, 0, sizeof(*aslr));
}
}
next:
pthread_mutex_unlock(&redirect_fds_lock);
case EVFILT_TIMER:
if (ev.udata == ASL_REDIRECT_TERMINATE)
return NULL;
default:
;
;
}
}
return NULL;
}
#endif
static void
redirect_atexit(void)
{
/* stdout is linebuffered, so flush the buffer */
if (redirect_fds[STDOUT_FILENO].buf)
fflush(stdout);
#ifdef HAVE_LIBDISPATCH
{
int i;
/* Cancel all of our dispatch sources, so they flush to ASL */
for (i = 0; i < n_redirect_fds; i++)
if (redirect_fds[i].read_source)
dispatch_source_cancel(redirect_fds[i].read_source);
/* Wait at least three seconds for our sources to flush to ASL */
dispatch_group_wait(read_source_group,
dispatch_time(DISPATCH_TIME_NOW, 3LL *
NSEC_PER_SEC));
}
#else
{
struct kevent ev;
/* Tell our reader thread it is time to pack up and go home */
EV_SET(&ev, 0, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 0,
ASL_REDIRECT_TERMINATE);
kevent(kq, &ev, 1, NULL, 0, NULL);
pthread_join(redirect_pthread, NULL);
}
#endif
}
#ifdef HAVE_LIBDISPATCH
static void
xq_asl_init(void *ctx __unused)
#else
static void
xq_asl_init(void)
#endif
{
assert((redirect_fds = calloc(16, sizeof(*redirect_fds))) != NULL);
n_redirect_fds = 16;
#ifdef HAVE_LIBDISPATCH
redirect_serial_q = dispatch_queue_create("com.apple.asl-redirect", NULL);
assert(redirect_serial_q != NULL);
read_source_group = dispatch_group_create();
assert(read_source_group != NULL);
#else
assert((kq = kqueue()) != -1);
assert(pthread_create(&redirect_pthread, NULL, redirect_thread,
NULL) == 0);
#endif
atexit(redirect_atexit);
}
int
xq_asl_log_fd(aslclient asl, aslmsg msg, int level, int fd)
{
#ifdef HAVE_LIBDISPATCH
int err __block = 0;
static dispatch_once_t once_control;
dispatch_once_f(&once_control, NULL, xq_asl_init);
#else
int err = 0;
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
assert(pthread_once(&once_control, xq_asl_init) == 0);
#endif
if (fd < 0)
return EBADF;
#ifdef HAVE_LIBDISPATCH
#define BLOCK_DONE return
dispatch_sync(redirect_serial_q, ^
#else
#define BLOCK_DONE goto done
assert(pthread_mutex_lock(&redirect_fds_lock) == 0);
#endif
{
/* Reallocate if we need more space */
if (fd >= n_redirect_fds) {
size_t new_n = 1 << (ffs(fd) + 1);
asl_redirect *new_array =
realloc(redirect_fds, new_n *
sizeof(*redirect_fds));
if (!new_array) {
err = errno;
BLOCK_DONE;
}
redirect_fds = new_array;
memset(redirect_fds + n_redirect_fds, 0, new_n -
n_redirect_fds);
n_redirect_fds = new_n;
}
/* If we're already listening on it, return error. */
if (redirect_fds[fd].buf != NULL) {
err = EBADF;
BLOCK_DONE;
}
/* Initialize our buffer */
redirect_fds[fd].buf = (char *)malloc(BUF_SIZE);
if (redirect_fds[fd].buf == NULL) {
err = errno;
BLOCK_DONE;
}
redirect_fds[fd].w = redirect_fds[fd].buf;
/* Store our ASL settings */
redirect_fds[fd].level = level;
redirect_fds[fd].asl = asl;
redirect_fds[fd].msg = msg;
/* Don't block on reads from this fd */
fcntl(fd, F_SETFL,
O_NONBLOCK);
/* Start listening */
#ifdef HAVE_LIBDISPATCH
{
dispatch_source_t read_source =
dispatch_source_create(
DISPATCH_SOURCE_TYPE_READ, fd, 0,
redirect_serial_q);
redirect_fds[fd].read_source = read_source;
dispatch_set_context(read_source, read_source);
dispatch_source_set_event_handler_f(read_source,
read_from_source);
dispatch_source_set_cancel_handler_f(read_source,
cancel_source);
dispatch_group_enter(read_source_group);
dispatch_resume(read_source);
}
#else
{
struct kevent ev;
EV_SET(&ev, fd, EVFILT_READ, EV_ADD, 0, 0, 0);
kevent(kq, &ev, 1, NULL, 0, NULL);
}
#endif
}
#ifdef HAVE_LIBDISPATCH
);
#else
done:
assert(pthread_mutex_unlock(&redirect_fds_lock) == 0);
#endif
#undef BLOCK_DONE
return err;
}
int
xq_asl_capture_fd(aslclient asl, aslmsg msg, int level, int fd)
{
int pipepair[2];
/* Create pipe */
if (pipe(pipepair) == -1)
return errno;
/* Close the read fd but not the write fd on exec */
if (fcntl(pipepair[0], F_SETFD, FD_CLOEXEC) == -1)
return errno;
/* Replace the existing fd */
if (dup2(pipepair[1], fd) == -1) {
close(pipepair[0]);
close(pipepair[1]);
return errno;
}
/* If we capture STDOUT_FILENO, make sure we linebuffer stdout */
if (fd == STDOUT_FILENO)
setlinebuf(stdout);
/* Close the duplicate fds since they've been reassigned */
close(pipepair[1]);
/* Hand off the read end of our pipe to xq_asl_log_fd */
return xq_asl_log_fd(asl, msg, level, pipepair[0]);
}
#ifdef DEBUG_CONSOLE_REDIRECT
int
main(int argc __unused, char * *argv __unused)
{
xq_asl_capture_fd(NULL, NULL, ASL_LEVEL_NOTICE, STDOUT_FILENO);
xq_asl_capture_fd(NULL, NULL, ASL_LEVEL_ERR, STDERR_FILENO);
fprintf(stderr, "TEST ERR1\n");
fprintf(stdout, "TEST OUT1\n");
fprintf(stderr, "TEST ERR2\n");
fprintf(stdout, "TEST OUT2\n");
system("/bin/echo SYST OUT");
system("/bin/echo SYST ERR >&2");
fprintf(stdout, "TEST OUT3\n");
fprintf(stdout, "TEST OUT4\n");
fprintf(stderr, "TEST ERR3\n");
fprintf(stderr, "TEST ERR4\n");
exit(0);
}
#endif