2 * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <mach/mach.h>
38 #include <servers/bootstrap.h>
43 #include <asl_private.h>
46 #include <asl_store.h>
49 #define MOD_CASE_FOLD 'C'
51 #define MOD_SUBSTRING 'S'
52 #define MOD_PREFIX 'A'
53 #define MOD_SUFFIX 'Z'
54 #define MOD_NUMERIC 'N'
63 #define ASL_QUERY_OP_NOT 0x1000
65 #define QUERY_FLAG_SEARCH_REVERSE 0x00000001
67 #define FACILITY_CONSOLE "com.apple.console"
69 /* Shared with Libc */
70 #define NOTIFY_RC "com.apple.asl.remote"
74 #define SEARCH_MATCH 1
76 #define PROC_NOT_FOUND -1
77 #define PROC_NOT_UNIQUE -2
83 #define forever for(;;)
85 #define SEND_FORMAT_LEGACY 0
86 #define SEND_FORMAT_ASL 1
88 #define TIME_SEC 0x00000010
89 #define TIME_UTC 0x00000020
90 #define TIME_LCL 0x00000040
92 #define FORMAT_RAW 0x00000100
93 #define FORMAT_LEGACY 0x00000200
94 #define FORMAT_STD 0x00000400
95 #define FORMAT_XML 0x00000800
96 #define COMPRESS_DUPS 0x00010000
98 #define EXPORT 0x00000100
100 #define ASL_FILTER_MASK_PACEWNID 0xff
101 #define ASL_FILTER_MASK_PACEWNI 0x7f
102 #define ASL_FILTER_MASK_PACEWN 0x3f
103 #define ASL_FILTER_MASK_PACEW 0x1f
104 #define ASL_FILTER_MASK_PACE 0x0f
105 #define ASL_FILTER_MASK_PAC 0x07
107 #define FETCH_BATCH 1024
108 #define MAX_RANDOM 8192
110 #define DB_SELECT_ASL 0
111 #define DB_SELECT_STORE 1
112 #define DB_SELECT_FILES 2
113 #define DB_SELECT_SYSLOGD 3
114 #define DB_SELECT_LEGACY 4
116 /* STD and BSD format messages start with 'DAY MMM DD HH:MM:SS ' timestamp */
117 #define STD_BSD_DATE_LEN 20
119 /* Max message size for direct watch */
120 #define MAX_DIRECT_SIZE 16384
122 /* Buffer for direct watch data */
123 #define DIRECT_BUF_SIZE 1024
125 static asl_file_list_t
*db_files
= NULL
;
126 static asl_store_t
*store
= NULL
;
127 static asl_file_t
*legacy
= NULL
;
128 static asl_file_t
*export
= NULL
;
129 static const char *sort_key
= NULL
;
130 static const char *sort_key_2
= NULL
;
131 static int sort_numeric
= 0;
132 static char *last_printmsg_str
= NULL
;
133 static int last_printmsg_count
= 0;
136 static uint32_t dbselect
= DB_SELECT_SYSLOGD
;
138 static uint32_t dbselect
= DB_SELECT_ASL
;
142 uint32_t notify_register_plain(const char *name
, int *out_token
);
144 extern asl_msg_t
*asl_msg_from_string(const char *buf
);
145 extern char *asl_list_to_string(asl_search_result_t
*list
, uint32_t *outlen
);
146 extern asl_search_result_t
*asl_list_from_string(const char *buf
);
147 extern int asl_msg_cmp(asl_msg_t
*a
, asl_msg_t
*b
);
148 extern time_t asl_parse_time(const char *in
);
149 /* END PRIVATE API */
151 #define ASL_SERVICE_NAME "com.apple.system.logger"
152 static mach_port_t asl_server_port
= MACH_PORT_NULL
;
154 static const char *myname
= "syslog";
159 fprintf(stderr
, "usage:\n");
160 fprintf(stderr
, "%s -s [-r host] [-l level] message...\n", myname
);
161 fprintf(stderr
, " send a message\n");
162 fprintf(stderr
, "\n");
163 fprintf(stderr
, "%s -s [-r host] -k key val [key val]...\n", myname
);
164 fprintf(stderr
, " send a message with the given keys and values\n");
165 fprintf(stderr
, "\n");
166 fprintf(stderr
, "%s -c process [filter]\n", myname
);
167 fprintf(stderr
, " get (set if filter is specified) syslog filter for process (pid or name)\n");
168 fprintf(stderr
, " level may be any combination of the characters \"p a c e w n i d\"\n");
169 fprintf(stderr
, " p = Emergency (\"Panic\")\n");
170 fprintf(stderr
, " a = Alert\n");
171 fprintf(stderr
, " c = Critical\n");
172 fprintf(stderr
, " e = Error\n");
173 fprintf(stderr
, " w = Warning\n");
174 fprintf(stderr
, " n = Notice\n");
175 fprintf(stderr
, " i = Info\n");
176 fprintf(stderr
, " d = Debug\n");
177 fprintf(stderr
, " a minus sign preceeding a single letter means \"up to\" that level\n");
178 fprintf(stderr
, "\n");
179 fprintf(stderr
, "%s [-f file...] [-d path...] [-x file] [-w [N]] [-F format] [-nocompress] [-u] [-sort key1 [key2]] [-nsort key1 [key2]] [-k key [[op] val]]... [-o -k key [[op] val]] ...]...\n", myname
);
180 fprintf(stderr
, " -f read named file[s], rather than standard log message store.\n");
181 fprintf(stderr
, " -d read all file in named directory path, rather than standard log message store.\n");
182 fprintf(stderr
, " -x export to named ASL format file, rather than printing\n");
183 fprintf(stderr
, " -w watch data store (^C to quit)\n");
184 fprintf(stderr
, " prints the last N matching lines (default 10) before waiting\n");
185 fprintf(stderr
, " \"-w all\" prints all matching lines before waiting\n");
186 fprintf(stderr
, " \"-w boot\" prints all matching lines since last system boot before waiting\n");
187 fprintf(stderr
, " -F output format may be \"std\", \"raw\", \"bsd\", or \"xml\"\n");
188 fprintf(stderr
, " format may also be a string containing variables of the form\n");
189 fprintf(stderr
, " $Key or $(Key) - use the latter for non-whitespace delimited variables\n");
190 fprintf(stderr
, " -T timestamp format may be \"sec\" (seconds), \"utc\" (UTC), or \"local\" (local timezone)\n");
191 fprintf(stderr
, " -E text encoding may be \"vis\", \"safe\", or \"none\"\n");
192 fprintf(stderr
, " -nodc no duplicate message compression\n");
193 fprintf(stderr
, " -u print timestamps using UTC (equivalent to \"-T utc\")\n");
194 fprintf(stderr
, " -sort sort messages using value for specified key1 (secondary sort by key2 if provided)\n");
195 fprintf(stderr
, " -nsort numeric sort messages using value for specified key1 (secondary sort by key2 if provided)\n");
196 fprintf(stderr
, " -k key/value match\n");
197 fprintf(stderr
, " if no operator or value is given, checks for the existance of the key\n");
198 fprintf(stderr
, " if no operator is given, default is \"%s\"\n", OP_EQ
);
199 fprintf(stderr
, " -B only process log messages since last system boot\n");
200 fprintf(stderr
, " -C alias for \"-k Facility com.apple.console\"\n");
201 fprintf(stderr
, " -o begins a new query\n");
202 fprintf(stderr
, " queries are \'OR\'ed together\n");
203 fprintf(stderr
, "operators are zero or more modifiers followed by a comparison\n");
204 fprintf(stderr
, " %s equal\n", OP_EQ
);
205 fprintf(stderr
, " %s not equal\n", OP_NE
);
206 fprintf(stderr
, " %s greater than\n", OP_GT
);
207 fprintf(stderr
, " %s greater or equal\n", OP_GE
);
208 fprintf(stderr
, " %s less than\n", OP_LT
);
209 fprintf(stderr
, " %s less or equal\n", OP_LE
);
210 fprintf(stderr
, "optional modifiers for operators\n");
211 fprintf(stderr
, " %c case-fold\n", MOD_CASE_FOLD
);
212 fprintf(stderr
, " %c regular expression\n", MOD_REGEX
);
213 fprintf(stderr
, " %c substring\n", MOD_SUBSTRING
);
214 fprintf(stderr
, " %c prefix\n", MOD_PREFIX
);
215 fprintf(stderr
, " %c suffix\n", MOD_SUFFIX
);
216 fprintf(stderr
, " %c numeric comparison\n", MOD_NUMERIC
);
220 notify_status_string(int status
)
222 if (status
== NOTIFY_STATUS_OK
) return "OK";
223 if (status
== NOTIFY_STATUS_INVALID_NAME
) return "Process not registered";
224 if (status
== NOTIFY_STATUS_NOT_AUTHORIZED
) return "Not authorized";
225 return "Operation failed";
229 asl_level_string(int level
)
231 if (level
== ASL_LEVEL_EMERG
) return ASL_STRING_EMERG
;
232 if (level
== ASL_LEVEL_ALERT
) return ASL_STRING_ALERT
;
233 if (level
== ASL_LEVEL_CRIT
) return ASL_STRING_CRIT
;
234 if (level
== ASL_LEVEL_ERR
) return ASL_STRING_ERR
;
235 if (level
== ASL_LEVEL_WARNING
) return ASL_STRING_WARNING
;
236 if (level
== ASL_LEVEL_NOTICE
) return ASL_STRING_NOTICE
;
237 if (level
== ASL_LEVEL_INFO
) return ASL_STRING_INFO
;
238 if (level
== ASL_LEVEL_DEBUG
) return ASL_STRING_DEBUG
;
243 procinfo(char *pname
, int *pid
, int *uid
)
246 int i
, status
, nprocs
;
248 struct kinfo_proc
*procs
, *newprocs
;
255 mib
[2] = KERN_PROC_ALL
;
259 status
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
263 newprocs
= reallocf(procs
, size
);
264 if (newprocs
== NULL
)
266 if (procs
!= NULL
) free(procs
);
267 return PROC_NOT_FOUND
;
271 status
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
272 } while ((status
== -1) && (errno
== ENOMEM
));
276 if (procs
!= NULL
) free(procs
);
277 return PROC_NOT_FOUND
;
280 if (size
% sizeof(struct kinfo_proc
) != 0)
282 if (procs
!= NULL
) free(procs
);
283 return PROC_NOT_FOUND
;
286 if (procs
== NULL
) return PROC_NOT_FOUND
;
288 nprocs
= size
/ sizeof(struct kinfo_proc
);
292 /* Search for a pid */
293 for (i
= 0; i
< nprocs
; i
++)
295 if (*pid
== procs
[i
].kp_proc
.p_pid
)
297 *uid
= procs
[i
].kp_eproc
.e_ucred
.cr_uid
;
302 return PROC_NOT_FOUND
;
305 *pid
= PROC_NOT_FOUND
;
307 for (i
= 0; i
< nprocs
; i
++)
309 if (!strcmp(procs
[i
].kp_proc
.p_comm
, pname
))
311 if (*pid
!= PROC_NOT_FOUND
)
314 return PROC_NOT_UNIQUE
;
317 *pid
= procs
[i
].kp_proc
.p_pid
;
318 *uid
= procs
[i
].kp_eproc
.e_ucred
.cr_uid
;
323 if (*pid
== PROC_NOT_FOUND
) return PROC_NOT_FOUND
;
329 rcontrol_get_string(const char *name
, int *val
)
334 status
= notify_register_plain(name
, &t
);
335 if (status
!= NOTIFY_STATUS_OK
) return status
;
338 status
= notify_get_state(t
, &x
);
347 rcontrol_set_string(const char *name
, int filter
)
352 status
= notify_register_plain(name
, &t
);
353 if (status
!= NOTIFY_STATUS_OK
) return status
;
356 status
= notify_set_state(t
, x
);
357 notify_post(NOTIFY_RC
);
363 asl_string_to_filter(char *s
)
367 if (s
== NULL
) return 0;
368 if (s
[0] == '\0') return 0;
370 if ((s
[0] >= '0') && (s
[0] <= '9')) return ASL_FILTER_MASK(atoi(s
));
374 if ((s
[1] == 'P') || (s
[1] == 'p')) i
= ASL_LEVEL_EMERG
;
375 else if ((s
[1] == 'A') || (s
[1] == 'a')) i
= ASL_LEVEL_ALERT
;
376 else if ((s
[1] == 'C') || (s
[1] == 'c')) i
= ASL_LEVEL_CRIT
;
377 else if ((s
[1] == 'E') || (s
[1] == 'e')) i
= ASL_LEVEL_ERR
;
378 else if ((s
[1] == 'X') || (s
[1] == 'x')) i
= ASL_LEVEL_ERR
;
379 else if ((s
[1] == 'W') || (s
[1] == 'w')) i
= ASL_LEVEL_WARNING
;
380 else if ((s
[1] == 'N') || (s
[1] == 'n')) i
= ASL_LEVEL_NOTICE
;
381 else if ((s
[1] == 'I') || (s
[1] == 'i')) i
= ASL_LEVEL_INFO
;
382 else if ((s
[1] == 'D') || (s
[1] == 'd')) i
= ASL_LEVEL_DEBUG
;
383 else i
= atoi(s
+ 1);
384 f
= ASL_FILTER_MASK_UPTO(i
);
389 for (i
= 0; s
[i
] != '\0'; i
++)
391 if ((s
[i
] == 'P') || (s
[i
] == 'p')) f
|= ASL_FILTER_MASK_EMERG
;
392 else if ((s
[i
] == 'A') || (s
[i
] == 'a')) f
|= ASL_FILTER_MASK_ALERT
;
393 else if ((s
[i
] == 'C') || (s
[i
] == 'c')) f
|= ASL_FILTER_MASK_CRIT
;
394 else if ((s
[i
] == 'E') || (s
[i
] == 'e')) f
|= ASL_FILTER_MASK_ERR
;
395 else if ((s
[i
] == 'X') || (s
[i
] == 'x')) f
|= ASL_FILTER_MASK_ERR
;
396 else if ((s
[i
] == 'W') || (s
[i
] == 'w')) f
|= ASL_FILTER_MASK_WARNING
;
397 else if ((s
[i
] == 'N') || (s
[i
] == 'n')) f
|= ASL_FILTER_MASK_NOTICE
;
398 else if ((s
[i
] == 'I') || (s
[i
] == 'i')) f
|= ASL_FILTER_MASK_INFO
;
399 else if ((s
[i
] == 'D') || (s
[i
] == 'd')) f
|= ASL_FILTER_MASK_DEBUG
;
406 asl_filter_string(int f
)
408 static char str
[1024];
411 memset(str
, 0, sizeof(str
));
414 if ((f
== ASL_FILTER_MASK_PACEWNID
) != 0)
416 strcat(str
, "Emergency - Debug");
420 if ((f
== ASL_FILTER_MASK_PACEWNI
) != 0)
422 strcat(str
, "Emergency - Info");
426 if ((f
== ASL_FILTER_MASK_PACEWN
) != 0)
428 strcat(str
, "Emergency - Notice");
432 if ((f
== ASL_FILTER_MASK_PACEW
) != 0)
434 strcat(str
, "Emergency - Warning");
438 if ((f
== ASL_FILTER_MASK_PACE
) != 0)
440 strcat(str
, "Emergency - Error");
444 if ((f
== ASL_FILTER_MASK_PAC
) != 0)
446 strcat(str
, "Emergency - Critical");
450 if ((f
& ASL_FILTER_MASK_EMERG
) != 0)
452 strcat(str
, "Emergency");
456 if ((f
& ASL_FILTER_MASK_ALERT
) != 0)
458 if (i
> 0) strcat(str
, ", ");
459 strcat(str
, "Alert");
463 if ((f
& ASL_FILTER_MASK_CRIT
) != 0)
465 if (i
> 0) strcat(str
, ", ");
466 strcat(str
, "Critical");
470 if ((f
& ASL_FILTER_MASK_ERR
) != 0)
472 if (i
> 0) strcat(str
, ", ");
473 strcat(str
, "Error");
477 if ((f
& ASL_FILTER_MASK_WARNING
) != 0)
479 if (i
> 0) strcat(str
, ", ");
480 strcat(str
, "Warning");
484 if ((f
& ASL_FILTER_MASK_NOTICE
) != 0)
486 if (i
> 0) strcat(str
, ", ");
487 strcat(str
, "Notice");
491 if ((f
& ASL_FILTER_MASK_INFO
) != 0)
493 if (i
> 0) strcat(str
, ", ");
498 if ((f
& ASL_FILTER_MASK_DEBUG
) != 0)
500 if (i
> 0) strcat(str
, ", ");
501 strcat(str
, "Debug");
505 if (i
== 0) sprintf(str
, "Off");
511 rcontrol_name(pid_t pid
, uid_t uid
)
513 static char str
[1024];
515 if (pid
== RC_SYSLOGD
) return NOTIFY_SYSTEM_ASL_FILTER
;
516 if (pid
== RC_MASTER
) return NOTIFY_SYSTEM_MASTER
;
518 memset(str
, 0, sizeof(str
));
519 if (uid
== 0) snprintf(str
, sizeof(str
) - 1, "%s.%d", NOTIFY_PREFIX_SYSTEM
, pid
);
520 else snprintf(str
, sizeof(str
) - 1, "user.uid.%d.syslog.%d", uid
, pid
);
525 rcontrol_get(pid_t pid
, uid_t uid
)
535 if (pid
== RC_SYSLOGD
) name
= "ASL Data Store";
537 status
= rcontrol_get_string(rcontrol_name(pid
, uid
), &filter
);
538 if (status
== NOTIFY_STATUS_OK
)
540 printf("%s filter mask: %s\n", name
, asl_filter_string(filter
));
544 printf("Unable to determine %s filter mask\n", name
);
548 status
= rcontrol_get_string(rcontrol_name(pid
, uid
), &filter
);
549 if (status
== NOTIFY_STATUS_OK
)
551 printf("Process %d syslog filter mask: %s\n", pid
, asl_filter_string(filter
));
555 printf("Unable to determine syslog filter mask for pid %d\n", pid
);
560 rcontrol_set(pid_t pid
, uid_t uid
, int filter
)
563 const char *name
, *rcname
;
565 rcname
= rcontrol_name(pid
, uid
);
570 if (pid
== RC_SYSLOGD
) name
= "ASL Data Store";
571 status
= rcontrol_set_string(rcname
, filter
);
573 if (status
== NOTIFY_STATUS_OK
)
575 if (pid
== RC_MASTER
) status
= notify_post(NOTIFY_SYSTEM_MASTER
);
579 printf("Unable to set %s syslog filter mask: %s\n", name
, notify_status_string(status
));
583 status
= rcontrol_set_string(rcname
, filter
);
584 if (status
== NOTIFY_STATUS_OK
)
586 status
= notify_post(rcname
);
590 printf("Unable to set syslog filter mask for pid %d: %s\n", pid
, notify_status_string(status
));
595 rsend(aslmsg msg
, char *rhost
)
603 struct sockaddr_in dst
;
605 char myname
[MAXHOSTNAMELEN
+ 1];
607 if (msg
== NULL
) return 0;
609 h
= gethostbyname(rhost
);
610 if (h
== NULL
) return -1;
612 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
613 if (s
<= 0) return -1;
615 memset(&dst
, 0, sizeof(struct sockaddr_in
));
616 memcpy(&(dst
.sin_addr
.s_addr
), h
->h_addr_list
[0], 4);
617 dst
.sin_family
= AF_INET
;
619 dst
.sin_len
= sizeof(struct sockaddr_in
);
621 level
= ASL_LEVEL_DEBUG
;
623 val
= asl_get(msg
, ASL_KEY_LEVEL
);
624 if (val
!= NULL
) level
= atoi(val
);
629 asprintf(×tr
, "%lu", tick
);
632 asl_set(msg
, ASL_KEY_TIME
, timestr
);
636 if (gethostname(myname
, MAXHOSTNAMELEN
) == 0) asl_set(msg
, ASL_KEY_HOST
, myname
);
639 str
= asl_msg_to_string((asl_msg_t
*)msg
, &len
);
640 if (str
== NULL
) return -1;
642 asprintf(&out
, "%10u %s\n", len
+1, str
);
644 if (out
== NULL
) return -1;
646 sendto(s
, out
, len
+12, 0, (const struct sockaddr
*)&dst
, sizeof(struct sockaddr_in
));
654 rlegacy(char *msg
, int level
, char *rhost
)
661 struct sockaddr_in dst
;
663 char myname
[MAXHOSTNAMELEN
+ 1];
665 if (msg
== NULL
) return 0;
667 h
= gethostbyname(rhost
);
668 if (h
== NULL
) return -1;
670 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
671 if (s
<= 0) return -1;
673 memset(&dst
, 0, sizeof(struct sockaddr_in
));
674 memcpy(&(dst
.sin_addr
.s_addr
), h
->h_addr_list
[0], 4);
675 dst
.sin_family
= AF_INET
;
677 dst
.sin_len
= sizeof(struct sockaddr_in
);
680 ltime
= ctime(&tick
);
683 gethostname(myname
, MAXHOSTNAMELEN
);
685 asprintf(&out
, "<%d>%s %s syslog[%d]: %s", level
, ltime
+4, myname
, getpid(), msg
);
687 sendto(s
, out
, len
, 0, (const struct sockaddr
*)&dst
, sizeof(struct sockaddr_in
));
699 if (s
== NULL
) return 0;
702 if ((s
[0] == '-') || (s
[0] == '+')) i
= 1;
704 if (s
[i
] == '\0') return 0;
706 for (; s
[i
] != '\0'; i
++)
708 if (!isdigit(s
[i
])) return 0;
715 asl_string_to_level(const char *s
)
717 if (s
== NULL
) return -1;
719 if ((s
[0] >= '0') && (s
[0] <= '7') && (s
[1] == '\0')) return atoi(s
);
721 if (!strncasecmp(s
, "em", 2)) return ASL_LEVEL_EMERG
;
722 else if (!strncasecmp(s
, "p", 1)) return ASL_LEVEL_EMERG
;
723 else if (!strncasecmp(s
, "a", 1)) return ASL_LEVEL_ALERT
;
724 else if (!strncasecmp(s
, "c", 1)) return ASL_LEVEL_CRIT
;
725 else if (!strncasecmp(s
, "er", 2)) return ASL_LEVEL_ERR
;
726 else if (!strncasecmp(s
, "x", 1)) return ASL_LEVEL_ERR
;
727 else if (!strncasecmp(s
, "w", 1)) return ASL_LEVEL_WARNING
;
728 else if (!strncasecmp(s
, "n", 1)) return ASL_LEVEL_NOTICE
;
729 else if (!strncasecmp(s
, "i", 1)) return ASL_LEVEL_INFO
;
730 else if (!strncasecmp(s
, "d", 1)) return ASL_LEVEL_DEBUG
;
736 asl_string_to_char_level(const char *s
)
738 if (s
== NULL
) return NULL
;
740 if ((s
[0] >= '0') && (s
[0] <= '7') && (s
[1] == '\0')) return s
;
742 if (!strncasecmp(s
, "em", 2)) return "0";
743 else if (!strncasecmp(s
, "p", 1)) return "0";
744 else if (!strncasecmp(s
, "a", 1)) return "1";
745 else if (!strncasecmp(s
, "c", 1)) return "2";
746 else if (!strncasecmp(s
, "er", 2)) return "3";
747 else if (!strncasecmp(s
, "x", 1)) return "3";
748 else if (!strncasecmp(s
, "w", 1)) return "4";
749 else if (!strncasecmp(s
, "n", 1)) return "5";
750 else if (!strncasecmp(s
, "i", 1)) return "6";
751 else if (!strncasecmp(s
, "d", 1)) return "7";
757 syslog_remote_control(int argc
, char *argv
[])
759 int pid
, uid
, status
, mask
;
761 if ((argc
< 3) || (argc
> 4))
763 fprintf(stderr
, "usage:\n");
764 fprintf(stderr
, "%s -c process [mask]\n", myname
);
765 fprintf(stderr
, " get (set if mask is specified) syslog filter mask for process (pid or name)\n");
766 fprintf(stderr
, " process may be pid or process name\n");
767 fprintf(stderr
, " use \"-c 0\" to get master syslog filter mask\n");
768 fprintf(stderr
, " use \"-c 0 off\" to disable master syslog filter mask\n");
769 fprintf(stderr
, "\n");
776 status
= PROC_NOT_FOUND
;
778 if ((!strcmp(argv
[2], "syslogd")) || (!strcmp(argv
[2], "syslog")))
784 else if (_isanumber(argv
[2]) != 0)
787 status
= procinfo(NULL
, &pid
, &uid
);
791 status
= procinfo(argv
[2], &pid
, &uid
);
794 if (status
== PROC_NOT_FOUND
)
796 fprintf(stderr
, "%s: process not found\n", argv
[2]);
800 if (status
== PROC_NOT_UNIQUE
)
802 fprintf(stderr
, "%s: multiple processes found\n", argv
[2]);
803 fprintf(stderr
, "use pid to identify a process uniquely\n");
807 if (pid
== 0) pid
= RC_MASTER
;
811 if ((pid
== RC_MASTER
) && (!strcasecmp(argv
[3], "off"))) mask
= 0;
812 else if ((pid
== RC_SYSLOGD
) && (!strcasecmp(argv
[3], "off"))) mask
= 0;
815 mask
= asl_string_to_filter(argv
[3]);
818 printf("unknown syslog mask: %s\n", argv
[3]);
823 rcontrol_set(pid
, uid
, mask
);
827 rcontrol_get(pid
, uid
);
834 syslog_send(int argc
, char *argv
[])
836 int i
, start
, kv
, len
, rfmt
, rlevel
, filter
, status
;
839 char tmp
[64], *str
, *rhost
;
843 rfmt
= SEND_FORMAT_LEGACY
;
847 for (i
= 1; i
< argc
; i
++)
849 if (!strcmp(argv
[i
], "-s")) start
= i
+1;
850 else if (!strcmp(argv
[i
], "-k"))
853 rfmt
= SEND_FORMAT_ASL
;
855 else if (!strcmp(argv
[i
], "-r"))
860 else if (!strcmp(argv
[i
], "-l"))
862 rlevel
= asl_string_to_level(argv
[++i
]);
865 fprintf(stderr
, "Unknown level: %s\n", argv
[i
]);
872 asl
= asl_open(myname
, "syslog", 0);
873 asl_set_filter(asl
, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG
));
875 m
= asl_new(ASL_TYPE_MSG
);
876 asl_set(m
, ASL_KEY_SENDER
, myname
);
878 sprintf(tmp
, "%d", rlevel
);
879 asl_set(m
, ASL_KEY_LEVEL
, tmp
);
886 for (i
= start
; i
< argc
; i
++) len
+= (strlen(argv
[i
]) + 1);
887 str
= calloc(len
+ 1, 1);
888 if (str
== NULL
) return -1;
890 for (i
= start
; i
< argc
; i
++)
892 strcat(str
, argv
[i
]);
893 if ((i
+1) < argc
) strcat(str
, " ");
895 asl_set(m
, ASL_KEY_MSG
, str
);
899 for (i
= start
+ 1; i
< argc
; i
+= 2)
901 if (!strcmp(argv
[i
], "-k")) i
++;
902 asl_set(m
, argv
[i
], argv
[i
+ 1]);
903 if (!strcmp(argv
[i
], ASL_KEY_LEVEL
)) rlevel
= atoi(argv
[i
+ 1]);
910 status
= rcontrol_get_string(rcontrol_name(RC_SYSLOGD
, 0), &filter
);
913 fprintf(stderr
, "Warning: Can't get current syslogd ASL filter value\n");
915 else if ((ASL_FILTER_MASK(rlevel
) & filter
) == 0)
917 fprintf(stderr
, "Warning: The current syslogd ASL filter value (%s)\n", asl_filter_string(filter
));
918 fprintf(stderr
, " will exclude this message from the ASL database\n");
923 else if (rfmt
== SEND_FORMAT_ASL
)
927 else if ((rfmt
== SEND_FORMAT_LEGACY
) && (str
!= NULL
))
929 rlegacy(str
, rlevel
, rhost
);
934 if (str
!= NULL
) free(str
);
942 print_xml_header(FILE *f
)
944 if (f
== NULL
) return;
946 fprintf(f
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
947 fprintf(f
, "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n");
948 fprintf(f
, "<plist version=\"1.0\">\n");
949 fprintf(f
, "<array>\n");
953 print_xml_trailer(FILE *f
)
955 if (f
== NULL
) return;
957 fprintf(f
, "</array>\n");
958 fprintf(f
, "</plist>\n");
962 printmsg(FILE *f
, aslmsg msg
, char *fmt
, int pflags
)
966 uint32_t encode
, len
, status
;
974 status
= asl_file_save(export
, msg
, &xid
);
975 if (status
!= ASL_STATUS_OK
)
977 fprintf(stderr
, "export file write failed: %s\n", asl_core_error(status
));
978 asl_file_close(export
);
986 encode
= pflags
& 0x0000000f;
988 mf
= ASL_MSG_FMT_RAW
;
989 if (fmt
!= NULL
) mf
= (const char *)fmt
;
990 else if (pflags
& FORMAT_STD
) mf
= ASL_MSG_FMT_STD
;
991 else if (pflags
& FORMAT_LEGACY
) mf
= ASL_MSG_FMT_BSD
;
992 else if (pflags
& FORMAT_XML
) mf
= ASL_MSG_FMT_XML
;
994 tf
= ASL_TIME_FMT_SEC
;
995 if (pflags
& TIME_UTC
) tf
= ASL_TIME_FMT_UTC
;
996 if (pflags
& TIME_LCL
) tf
= ASL_TIME_FMT_LCL
;
999 str
= asl_format_message((asl_msg_t
*)msg
, mf
, tf
, encode
, &len
);
1000 if (str
== NULL
) return;
1002 if (pflags
& COMPRESS_DUPS
)
1004 if (last_printmsg_str
!= NULL
)
1006 if (!strcmp(str
+ STD_BSD_DATE_LEN
, last_printmsg_str
+ STD_BSD_DATE_LEN
))
1008 last_printmsg_count
++;
1013 if (last_printmsg_count
> 0)
1015 fprintf(f
, "--- last message repeated %d time%s ---\n", last_printmsg_count
, (last_printmsg_count
== 1) ? "" : "s");
1018 free(last_printmsg_str
);
1019 last_printmsg_str
= str
;
1020 last_printmsg_count
= 0;
1022 fprintf(f
, "%s", str
);
1027 last_printmsg_str
= str
;
1028 last_printmsg_count
= 0;
1030 fprintf(f
, "%s", str
);
1035 fprintf(f
, "%s", str
);
1040 asl_search_result_t
*
1041 store_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1044 asl_search_result_t
*res
;
1048 status
= asl_store_open_read(NULL
, &store
);
1049 if (status
!= 0) return NULL
;
1053 status
= asl_store_match(store
, q
, &res
, last
, start
, count
, dir
);
1054 if (status
!= 0) return NULL
;
1059 asl_search_result_t
*
1060 file_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1063 asl_search_result_t
*res
;
1066 status
= asl_file_list_match(db_files
, q
, &res
, last
, start
, count
, dir
);
1067 if (status
!= 0) return NULL
;
1072 asl_search_result_t
*
1073 legacy_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1076 asl_search_result_t
*res
;
1079 status
= asl_file_match(legacy
, q
, &res
, last
, start
, count
, dir
);
1080 if (status
!= 0) return NULL
;
1085 asl_search_result_t
*
1086 syslogd_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1090 uint32_t len
, reslen
, status
;
1092 kern_return_t kstatus
;
1093 security_token_t sec
;
1094 asl_search_result_t
*l
;
1096 if (asl_server_port
== MACH_PORT_NULL
)
1098 kstatus
= bootstrap_look_up(bootstrap_port
, ASL_SERVICE_NAME
, &asl_server_port
);
1099 if (kstatus
!= KERN_SUCCESS
)
1101 fprintf(stderr
, "query failed: can't contact syslogd\n");
1107 str
= asl_list_to_string(q
, &len
);
1109 kstatus
= vm_allocate(mach_task_self(), (vm_address_t
*)&vmstr
, len
, TRUE
);
1110 if (kstatus
!= KERN_SUCCESS
)
1116 memmove(vmstr
, str
, len
);
1125 if (dir
< 0) flags
= QUERY_FLAG_SEARCH_REVERSE
;
1127 kstatus
= _asl_server_query(asl_server_port
, (caddr_t
)vmstr
, len
, start
, count
, flags
, (caddr_t
*)&res
, &reslen
, last
, (int *)&status
, &sec
);
1129 if (res
== NULL
) return NULL
;
1130 l
= asl_list_from_string(res
);
1131 vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1136 filter_and_print(aslmsg msg
, asl_search_result_t
*ql
, FILE *f
, char *pfmt
, int pflags
)
1138 int i
, do_match
, did_match
;
1140 if (msg
== NULL
) return;
1143 if (ql
== NULL
) do_match
= 0;
1144 else if (ql
->count
== 0) do_match
= 0;
1152 for (i
= 0; (i
< ql
->count
) && (did_match
== 0); i
++)
1154 did_match
= asl_msg_cmp(ql
->msg
[i
], (asl_msg_t
*)msg
);
1158 if (did_match
!= 0) printmsg(f
, msg
, pfmt
, pflags
);
1162 syslogd_direct_watch(FILE *f
, char *pfmt
, int pflags
, asl_search_result_t
*ql
)
1164 struct sockaddr_in address
;
1165 int i
, bytes
, n
, inlen
, sock
, stream
, status
;
1167 socklen_t addresslength
;
1168 char *str
, buf
[DIRECT_BUF_SIZE
];
1171 if (asl_server_port
== MACH_PORT_NULL
)
1173 status
= bootstrap_look_up(bootstrap_port
, ASL_SERVICE_NAME
, &asl_server_port
);
1174 if (status
!= KERN_SUCCESS
)
1176 fprintf(stderr
, "query failed: can't contact syslogd\n");
1183 addresslength
= sizeof(address
);
1184 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
1185 port
= (arc4random() % (IPPORT_HILASTAUTO
- IPPORT_HIFIRSTAUTO
)) + IPPORT_HIFIRSTAUTO
;
1187 address
.sin_addr
.s_addr
= 0;
1188 address
.sin_family
= AF_INET
;
1189 address
.sin_port
= htons(port
);
1191 status
= bind(sock
, (struct sockaddr
*)&address
, sizeof(address
));
1193 for (i
= 0; (i
< MAX_RANDOM
) && (status
< 0); i
++)
1195 port
= (arc4random() % (IPPORT_HILASTAUTO
- IPPORT_HIFIRSTAUTO
)) + IPPORT_HIFIRSTAUTO
;
1196 address
.sin_port
= htons(port
);
1198 status
= bind(sock
, (struct sockaddr
*)&address
, sizeof(address
));
1203 fprintf(stderr
, "query failed: can't find a port to connect to syslogd\n");
1209 if (listen(sock
, 1) == -1)
1216 _asl_server_register_direct_watch(asl_server_port
, i
);
1218 stream
= accept(sock
, (struct sockaddr
*)&address
, &addresslength
);
1228 bytes
= recvfrom(stream
, &n
, sizeof(n
), 0, NULL
, NULL
);
1229 if (bytes
== 0) break;
1230 else inlen
= ntohl(n
);
1232 if (inlen
== 0) continue;
1233 if (inlen
> MAX_DIRECT_SIZE
)
1235 fprintf(f
, "\n*** received invalid message data [%d] from syslogd - resetting connection ***\n\n", inlen
);
1242 if (inlen
<= DIRECT_BUF_SIZE
)
1248 str
= calloc(1, inlen
+ 1);
1251 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
1261 bytes
= recvfrom(stream
, str
+ n
, inlen
- n
, 0, NULL
, NULL
);
1262 if (bytes
== 0) break;
1266 msg
= (aslmsg
)asl_msg_from_string(str
);
1267 if (str
!= buf
) free(str
);
1268 filter_and_print(msg
, ql
, f
, pfmt
, pflags
);
1275 address
.sin_addr
.s_addr
= 0;
1279 sort_compare_key(const void *a
, const void *b
, const char *key
)
1282 const char *va
, *vb
;
1285 if (key
== NULL
) return 0;
1290 va
= asl_get(*ma
, key
);
1291 vb
= asl_get(*mb
, key
);
1293 if (va
== NULL
) return -1;
1294 if (vb
== NULL
) return 1;
1296 if (sort_numeric
== 1)
1300 if (na
< nb
) return -1;
1301 if (na
> nb
) return 1;
1305 return strcmp(va
, vb
);
1309 sort_compare(const void *a
, const void *b
)
1313 if (sort_key
== NULL
) return 0;
1315 cmp
= sort_compare_key(a
, b
, sort_key
);
1316 if ((cmp
== 0) && (sort_key_2
!= NULL
)) cmp
= sort_compare_key(a
, b
, sort_key_2
);
1322 search_once(FILE *f
, char *pfmt
, int pflags
, asl_search_result_t
*ql
, uint64_t qmin
, uint64_t *cmax
, uint32_t count
, uint32_t batch
, int dir
, uint32_t tail
)
1324 asl_search_result_t
*res
;
1327 if (pflags
& FORMAT_XML
) print_xml_header(f
);
1335 if (batch
== 0) more
= 0;
1337 if ((dbselect
== DB_SELECT_ASL
) || (dbselect
== DB_SELECT_STORE
)) res
= store_query(ql
, qmin
, batch
, dir
, cmax
);
1338 else if (dbselect
== DB_SELECT_FILES
) res
= file_query(ql
, qmin
, batch
, dir
, cmax
);
1339 else if (dbselect
== DB_SELECT_SYSLOGD
) res
= syslogd_query(ql
, qmin
, batch
, dir
, cmax
);
1340 else if (dbselect
== DB_SELECT_LEGACY
) res
= legacy_query(ql
, qmin
, batch
, dir
, cmax
);
1342 if ((dir
>= 0) && (*cmax
> qmin
)) qmin
= *cmax
+ 1;
1343 else if ((dir
< 0) && (*cmax
< qmin
)) qmin
= *cmax
- 1;
1351 if ((batch
> 0) && (res
->count
< batch
)) more
= 0;
1352 total
+= res
->count
;
1353 if ((count
> 0) && (total
>= count
)) more
= 0;
1358 i
= res
->count
- tail
;
1363 if (sort_key
!= NULL
)
1365 qsort(res
->msg
, res
->count
, sizeof(asl_msg_t
*), sort_compare
);
1368 if ((f
!= NULL
) || (export
!= NULL
))
1370 for (; i
< res
->count
; i
++) printmsg(f
, (aslmsg
)(res
->msg
[i
]), pfmt
, pflags
);
1373 aslresponse_free((aslresponse
)res
);
1377 if ((pflags
& COMPRESS_DUPS
) && (last_printmsg_count
> 0))
1379 fprintf(f
, "--- last message repeated %d time%s ---\n", last_printmsg_count
, (last_printmsg_count
== 1) ? "" : "s");
1380 free(last_printmsg_str
);
1381 last_printmsg_str
= NULL
;
1382 last_printmsg_count
= 0;
1385 if (pflags
& FORMAT_XML
) print_xml_trailer(f
);
1393 op
= ASL_QUERY_OP_NULL
;
1395 if (o
== NULL
) return op
;
1397 for (i
= 0; o
[i
] != '\0'; i
++)
1399 if (o
[i
] == MOD_CASE_FOLD
) op
|= ASL_QUERY_OP_CASEFOLD
;
1400 else if (o
[i
] == MOD_REGEX
) op
|= ASL_QUERY_OP_REGEX
;
1401 else if (o
[i
] == MOD_NUMERIC
) op
|= ASL_QUERY_OP_NUMERIC
;
1402 else if (o
[i
] == MOD_SUBSTRING
) op
|= ASL_QUERY_OP_SUBSTRING
;
1403 else if (o
[i
] == MOD_PREFIX
) op
|= ASL_QUERY_OP_PREFIX
;
1404 else if (o
[i
] == MOD_SUFFIX
) op
|= ASL_QUERY_OP_SUFFIX
;
1406 else if (!strncasecmp(o
+i
, OP_EQ
, sizeof(OP_EQ
)))
1408 op
|= ASL_QUERY_OP_EQUAL
;
1409 i
+= (sizeof(OP_EQ
) - 2);
1411 else if (!strncasecmp(o
+i
, OP_NE
, sizeof(OP_NE
)))
1413 op
|= ASL_QUERY_OP_NOT_EQUAL
;
1414 i
+= (sizeof(OP_NE
) - 2);
1416 else if (!strncasecmp(o
+i
, OP_GT
, sizeof(OP_GT
)))
1418 op
|= ASL_QUERY_OP_GREATER
;
1419 i
+= (sizeof(OP_GT
) - 2);
1421 else if (!strncasecmp(o
+i
, OP_GE
, sizeof(OP_GE
)))
1423 op
|= ASL_QUERY_OP_GREATER_EQUAL
;
1424 i
+= (sizeof(OP_GE
) - 2);
1426 else if (!strncasecmp(o
+i
, OP_LT
, sizeof(OP_LT
)))
1428 op
|= ASL_QUERY_OP_LESS
;
1429 i
+= (sizeof(OP_LT
) - 2);
1431 else if (!strncasecmp(o
+i
, OP_LE
, sizeof(OP_LE
)))
1433 op
|= ASL_QUERY_OP_LESS_EQUAL
;
1434 i
+= (sizeof(OP_LE
) - 2);
1438 fprintf(stderr
, "invalid option: %s\n", o
);
1444 if (op
& ASL_QUERY_OP_NUMERIC
)
1446 if (op
& ASL_QUERY_OP_CASEFOLD
)
1448 fprintf(stderr
, "warning: case fold modifier has no effect with numeric comparisons\n");
1449 op
&= ~ASL_QUERY_OP_CASEFOLD
;
1452 if (op
& ASL_QUERY_OP_REGEX
)
1454 fprintf(stderr
, "warning: regex modifier has no effect with numeric comparisons\n");
1455 op
&= ~ASL_QUERY_OP_REGEX
;
1458 if (op
& ASL_QUERY_OP_SUBSTRING
)
1460 fprintf(stderr
, "warning: substring modifier has no effect with numeric comparisons\n");
1461 op
&= ~ASL_QUERY_OP_SUBSTRING
;
1464 if (op
& ASL_QUERY_OP_PREFIX
)
1466 fprintf(stderr
, "warning: prefix modifier has no effect with numeric comparisons\n");
1467 op
&= ~ASL_QUERY_OP_PREFIX
;
1470 if (op
& ASL_QUERY_OP_SUFFIX
)
1472 fprintf(stderr
, "warning: suffix modifier has no effect with numeric comparisons\n");
1473 op
&= ~ASL_QUERY_OP_SUFFIX
;
1477 if (op
& ASL_QUERY_OP_REGEX
)
1479 if (op
& ASL_QUERY_OP_SUBSTRING
)
1481 fprintf(stderr
, "warning: substring modifier has no effect with regular expression comparisons\n");
1482 op
&= ~ASL_QUERY_OP_SUBSTRING
;
1485 if (op
& ASL_QUERY_OP_PREFIX
)
1487 fprintf(stderr
, "warning: prefix modifier has no effect with regular expression comparisons\n");
1488 op
&= ~ASL_QUERY_OP_PREFIX
;
1491 if (op
& ASL_QUERY_OP_SUFFIX
)
1493 fprintf(stderr
, "warning: suffix modifier has no effect with regular expression comparisons\n");
1494 op
&= ~ASL_QUERY_OP_SUFFIX
;
1502 add_op(asl_msg_t
*q
, char *key
, char *op
, char *val
, uint32_t flags
)
1507 if (key
== NULL
) return -1;
1508 if (q
== NULL
) return -1;
1511 if (strcmp(key
, ASL_KEY_TIME
) == 0)
1513 qval
= (const char *)val
;
1515 else if ((strcmp(key
, ASL_KEY_LEVEL
) == 0) && (_isanumber(val
) == 0))
1517 /* Convert level strings to numeric values */
1518 qval
= asl_string_to_char_level(val
);
1521 fprintf(stderr
, "invalid value for \"Level\"key: %s\n", val
);
1526 o
= ASL_QUERY_OP_NULL
;
1527 if (val
== NULL
) o
= ASL_QUERY_OP_TRUE
;
1532 if (o
== ASL_QUERY_OP_NULL
) return -1;
1535 fprintf(stderr
, "no value supplied for operator %s %s\n", key
, op
);
1539 if ((qval
== NULL
) && (o
& ASL_QUERY_OP_NUMERIC
) && (_isanumber(val
) == 0))
1541 fprintf(stderr
, "non-numeric value supplied for numeric operator %s %s %s\n", key
, op
, val
);
1547 if (qval
!= NULL
) asl_msg_set_key_val_op(q
, key
, qval
, o
);
1548 else asl_msg_set_key_val_op(q
, key
, val
, o
);
1554 add_db_file(const char *name
)
1559 if (dbselect
== DB_SELECT_LEGACY
)
1561 fprintf(stderr
, "syslog can only read one legacy format database\n");
1562 fprintf(stderr
, "can't combine legacy and non-legacy databases in a single search\n");
1566 /* shouldn't happen */
1567 if (name
== NULL
) return DB_SELECT_ASL
;
1570 status
= asl_file_open_read(name
, &s
);
1571 if (status
!= ASL_STATUS_OK
)
1573 fprintf(stderr
, "data store file %s open failed: %s \n", name
, asl_core_error(status
));
1579 fprintf(stderr
, "data store file %s open failed\n", name
);
1583 if (s
->flags
& ASL_FILE_FLAG_LEGACY_STORE
)
1585 if (db_files
!= NULL
)
1587 fprintf(stderr
, "syslog can only read a single legacy format database\n");
1588 fprintf(stderr
, "can't combine legacy and non-legacy databases in a single search\n");
1593 return DB_SELECT_LEGACY
;
1596 db_files
= asl_file_list_add(db_files
, s
);
1597 return DB_SELECT_FILES
;
1601 add_db_dir(const char *name
)
1604 struct dirent
*dent
;
1610 * Try opening as a data store
1612 status
= asl_store_open_read(name
, &store
);
1615 if (name
== NULL
) return DB_SELECT_ASL
;
1616 if (!strcmp(name
, PATH_ASL_STORE
)) return DB_SELECT_ASL
;
1617 return DB_SELECT_STORE
;
1621 * Open all readable files
1626 fprintf(stderr
, "%s: %s\n", name
, strerror(errno
));
1630 while ((dent
= readdir(dp
)) != NULL
)
1632 if (dent
->d_name
[0] == '.') continue;
1635 asprintf(&path
, "%s/%s", name
, dent
->d_name
);
1638 * asl_file_open_read will fail if path is NULL,
1639 * if the file is not an ASL store file,
1640 * or if it isn't readable.
1643 status
= asl_file_open_read(path
, &s
);
1644 if (path
!= NULL
) free(path
);
1645 if ((status
!= ASL_STATUS_OK
) || (s
== NULL
)) continue;
1647 db_files
= asl_file_list_add(db_files
, s
);
1652 return DB_SELECT_FILES
;
1656 main(int argc
, char *argv
[])
1659 int i
, j
, n
, watch
, status
, pflags
, tflags
, iamroot
, user_tflag
, export_preserve_id
, saw_dash_d
, since_boot
;
1660 int notify_file
, notify_token
;
1661 asl_search_result_t
*qlist
;
1664 const char *exportname
;
1665 uint32_t flags
, tail_count
, batch
, encode
;
1666 uint64_t qmin
, cmax
;
1674 batch
= FETCH_BATCH
;
1675 pflags
= FORMAT_STD
| COMPRESS_DUPS
;
1677 encode
= ASL_ENCODE_SAFE
;
1680 export_preserve_id
= 0;
1684 i
= asl_store_location();
1685 if (i
== ASL_STORE_LOCATION_MEMORY
) dbselect
= DB_SELECT_SYSLOGD
;
1687 if (getuid() == 0) iamroot
= 1;
1689 for (i
= 1; i
< argc
; i
++)
1691 if ((!strcmp(argv
[i
], "-help")) || (!strcmp(argv
[i
], "--help")))
1697 if (!strcmp(argv
[i
], "-time"))
1700 printf("%llu\n", qmin
);
1704 if (!strcmp(argv
[i
], "-s"))
1706 syslog_send(argc
, argv
);
1710 if (!strcmp(argv
[i
], "-c"))
1712 syslog_remote_control(argc
, argv
);
1717 qlist
= (asl_search_result_t
*)calloc(1, sizeof(asl_search_result_t
));
1718 if (qlist
== NULL
) exit(1);
1720 for (i
= 1; i
< argc
; i
++)
1722 if (!strcmp(argv
[i
], "-f"))
1726 for (j
= i
+ 1; j
< argc
; j
++)
1728 if (!strcmp(argv
[j
], "-"))
1730 dbselect
= DB_SELECT_SYSLOGD
;
1734 else if (argv
[j
][0] == '-')
1740 dbselect
= add_db_file(argv
[j
]);
1746 else if (!strcmp(argv
[i
], "-d"))
1750 if (saw_dash_d
< argc
)
1752 for (j
= saw_dash_d
; j
< argc
; j
++)
1754 if (!strcmp(argv
[j
], "store"))
1756 dbselect
= add_db_dir(PATH_ASL_STORE
);
1759 else if (!strcmp(argv
[j
], "archive"))
1761 dbselect
= add_db_dir(PATH_ASL_ARCHIVE
);
1764 else if (argv
[j
][0] == '-')
1770 dbselect
= add_db_dir(argv
[j
]);
1777 fprintf(stderr
, "missing directory name following -d flag\n");
1781 else if (!strcmp(argv
[i
], "-b"))
1783 batch
= atoi(argv
[++i
]);
1785 else if (!strcmp(argv
[i
], "-B"))
1789 else if (!strcmp(argv
[i
], "-w"))
1793 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1796 if (!strcmp(argv
[i
], "all"))
1798 tail_count
= (uint32_t)-1;
1800 else if (!strcmp(argv
[i
], "boot"))
1806 tail_count
= atoi(argv
[i
]);
1810 else if (!strcmp(argv
[i
], "-sort"))
1812 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1817 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1820 sort_key_2
= argv
[i
];
1825 sort_key
= ASL_KEY_MSG_ID
;
1830 else if (!strcmp(argv
[i
], "-nsort"))
1832 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1837 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1840 sort_key_2
= argv
[i
];
1845 sort_key
= ASL_KEY_MSG_ID
;
1851 else if (!strcmp(argv
[i
], "-u"))
1856 else if ((!strcmp(argv
[i
], "-x")) || (!strcmp(argv
[i
], "-X")))
1858 if ((i
+ 1) >= argc
)
1860 aslresponse_free(qlist
);
1865 if (!strcmp(argv
[i
], "-x")) export_preserve_id
= 1;
1867 exportname
= argv
[++i
];
1869 else if (!strcmp(argv
[i
], "-E"))
1871 if ((i
+ 1) >= argc
)
1873 aslresponse_free(qlist
);
1880 if (!strcmp(argv
[i
], "vis")) encode
= ASL_ENCODE_ASL
;
1881 else if (!strcmp(argv
[i
], "safe")) encode
= ASL_ENCODE_SAFE
;
1882 else if (!strcmp(argv
[i
], "none")) encode
= ASL_ENCODE_NONE
;
1883 else if ((argv
[i
][0] >= '0') && (argv
[i
][0] <= '9') && (argv
[i
][1] == '\0')) encode
= atoi(argv
[i
]);
1885 else if (!strcmp(argv
[i
], "-F"))
1887 if ((i
+ 1) >= argc
)
1889 aslresponse_free(qlist
);
1896 if (!strcmp(argv
[i
], "raw"))
1898 pflags
= FORMAT_RAW
;
1899 if (user_tflag
== 0) tflags
= TIME_SEC
;
1901 else if (!strcmp(argv
[i
], "std"))
1903 pflags
= FORMAT_STD
| COMPRESS_DUPS
;
1905 else if (!strcmp(argv
[i
], "bsd"))
1907 pflags
= FORMAT_LEGACY
| COMPRESS_DUPS
;
1909 else if (!strcmp(argv
[i
], "xml"))
1911 pflags
= FORMAT_XML
;
1919 else if (!strcmp(argv
[i
], "-T"))
1921 if ((i
+ 1) >= argc
)
1923 aslresponse_free(qlist
);
1931 if (!strcmp(argv
[i
], "sec")) tflags
= TIME_SEC
;
1932 else if (!strcmp(argv
[i
], "utc")) tflags
= TIME_UTC
;
1933 else if (!strcmp(argv
[i
], "local")) tflags
= TIME_LCL
;
1934 else if (!strcmp(argv
[i
], "lcl")) tflags
= TIME_LCL
;
1935 else tflags
= TIME_LCL
;
1937 else if (!strcmp(argv
[i
], "-nodc"))
1939 pflags
= pflags
& ~COMPRESS_DUPS
;
1941 else if (!strcmp(argv
[i
], "-o"))
1945 if (qlist
->count
== 0)
1947 qlist
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
1951 qlist
->msg
= (asl_msg_t
**)reallocf(qlist
->msg
, (qlist
->count
+ 1) * sizeof(asl_msg_t
*));
1954 if (qlist
->msg
== NULL
) exit(1);
1956 cq
= asl_msg_new(ASL_TYPE_QUERY
);
1957 qlist
->msg
[qlist
->count
] = cq
;
1960 else if (!strcmp(argv
[i
], "-n"))
1962 flags
= ASL_QUERY_OP_NOT
;
1964 else if (!strcmp(argv
[i
], "-C"))
1966 if (qlist
->count
== 0)
1968 qlist
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
1969 if (qlist
->msg
== NULL
) exit(1);
1971 cq
= asl_msg_new(ASL_TYPE_QUERY
);
1972 qlist
->msg
[qlist
->count
] = cq
;
1976 status
= add_op(cq
, ASL_KEY_FACILITY
, OP_EQ
, FACILITY_CONSOLE
, flags
);
1981 aslresponse_free(qlist
);
1985 else if (!strcmp(argv
[i
], "-k"))
1988 for (n
= i
; n
< argc
; n
++)
1990 if (!strcmp(argv
[n
], "-o")) break;
1991 if (!strcmp(argv
[n
], "-n")) break;
1992 if (!strcmp(argv
[n
], "-k")) break;
1995 fprintf(stderr
, "invalid sequence: -k");
1996 for (j
= i
; j
<= n
; j
++) fprintf(stderr
, " %s", argv
[j
]);
1997 fprintf(stderr
, "\n");
2010 if (qlist
->count
== 0)
2012 qlist
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
2013 if (qlist
->msg
== NULL
) exit(1);
2015 cq
= asl_msg_new(ASL_TYPE_QUERY
);
2016 qlist
->msg
[qlist
->count
] = cq
;
2021 if (n
== 1) status
= add_op(cq
, argv
[i
], NULL
, NULL
, flags
);
2022 else if (n
== 2) status
= add_op(cq
, argv
[i
], OP_EQ
, argv
[i
+1], flags
);
2023 else status
= add_op(cq
, argv
[i
], argv
[i
+1], argv
[i
+2], flags
);
2028 aslresponse_free(qlist
);
2036 fprintf(stderr
, "syslog: unknown option \"%s\"\n", argv
[i
]);
2037 fprintf(stderr
, "run \"syslog -help\" for usage\n");
2048 * Catch and report some cases where watch (-w) doesn't work
2052 if (sort_key
!= NULL
)
2054 fprintf(stderr
, "Warning: -w flag has no effect with -sort flag\n");
2058 if (dbselect
== DB_SELECT_FILES
)
2060 if (saw_dash_d
== 0)
2062 fprintf(stderr
, "Warning: -w flag not supported for a set of one or more files\n");
2066 fprintf(stderr
, "Warning: directory \"%s\" is not an ASL data store\n", argv
[saw_dash_d
]);
2067 fprintf(stderr
, " -w flag not supported for a set of one or more files\n");
2074 if (exportname
!= NULL
)
2078 fprintf(stderr
, "Warning: -w flag has no effect with -x export flag\n");
2082 status
= asl_file_open_write(exportname
, 0644, -1, -1, &export
);
2083 if (status
!= ASL_STATUS_OK
)
2085 aslresponse_free(qlist
);
2086 fprintf(stderr
, "export file open failed: %s\n", asl_core_error(status
));
2091 * allow the string cache to be unlimited to maximize string dup compression
2092 * preserve message IDs
2094 export
->flags
= ASL_FILE_FLAG_UNLIMITED_CACHE
;
2095 if (export_preserve_id
!= 0) export
->flags
|= ASL_FILE_FLAG_PRESERVE_MSG_ID
;
2106 /* set starting point */
2107 if (since_boot
== 1)
2109 /* search back for last "BOOT_TIME (ut_type == 2) record */
2110 asl_search_result_t
*bt
;
2113 bt
= (asl_search_result_t
*)calloc(1, sizeof(asl_search_result_t
));
2116 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
2120 bt
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
2121 if (bt
->msg
== NULL
)
2123 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
2127 bq
= asl_new(ASL_TYPE_QUERY
);
2130 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
2134 asl_set_query(bq
, "ut_type", "2", ASL_QUERY_OP_EQUAL
);
2136 bt
->msg
[0] = (asl_msg_t
*)bq
;
2139 search_once(NULL
, NULL
, 0, bt
, -1, &qmin
, 1, 1, -1, 0);
2144 if (qmin
> 0) qmin
--;
2147 else if (watch
== 1)
2149 /* go back tail_count records from last record */
2151 search_once(NULL
, NULL
, 0, qlist
, qmin
, &cmax
, 1, 1, -1, 0);
2153 if (cmax
>= tail_count
) qmin
= cmax
- tail_count
;
2159 if ((watch
== 1) && (dbselect
== DB_SELECT_ASL
))
2161 status
= notify_register_file_descriptor("com.apple.system.logger.message", ¬ify_file
, 0, ¬ify_token
);
2162 if (status
!= NOTIFY_STATUS_OK
) notify_token
= -1;
2165 /* output should be line buffered */
2166 if (outfile
!= NULL
) setlinebuf(outfile
);
2168 search_once(outfile
, pfmt
, pflags
, qlist
, qmin
+ 1, &cmax
, 0, 0, 1, tail_count
);
2172 if (dbselect
== DB_SELECT_SYSLOGD
)
2174 syslogd_direct_watch(outfile
, pfmt
, pflags
, qlist
);
2176 else if (notify_token
== -1)
2181 if (cmax
> qmin
) qmin
= cmax
;
2182 search_once(outfile
, pfmt
, pflags
, qlist
, qmin
+ 1, &cmax
, 0, batch
, 1, 0);
2187 while (read(notify_file
, &i
, 4) == 4)
2189 if (cmax
> qmin
) qmin
= cmax
;
2190 search_once(outfile
, pfmt
, pflags
, qlist
, qmin
+ 1, &cmax
, 0, batch
, 1, 0);
2195 if (db_files
!= NULL
) asl_file_list_close(db_files
);
2196 if (store
!= NULL
) asl_store_close(store
);
2197 if (export
!= NULL
) asl_file_close(export
);
2199 aslresponse_free(qlist
);