PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/opensource.apple.com/source/syslog/syslog-100.0.1/syslogd.tproj/klog_in.c

#
C | 132 lines | 94 code | 18 blank | 20 comment | 5 complexity | cb80e9457fb6dcc4e6c51164389f3675 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, MPL-2.0, LGPL-2.0, LGPL-2.1, CC-BY-SA-3.0, IPL-1.0, ISC, AGPL-1.0, AGPL-3.0, JSON, Apache-2.0, 0BSD
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>klog_in.c</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">klog_in.c&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. <span class="enscript-comment">/*
  24. * Copyright (c) 2004-2008 Apple Inc. All rights reserved.
  25. *
  26. * @APPLE_LICENSE_HEADER_START@
  27. *
  28. * This file contains Original Code and/or Modifications of Original Code
  29. * as defined in and that are subject to the Apple Public Source License
  30. * Version 2.0 (the 'License'). You may not use this file except in
  31. * compliance with the License. Please obtain a copy of the License at
  32. * <a href="http://www.opensource.apple.com/apsl/">http://www.opensource.apple.com/apsl/</a> and read it before using this
  33. * file.
  34. *
  35. * The Original Code and all software distributed under the License are
  36. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  37. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  38. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  39. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  40. * Please see the License for the specific language governing rights and
  41. * limitations under the License.
  42. *
  43. * @APPLE_LICENSE_HEADER_END@
  44. */</span>
  45. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/types.h&gt;</span>
  46. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/stat.h&gt;</span>
  47. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/socket.h&gt;</span>
  48. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/un.h&gt;</span>
  49. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;sys/uio.h&gt;</span>
  50. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;stdio.h&gt;</span>
  51. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;stdlib.h&gt;</span>
  52. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;string.h&gt;</span>
  53. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;fcntl.h&gt;</span>
  54. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;errno.h&gt;</span>
  55. #<span class="enscript-reference">include</span> <span class="enscript-string">&lt;unistd.h&gt;</span>
  56. #<span class="enscript-reference">include</span> <span class="enscript-string">&quot;daemon.h&quot;</span>
  57. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">forever</span> for(;;)
  58. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">MY_ID</span> <span class="enscript-string">&quot;klog_in&quot;</span>
  59. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">MAXLINE</span> 4096
  60. <span class="enscript-type">static</span> <span class="enscript-type">int</span> kx = 0;
  61. <span class="enscript-type">static</span> <span class="enscript-type">char</span> kline[MAXLINE + 1];
  62. asl_msg_t *
  63. <span class="enscript-function-name">klog_in_acceptmsg</span>(<span class="enscript-type">int</span> fd)
  64. {
  65. <span class="enscript-type">int</span> n;
  66. <span class="enscript-type">char</span> c;
  67. n = read(fd, &amp;c, 1);
  68. <span class="enscript-keyword">while</span> ((n == 1) &amp;&amp; (c != <span class="enscript-string">'\n'</span>))
  69. {
  70. <span class="enscript-keyword">if</span> (kx &lt; MAXLINE) kline[kx++] = c;
  71. n = read(fd, &amp;c, 1);
  72. }
  73. <span class="enscript-keyword">if</span> (kx == 0) <span class="enscript-keyword">return</span> NULL;
  74. n = kx - 1;
  75. kline[kx] = <span class="enscript-string">'\0'</span>;
  76. kx = 0;
  77. <span class="enscript-keyword">return</span> asl_input_parse(kline, n, NULL, 1);
  78. }
  79. <span class="enscript-type">int</span>
  80. <span class="enscript-function-name">klog_in_init</span>(<span class="enscript-type">void</span>)
  81. {
  82. asldebug(<span class="enscript-string">&quot;%s: init\n&quot;</span>, MY_ID);
  83. <span class="enscript-keyword">if</span> (global.kfd &gt;= 0) <span class="enscript-keyword">return</span> global.kfd;
  84. global.kfd = open(_PATH_KLOG, O_RDONLY, 0);
  85. <span class="enscript-keyword">if</span> (global.kfd &lt; 0)
  86. {
  87. asldebug(<span class="enscript-string">&quot;%s: couldn't open %s: %s\n&quot;</span>, MY_ID, _PATH_KLOG, strerror(errno));
  88. <span class="enscript-keyword">return</span> -1;
  89. }
  90. <span class="enscript-keyword">if</span> (fcntl(global.kfd, F_SETFL, O_NONBLOCK) &lt; 0)
  91. {
  92. close(global.kfd);
  93. global.kfd = -1;
  94. asldebug(<span class="enscript-string">&quot;%s: couldn't set O_NONBLOCK for fd %d (%s): %s\n&quot;</span>, MY_ID, global.kfd, _PATH_KLOG, strerror(errno));
  95. <span class="enscript-keyword">return</span> -1;
  96. }
  97. <span class="enscript-keyword">return</span> aslevent_addfd(SOURCE_KERN, global.kfd, ADDFD_FLAGS_LOCAL, klog_in_acceptmsg, NULL, NULL);
  98. }
  99. <span class="enscript-type">int</span>
  100. <span class="enscript-function-name">klog_in_reset</span>(<span class="enscript-type">void</span>)
  101. {
  102. <span class="enscript-keyword">return</span> 0;
  103. }
  104. <span class="enscript-type">int</span>
  105. <span class="enscript-function-name">klog_in_close</span>(<span class="enscript-type">void</span>)
  106. {
  107. <span class="enscript-keyword">if</span> (global.kfd &lt; 0) <span class="enscript-keyword">return</span> 1;
  108. close(global.kfd);
  109. global.kfd = -1;
  110. <span class="enscript-keyword">return</span> 0;
  111. }
  112. </pre>
  113. <hr />
  114. </body></html>