2 * Copyright (c) 2007-2011 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"
72 /* XXX add to asl_private.h */
73 #define ASL_OPT_CONTROL "control"
77 #define SEARCH_MATCH 1
79 #define PROC_NOT_FOUND -1
80 #define PROC_NOT_UNIQUE -2
85 #define forever for(;;)
87 #define SEND_FORMAT_LEGACY 0
88 #define SEND_FORMAT_ASL 1
90 #define FORMAT_RAW 0x00000100
91 #define FORMAT_LEGACY 0x00000200
92 #define FORMAT_STD 0x00000400
93 #define FORMAT_XML 0x00000800
94 #define COMPRESS_DUPS 0x00010000
96 #define EXPORT 0x00000100
98 #define ASL_FILTER_MASK_PACEWNID 0xff
99 #define ASL_FILTER_MASK_PACEWNI 0x7f
100 #define ASL_FILTER_MASK_PACEWN 0x3f
101 #define ASL_FILTER_MASK_PACEW 0x1f
102 #define ASL_FILTER_MASK_PACE 0x0f
103 #define ASL_FILTER_MASK_PAC 0x07
105 #define FETCH_BATCH 1024
106 #define MAX_RANDOM 8192
108 #define DB_SELECT_ASL 0
109 #define DB_SELECT_STORE 1
110 #define DB_SELECT_FILES 2
111 #define DB_SELECT_SYSLOGD 3
112 #define DB_SELECT_LEGACY 4
114 /* STD and BSD format messages start with 'DAY MMM DD HH:MM:SS ' timestamp */
115 #define STD_BSD_DATE_LEN 20
117 /* Max message size for direct watch */
118 #define MAX_DIRECT_SIZE 16384
120 /* Buffer for direct watch data */
121 #define DIRECT_BUF_SIZE 1024
123 static asl_file_list_t
*db_files
= NULL
;
124 static asl_store_t
*store
= NULL
;
125 static asl_file_t
*legacy
= NULL
;
126 static asl_file_t
*export
= NULL
;
127 static const char *sort_key
= NULL
;
128 static const char *sort_key_2
= NULL
;
129 static int sort_numeric
= 0;
130 static char *last_printmsg_str
= NULL
;
131 static int last_printmsg_count
= 0;
132 static const char *tfmt
= NULL
;
135 static uint32_t dbselect
= DB_SELECT_SYSLOGD
;
137 static uint32_t dbselect
= DB_SELECT_ASL
;
141 uint32_t notify_register_plain(const char *name
, int *out_token
);
143 extern asl_msg_t
*asl_msg_from_string(const char *buf
);
144 extern char *asl_list_to_string(asl_search_result_t
*list
, uint32_t *outlen
);
145 extern asl_search_result_t
*asl_list_from_string(const char *buf
);
146 extern int asl_msg_cmp(asl_msg_t
*a
, asl_msg_t
*b
);
147 extern time_t asl_parse_time(const char *in
);
148 /* END PRIVATE API */
150 #define ASL_SERVICE_NAME "com.apple.system.logger"
151 static mach_port_t asl_server_port
= MACH_PORT_NULL
;
153 static const char *myname
= "syslog";
158 fprintf(stderr
, "usage:\n");
159 fprintf(stderr
, "%s -s [-r host] [-l level] message...\n", myname
);
160 fprintf(stderr
, " send a message\n");
161 fprintf(stderr
, "\n");
162 fprintf(stderr
, "%s -s [-r host] -k key val [key val]...\n", myname
);
163 fprintf(stderr
, " send a message with the given keys and values\n");
164 fprintf(stderr
, "\n");
165 fprintf(stderr
, "%s -c process [filter]\n", myname
);
166 fprintf(stderr
, " get (set if filter is specified) syslog filter for process (pid or name)\n");
167 fprintf(stderr
, " level may be any combination of the characters \"p a c e w n i d\"\n");
168 fprintf(stderr
, " p = Emergency (\"Panic\")\n");
169 fprintf(stderr
, " a = Alert\n");
170 fprintf(stderr
, " c = Critical\n");
171 fprintf(stderr
, " e = Error\n");
172 fprintf(stderr
, " w = Warning\n");
173 fprintf(stderr
, " n = Notice\n");
174 fprintf(stderr
, " i = Info\n");
175 fprintf(stderr
, " d = Debug\n");
176 fprintf(stderr
, " a minus sign preceeding a single letter means \"up to\" that level\n");
177 fprintf(stderr
, "\n");
178 fprintf(stderr
, "%s -config params...\n", myname
);
179 fprintf(stderr
, " set or reset syslogd configuration parameters\n");
180 fprintf(stderr
, "\n");
181 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
);
182 fprintf(stderr
, " -f read named file[s], rather than standard log message store.\n");
183 fprintf(stderr
, " -d read all file in named directory path, rather than standard log message store.\n");
184 fprintf(stderr
, " -x export to named ASL format file, rather than printing\n");
185 fprintf(stderr
, " -w watch data store (^C to quit)\n");
186 fprintf(stderr
, " prints the last N matching lines (default 10) before waiting\n");
187 fprintf(stderr
, " \"-w all\" prints all matching lines before waiting\n");
188 fprintf(stderr
, " \"-w boot\" prints all matching lines since last system boot before waiting\n");
189 fprintf(stderr
, " -F output format may be \"std\", \"raw\", \"bsd\", or \"xml\"\n");
190 fprintf(stderr
, " format may also be a string containing variables of the form\n");
191 fprintf(stderr
, " $Key or $(Key) - use the latter for non-whitespace delimited variables\n");
192 fprintf(stderr
, " -T timestamp format may be \"sec\" (seconds), \"utc\" (UTC), or \"local\" (local timezone)\n");
193 fprintf(stderr
, " -E text encoding may be \"vis\", \"safe\", or \"none\"\n");
194 fprintf(stderr
, " -nodc no duplicate message compression\n");
195 fprintf(stderr
, " -u print timestamps using UTC (equivalent to \"-T utc\")\n");
196 fprintf(stderr
, " -sort sort messages using value for specified key1 (secondary sort by key2 if provided)\n");
197 fprintf(stderr
, " -nsort numeric sort messages using value for specified key1 (secondary sort by key2 if provided)\n");
198 fprintf(stderr
, " -k key/value match\n");
199 fprintf(stderr
, " if no operator or value is given, checks for the existance of the key\n");
200 fprintf(stderr
, " if no operator is given, default is \"%s\"\n", OP_EQ
);
201 fprintf(stderr
, " -B only process log messages since last system boot\n");
202 fprintf(stderr
, " -C alias for \"-k Facility com.apple.console\"\n");
203 fprintf(stderr
, " -o begins a new query\n");
204 fprintf(stderr
, " queries are \'OR\'ed together\n");
205 fprintf(stderr
, "operators are zero or more modifiers followed by a comparison\n");
206 fprintf(stderr
, " %s equal\n", OP_EQ
);
207 fprintf(stderr
, " %s not equal\n", OP_NE
);
208 fprintf(stderr
, " %s greater than\n", OP_GT
);
209 fprintf(stderr
, " %s greater or equal\n", OP_GE
);
210 fprintf(stderr
, " %s less than\n", OP_LT
);
211 fprintf(stderr
, " %s less or equal\n", OP_LE
);
212 fprintf(stderr
, "optional modifiers for operators\n");
213 fprintf(stderr
, " %c case-fold\n", MOD_CASE_FOLD
);
214 fprintf(stderr
, " %c regular expression\n", MOD_REGEX
);
215 fprintf(stderr
, " %c substring\n", MOD_SUBSTRING
);
216 fprintf(stderr
, " %c prefix\n", MOD_PREFIX
);
217 fprintf(stderr
, " %c suffix\n", MOD_SUFFIX
);
218 fprintf(stderr
, " %c numeric comparison\n", MOD_NUMERIC
);
222 notify_status_string(int status
)
224 if (status
== NOTIFY_STATUS_OK
) return "OK";
225 if (status
== NOTIFY_STATUS_INVALID_NAME
) return "Process not registered";
226 if (status
== NOTIFY_STATUS_NOT_AUTHORIZED
) return "Not authorized";
227 return "Operation failed";
231 asl_level_string(int level
)
233 if (level
== ASL_LEVEL_EMERG
) return ASL_STRING_EMERG
;
234 if (level
== ASL_LEVEL_ALERT
) return ASL_STRING_ALERT
;
235 if (level
== ASL_LEVEL_CRIT
) return ASL_STRING_CRIT
;
236 if (level
== ASL_LEVEL_ERR
) return ASL_STRING_ERR
;
237 if (level
== ASL_LEVEL_WARNING
) return ASL_STRING_WARNING
;
238 if (level
== ASL_LEVEL_NOTICE
) return ASL_STRING_NOTICE
;
239 if (level
== ASL_LEVEL_INFO
) return ASL_STRING_INFO
;
240 if (level
== ASL_LEVEL_DEBUG
) return ASL_STRING_DEBUG
;
245 procinfo(char *pname
, int *pid
, int *uid
)
248 int i
, status
, nprocs
;
250 struct kinfo_proc
*procs
, *newprocs
;
257 mib
[2] = KERN_PROC_ALL
;
261 status
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
265 newprocs
= reallocf(procs
, size
);
266 if (newprocs
== NULL
)
268 if (procs
!= NULL
) free(procs
);
269 return PROC_NOT_FOUND
;
273 status
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
274 } while ((status
== -1) && (errno
== ENOMEM
));
278 if (procs
!= NULL
) free(procs
);
279 return PROC_NOT_FOUND
;
282 if (size
% sizeof(struct kinfo_proc
) != 0)
284 if (procs
!= NULL
) free(procs
);
285 return PROC_NOT_FOUND
;
288 if (procs
== NULL
) return PROC_NOT_FOUND
;
290 nprocs
= size
/ sizeof(struct kinfo_proc
);
294 /* Search for a pid */
295 for (i
= 0; i
< nprocs
; i
++)
297 if (*pid
== procs
[i
].kp_proc
.p_pid
)
299 *uid
= procs
[i
].kp_eproc
.e_ucred
.cr_uid
;
304 return PROC_NOT_FOUND
;
307 *pid
= PROC_NOT_FOUND
;
309 for (i
= 0; i
< nprocs
; i
++)
311 if (!strcmp(procs
[i
].kp_proc
.p_comm
, pname
))
313 if (*pid
!= PROC_NOT_FOUND
)
316 return PROC_NOT_UNIQUE
;
319 *pid
= procs
[i
].kp_proc
.p_pid
;
320 *uid
= procs
[i
].kp_eproc
.e_ucred
.cr_uid
;
325 if (*pid
== PROC_NOT_FOUND
) return PROC_NOT_FOUND
;
331 rcontrol_get_string(const char *name
, int *val
)
336 status
= notify_register_plain(name
, &t
);
337 if (status
!= NOTIFY_STATUS_OK
) return status
;
340 status
= notify_get_state(t
, &x
);
349 rcontrol_set_string(const char *name
, int filter
)
354 status
= notify_register_plain(name
, &t
);
355 if (status
!= NOTIFY_STATUS_OK
) return status
;
358 status
= notify_set_state(t
, x
);
359 notify_post(NOTIFY_RC
);
365 asl_string_to_filter(char *s
)
369 if (s
== NULL
) return 0;
370 if (s
[0] == '\0') return 0;
372 if ((s
[0] >= '0') && (s
[0] <= '9')) return ASL_FILTER_MASK(atoi(s
));
376 if ((s
[1] == 'P') || (s
[1] == 'p')) i
= ASL_LEVEL_EMERG
;
377 else if ((s
[1] == 'A') || (s
[1] == 'a')) i
= ASL_LEVEL_ALERT
;
378 else if ((s
[1] == 'C') || (s
[1] == 'c')) i
= ASL_LEVEL_CRIT
;
379 else if ((s
[1] == 'E') || (s
[1] == 'e')) i
= ASL_LEVEL_ERR
;
380 else if ((s
[1] == 'X') || (s
[1] == 'x')) i
= ASL_LEVEL_ERR
;
381 else if ((s
[1] == 'W') || (s
[1] == 'w')) i
= ASL_LEVEL_WARNING
;
382 else if ((s
[1] == 'N') || (s
[1] == 'n')) i
= ASL_LEVEL_NOTICE
;
383 else if ((s
[1] == 'I') || (s
[1] == 'i')) i
= ASL_LEVEL_INFO
;
384 else if ((s
[1] == 'D') || (s
[1] == 'd')) i
= ASL_LEVEL_DEBUG
;
385 else i
= atoi(s
+ 1);
386 f
= ASL_FILTER_MASK_UPTO(i
);
391 for (i
= 0; s
[i
] != '\0'; i
++)
393 if ((s
[i
] == 'P') || (s
[i
] == 'p')) f
|= ASL_FILTER_MASK_EMERG
;
394 else if ((s
[i
] == 'A') || (s
[i
] == 'a')) f
|= ASL_FILTER_MASK_ALERT
;
395 else if ((s
[i
] == 'C') || (s
[i
] == 'c')) f
|= ASL_FILTER_MASK_CRIT
;
396 else if ((s
[i
] == 'E') || (s
[i
] == 'e')) f
|= ASL_FILTER_MASK_ERR
;
397 else if ((s
[i
] == 'X') || (s
[i
] == 'x')) f
|= ASL_FILTER_MASK_ERR
;
398 else if ((s
[i
] == 'W') || (s
[i
] == 'w')) f
|= ASL_FILTER_MASK_WARNING
;
399 else if ((s
[i
] == 'N') || (s
[i
] == 'n')) f
|= ASL_FILTER_MASK_NOTICE
;
400 else if ((s
[i
] == 'I') || (s
[i
] == 'i')) f
|= ASL_FILTER_MASK_INFO
;
401 else if ((s
[i
] == 'D') || (s
[i
] == 'd')) f
|= ASL_FILTER_MASK_DEBUG
;
408 asl_filter_string(int f
)
410 static char str
[1024];
413 memset(str
, 0, sizeof(str
));
416 if ((f
== ASL_FILTER_MASK_PACEWNID
) != 0)
418 strcat(str
, "Emergency - Debug");
422 if ((f
== ASL_FILTER_MASK_PACEWNI
) != 0)
424 strcat(str
, "Emergency - Info");
428 if ((f
== ASL_FILTER_MASK_PACEWN
) != 0)
430 strcat(str
, "Emergency - Notice");
434 if ((f
== ASL_FILTER_MASK_PACEW
) != 0)
436 strcat(str
, "Emergency - Warning");
440 if ((f
== ASL_FILTER_MASK_PACE
) != 0)
442 strcat(str
, "Emergency - Error");
446 if ((f
== ASL_FILTER_MASK_PAC
) != 0)
448 strcat(str
, "Emergency - Critical");
452 if ((f
& ASL_FILTER_MASK_EMERG
) != 0)
454 strcat(str
, "Emergency");
458 if ((f
& ASL_FILTER_MASK_ALERT
) != 0)
460 if (i
> 0) strcat(str
, ", ");
461 strcat(str
, "Alert");
465 if ((f
& ASL_FILTER_MASK_CRIT
) != 0)
467 if (i
> 0) strcat(str
, ", ");
468 strcat(str
, "Critical");
472 if ((f
& ASL_FILTER_MASK_ERR
) != 0)
474 if (i
> 0) strcat(str
, ", ");
475 strcat(str
, "Error");
479 if ((f
& ASL_FILTER_MASK_WARNING
) != 0)
481 if (i
> 0) strcat(str
, ", ");
482 strcat(str
, "Warning");
486 if ((f
& ASL_FILTER_MASK_NOTICE
) != 0)
488 if (i
> 0) strcat(str
, ", ");
489 strcat(str
, "Notice");
493 if ((f
& ASL_FILTER_MASK_INFO
) != 0)
495 if (i
> 0) strcat(str
, ", ");
500 if ((f
& ASL_FILTER_MASK_DEBUG
) != 0)
502 if (i
> 0) strcat(str
, ", ");
503 strcat(str
, "Debug");
507 if (i
== 0) sprintf(str
, "Off");
513 rcontrol_name(pid_t pid
, uid_t uid
)
515 static char str
[1024];
517 if (pid
== RC_MASTER
) return NOTIFY_SYSTEM_MASTER
;
519 memset(str
, 0, sizeof(str
));
520 if (uid
== 0) snprintf(str
, sizeof(str
) - 1, "%s.%d", NOTIFY_PREFIX_SYSTEM
, pid
);
521 else snprintf(str
, sizeof(str
) - 1, "user.uid.%d.syslog.%d", uid
, pid
);
526 rcontrol_get(pid_t pid
, uid_t uid
)
534 status
= rcontrol_get_string(rcontrol_name(pid
, uid
), &filter
);
535 if (status
== NOTIFY_STATUS_OK
)
537 printf("Master filter mask: %s\n", asl_filter_string(filter
));
541 printf("Unable to determine master filter mask\n");
545 status
= rcontrol_get_string(rcontrol_name(pid
, uid
), &filter
);
546 if (status
== NOTIFY_STATUS_OK
)
548 printf("Process %d syslog filter mask: %s\n", pid
, asl_filter_string(filter
));
552 printf("Unable to determine syslog filter mask for pid %d\n", pid
);
557 rcontrol_set(pid_t pid
, uid_t uid
, int filter
)
562 rcname
= rcontrol_name(pid
, uid
);
566 status
= rcontrol_set_string(rcname
, filter
);
568 if (status
== NOTIFY_STATUS_OK
)
570 if (pid
== RC_MASTER
) status
= notify_post(NOTIFY_SYSTEM_MASTER
);
574 printf("Unable to set master syslog filter mask: %s\n", notify_status_string(status
));
578 status
= rcontrol_set_string(rcname
, filter
);
579 if (status
== NOTIFY_STATUS_OK
)
581 status
= notify_post(rcname
);
585 printf("Unable to set syslog filter mask for pid %d: %s\n", pid
, notify_status_string(status
));
590 rsend(aslmsg msg
, char *rhost
)
598 struct sockaddr_in dst
;
600 char myname
[MAXHOSTNAMELEN
+ 1];
602 if (msg
== NULL
) return 0;
604 h
= gethostbyname(rhost
);
605 if (h
== NULL
) return -1;
607 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
608 if (s
<= 0) return -1;
610 memset(&dst
, 0, sizeof(struct sockaddr_in
));
611 memcpy(&(dst
.sin_addr
.s_addr
), h
->h_addr_list
[0], 4);
612 dst
.sin_family
= AF_INET
;
614 dst
.sin_len
= sizeof(struct sockaddr_in
);
616 level
= ASL_LEVEL_DEBUG
;
618 val
= asl_get(msg
, ASL_KEY_LEVEL
);
619 if (val
!= NULL
) level
= atoi(val
);
624 asprintf(×tr
, "%lu", tick
);
627 asl_set(msg
, ASL_KEY_TIME
, timestr
);
631 if (gethostname(myname
, MAXHOSTNAMELEN
) == 0) asl_set(msg
, ASL_KEY_HOST
, myname
);
634 str
= asl_msg_to_string((asl_msg_t
*)msg
, &len
);
635 if (str
== NULL
) return -1;
637 asprintf(&out
, "%10u %s\n", len
+1, str
);
639 if (out
== NULL
) return -1;
641 sendto(s
, out
, len
+12, 0, (const struct sockaddr
*)&dst
, sizeof(struct sockaddr_in
));
649 rlegacy(char *msg
, int level
, char *rhost
)
656 struct sockaddr_in dst
;
658 char myname
[MAXHOSTNAMELEN
+ 1];
660 if (msg
== NULL
) return 0;
662 h
= gethostbyname(rhost
);
663 if (h
== NULL
) return -1;
665 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
666 if (s
<= 0) return -1;
668 memset(&dst
, 0, sizeof(struct sockaddr_in
));
669 memcpy(&(dst
.sin_addr
.s_addr
), h
->h_addr_list
[0], 4);
670 dst
.sin_family
= AF_INET
;
672 dst
.sin_len
= sizeof(struct sockaddr_in
);
675 ltime
= ctime(&tick
);
678 gethostname(myname
, MAXHOSTNAMELEN
);
680 asprintf(&out
, "<%d>%s %s syslog[%d]: %s", level
, ltime
+4, myname
, getpid(), msg
);
682 sendto(s
, out
, len
, 0, (const struct sockaddr
*)&dst
, sizeof(struct sockaddr_in
));
694 if (s
== NULL
) return 0;
697 if ((s
[0] == '-') || (s
[0] == '+')) i
= 1;
699 if (s
[i
] == '\0') return 0;
701 for (; s
[i
] != '\0'; i
++)
703 if (!isdigit(s
[i
])) return 0;
710 asl_string_to_level(const char *s
)
712 if (s
== NULL
) return -1;
714 if ((s
[0] >= '0') && (s
[0] <= '7') && (s
[1] == '\0')) return atoi(s
);
716 if (!strncasecmp(s
, "em", 2)) return ASL_LEVEL_EMERG
;
717 else if (!strncasecmp(s
, "p", 1)) return ASL_LEVEL_EMERG
;
718 else if (!strncasecmp(s
, "a", 1)) return ASL_LEVEL_ALERT
;
719 else if (!strncasecmp(s
, "c", 1)) return ASL_LEVEL_CRIT
;
720 else if (!strncasecmp(s
, "er", 2)) return ASL_LEVEL_ERR
;
721 else if (!strncasecmp(s
, "x", 1)) return ASL_LEVEL_ERR
;
722 else if (!strncasecmp(s
, "w", 1)) return ASL_LEVEL_WARNING
;
723 else if (!strncasecmp(s
, "n", 1)) return ASL_LEVEL_NOTICE
;
724 else if (!strncasecmp(s
, "i", 1)) return ASL_LEVEL_INFO
;
725 else if (!strncasecmp(s
, "d", 1)) return ASL_LEVEL_DEBUG
;
731 asl_string_to_char_level(const char *s
)
733 if (s
== NULL
) return NULL
;
735 if ((s
[0] >= '0') && (s
[0] <= '7') && (s
[1] == '\0')) return s
;
737 if (!strncasecmp(s
, "em", 2)) return "0";
738 else if (!strncasecmp(s
, "p", 1)) return "0";
739 else if (!strncasecmp(s
, "a", 1)) return "1";
740 else if (!strncasecmp(s
, "c", 1)) return "2";
741 else if (!strncasecmp(s
, "er", 2)) return "3";
742 else if (!strncasecmp(s
, "x", 1)) return "3";
743 else if (!strncasecmp(s
, "w", 1)) return "4";
744 else if (!strncasecmp(s
, "n", 1)) return "5";
745 else if (!strncasecmp(s
, "i", 1)) return "6";
746 else if (!strncasecmp(s
, "d", 1)) return "7";
752 syslog_remote_control(int argc
, char *argv
[])
754 int pid
, uid
, status
, mask
;
756 if ((argc
< 3) || (argc
> 4))
758 fprintf(stderr
, "usage:\n");
759 fprintf(stderr
, "%s -c process [mask]\n", myname
);
760 fprintf(stderr
, " get (set if mask is specified) syslog filter mask for process (pid or name)\n");
761 fprintf(stderr
, " process may be pid or process name\n");
762 fprintf(stderr
, " use \"-c 0\" to get master syslog filter mask\n");
763 fprintf(stderr
, " use \"-c 0 off\" to disable master syslog filter mask\n");
764 fprintf(stderr
, "\n");
771 status
= PROC_NOT_FOUND
;
773 if ((!strcmp(argv
[2], "syslogd")) || (!strcmp(argv
[2], "syslog")))
775 fprintf(stderr
, "%s: does not have a filter mask\n", argv
[2]);
778 else if (_isanumber(argv
[2]) != 0)
781 status
= procinfo(NULL
, &pid
, &uid
);
785 status
= procinfo(argv
[2], &pid
, &uid
);
788 if (status
== PROC_NOT_FOUND
)
790 fprintf(stderr
, "%s: process not found\n", argv
[2]);
794 if (status
== PROC_NOT_UNIQUE
)
796 fprintf(stderr
, "%s: multiple processes found\n", argv
[2]);
797 fprintf(stderr
, "use pid to identify a process uniquely\n");
801 if (pid
== 0) pid
= RC_MASTER
;
805 if ((pid
== RC_MASTER
) && (!strcasecmp(argv
[3], "off"))) mask
= 0;
808 mask
= asl_string_to_filter(argv
[3]);
811 printf("unknown syslog mask: %s\n", argv
[3]);
816 rcontrol_set(pid
, uid
, mask
);
820 rcontrol_get(pid
, uid
);
827 syslog_send(int argc
, char *argv
[])
829 int i
, start
, kv
, len
, rfmt
, rlevel
;
832 char tmp
[64], *str
, *rhost
;
836 rfmt
= SEND_FORMAT_LEGACY
;
840 for (i
= 1; i
< argc
; i
++)
842 if (!strcmp(argv
[i
], "-s")) start
= i
+1;
843 else if (!strcmp(argv
[i
], "-k"))
846 rfmt
= SEND_FORMAT_ASL
;
848 else if (!strcmp(argv
[i
], "-r"))
853 else if (!strcmp(argv
[i
], "-l"))
855 rlevel
= asl_string_to_level(argv
[++i
]);
858 fprintf(stderr
, "Unknown level: %s\n", argv
[i
]);
865 asl
= asl_open(myname
, "syslog", 0);
866 asl_set_filter(asl
, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG
));
868 m
= asl_new(ASL_TYPE_MSG
);
869 asl_set(m
, ASL_KEY_SENDER
, myname
);
871 sprintf(tmp
, "%d", rlevel
);
872 asl_set(m
, ASL_KEY_LEVEL
, tmp
);
879 for (i
= start
; i
< argc
; i
++) len
+= (strlen(argv
[i
]) + 1);
880 str
= calloc(len
+ 1, 1);
881 if (str
== NULL
) return -1;
883 for (i
= start
; i
< argc
; i
++)
885 strcat(str
, argv
[i
]);
886 if ((i
+1) < argc
) strcat(str
, " ");
888 asl_set(m
, ASL_KEY_MSG
, str
);
892 for (i
= start
+ 1; i
< argc
; i
+= 2)
894 if (!strcmp(argv
[i
], "-k")) i
++;
895 asl_set(m
, argv
[i
], argv
[i
+ 1]);
896 if (!strcmp(argv
[i
], ASL_KEY_LEVEL
)) rlevel
= atoi(argv
[i
+ 1]);
904 else if (rfmt
== SEND_FORMAT_ASL
)
908 else if ((rfmt
== SEND_FORMAT_LEGACY
) && (str
!= NULL
))
910 rlegacy(str
, rlevel
, rhost
);
915 if (str
!= NULL
) free(str
);
923 syslog_config(int argc
, char *argv
[])
934 fprintf(stderr
, "syslogd parameters may only be set by the superuser\n");
938 str
= asl_string_new(0);
939 asl_string_append(str
, "= ");
941 for (i
= 2; i
< argc
; i
++)
943 asl_string_append(str
, argv
[i
]);
944 if ((i
+ 1) < argc
) asl_string_append(str
, " ");
947 asl
= asl_open(myname
, "syslog", 0);
949 m
= asl_new(ASL_TYPE_MSG
);
950 asl_set(m
, ASL_KEY_LEVEL
, ASL_STRING_NOTICE
);
951 asl_set(m
, ASL_KEY_OPTION
, ASL_OPT_CONTROL
);
952 asl_set(m
, ASL_KEY_SENDER
, myname
);
953 asl_set(m
, ASL_KEY_MSG
, asl_string_bytes(str
));
957 asl_string_free(str
);
965 print_xml_header(FILE *f
)
967 if (f
== NULL
) return;
969 fprintf(f
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
970 fprintf(f
, "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n");
971 fprintf(f
, "<plist version=\"1.0\">\n");
972 fprintf(f
, "<array>\n");
976 print_xml_trailer(FILE *f
)
978 if (f
== NULL
) return;
980 fprintf(f
, "</array>\n");
981 fprintf(f
, "</plist>\n");
985 printmsg(FILE *f
, aslmsg msg
, char *fmt
, int pflags
)
989 uint32_t encode
, len
, status
;
997 status
= asl_file_save(export
, msg
, &xid
);
998 if (status
!= ASL_STATUS_OK
)
1000 fprintf(stderr
, "export file write failed: %s\n", asl_core_error(status
));
1001 asl_file_close(export
);
1009 encode
= pflags
& 0x0000000f;
1011 mf
= ASL_MSG_FMT_RAW
;
1012 if (fmt
!= NULL
) mf
= (const char *)fmt
;
1013 else if (pflags
& FORMAT_STD
) mf
= ASL_MSG_FMT_STD
;
1014 else if (pflags
& FORMAT_LEGACY
) mf
= ASL_MSG_FMT_BSD
;
1015 else if (pflags
& FORMAT_XML
) mf
= ASL_MSG_FMT_XML
;
1018 str
= asl_format_message((asl_msg_t
*)msg
, mf
, tfmt
, encode
, &len
);
1019 if (str
== NULL
) return;
1021 if (pflags
& COMPRESS_DUPS
)
1023 if (last_printmsg_str
!= NULL
)
1025 if (!strcmp(str
+ STD_BSD_DATE_LEN
, last_printmsg_str
+ STD_BSD_DATE_LEN
))
1027 last_printmsg_count
++;
1032 if (last_printmsg_count
> 0)
1034 fprintf(f
, "--- last message repeated %d time%s ---\n", last_printmsg_count
, (last_printmsg_count
== 1) ? "" : "s");
1037 free(last_printmsg_str
);
1038 last_printmsg_str
= str
;
1039 last_printmsg_count
= 0;
1041 fprintf(f
, "%s", str
);
1046 last_printmsg_str
= str
;
1047 last_printmsg_count
= 0;
1049 fprintf(f
, "%s", str
);
1054 fprintf(f
, "%s", str
);
1059 asl_search_result_t
*
1060 store_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1063 asl_search_result_t
*res
;
1067 status
= asl_store_open_read(NULL
, &store
);
1068 if (status
!= 0) return NULL
;
1072 status
= asl_store_match(store
, q
, &res
, last
, start
, count
, dir
);
1073 if (status
!= 0) return NULL
;
1078 asl_search_result_t
*
1079 file_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1082 asl_search_result_t
*res
;
1085 status
= asl_file_list_match(db_files
, q
, &res
, last
, start
, count
, dir
);
1086 if (status
!= 0) return NULL
;
1091 asl_search_result_t
*
1092 legacy_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1095 asl_search_result_t
*res
;
1098 status
= asl_file_match(legacy
, q
, &res
, last
, start
, count
, dir
);
1099 if (status
!= 0) return NULL
;
1104 asl_search_result_t
*
1105 syslogd_query(asl_search_result_t
*q
, uint64_t start
, int count
, int dir
, uint64_t *last
)
1109 uint32_t len
, reslen
, status
;
1111 kern_return_t kstatus
;
1112 security_token_t sec
;
1113 asl_search_result_t
*l
;
1115 if (asl_server_port
== MACH_PORT_NULL
)
1117 kstatus
= bootstrap_look_up(bootstrap_port
, ASL_SERVICE_NAME
, &asl_server_port
);
1118 if (kstatus
!= KERN_SUCCESS
)
1120 fprintf(stderr
, "query failed: can't contact syslogd\n");
1126 str
= asl_list_to_string(q
, &len
);
1128 kstatus
= vm_allocate(mach_task_self(), (vm_address_t
*)&vmstr
, len
, TRUE
);
1129 if (kstatus
!= KERN_SUCCESS
)
1135 memmove(vmstr
, str
, len
);
1144 if (dir
< 0) flags
= QUERY_FLAG_SEARCH_REVERSE
;
1146 kstatus
= _asl_server_query(asl_server_port
, (caddr_t
)vmstr
, len
, start
, count
, flags
, (caddr_t
*)&res
, &reslen
, last
, (int *)&status
, &sec
);
1148 if (res
== NULL
) return NULL
;
1149 l
= asl_list_from_string(res
);
1150 vm_deallocate(mach_task_self(), (vm_address_t
)res
, reslen
);
1155 filter_and_print(aslmsg msg
, asl_search_result_t
*ql
, FILE *f
, char *pfmt
, int pflags
)
1157 int i
, do_match
, did_match
;
1159 if (msg
== NULL
) return;
1162 if (ql
== NULL
) do_match
= 0;
1163 else if (ql
->count
== 0) do_match
= 0;
1171 for (i
= 0; (i
< ql
->count
) && (did_match
== 0); i
++)
1173 did_match
= asl_msg_cmp(ql
->msg
[i
], (asl_msg_t
*)msg
);
1177 if (did_match
!= 0) printmsg(f
, msg
, pfmt
, pflags
);
1180 #ifdef CONFIG_IPHONE
1182 syslogd_direct_watch(FILE *f
, char *pfmt
, int pflags
, asl_search_result_t
*ql
)
1184 struct sockaddr_in address
;
1185 int i
, bytes
, sock
, stream
, status
;
1188 socklen_t addresslength
;
1189 char *str
, buf
[DIRECT_BUF_SIZE
];
1192 if (asl_server_port
== MACH_PORT_NULL
)
1194 status
= bootstrap_look_up(bootstrap_port
, ASL_SERVICE_NAME
, &asl_server_port
);
1195 if (status
!= KERN_SUCCESS
)
1197 fprintf(stderr
, "query failed: can't contact syslogd\n");
1202 addresslength
= sizeof(address
);
1203 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
1204 port
= (arc4random() % (IPPORT_HILASTAUTO
- IPPORT_HIFIRSTAUTO
)) + IPPORT_HIFIRSTAUTO
;
1206 memset(&address
, 0, addresslength
);
1207 address
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1208 address
.sin_family
= AF_INET
;
1209 address
.sin_port
= htons(port
);
1211 status
= bind(sock
, (struct sockaddr
*)&address
, sizeof(address
));
1213 for (i
= 0; (i
< MAX_RANDOM
) && (status
< 0); i
++)
1215 port
= (arc4random() % (IPPORT_HILASTAUTO
- IPPORT_HIFIRSTAUTO
)) + IPPORT_HIFIRSTAUTO
;
1216 address
.sin_port
= htons(port
);
1218 status
= bind(sock
, (struct sockaddr
*)&address
, sizeof(address
));
1223 fprintf(stderr
, "query failed: can't find a port to connect to syslogd\n");
1229 if (listen(sock
, 1) == -1)
1236 _asl_server_register_direct_watch(asl_server_port
, i
);
1238 stream
= accept(sock
, (struct sockaddr
*)&address
, &addresslength
);
1249 bytes
= recvfrom(stream
, &n
, sizeof(n
), 0, NULL
, NULL
);
1252 fprintf(stderr
, "\nrecvfrom (message length) returned %d (errno %d) - exiting\n", bytes
, errno
);
1255 else inlen
= ntohl(n
);
1257 if (inlen
== 0) continue;
1260 if (inlen
<= DIRECT_BUF_SIZE
)
1266 str
= calloc(1, inlen
+ 1);
1269 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
1280 bytes
= recvfrom(stream
, str
+ n
, inlen
- n
, 0, NULL
, NULL
);
1283 fprintf(stderr
, "\nrecvfrom (message body) returned %d (errno %d) at length %d of %d - exiting\n", bytes
, errno
, n
, inlen
);
1291 fprintf(stderr
, "\ntruncated message: expected %d bytes received %d bytes\n", inlen
, n
);
1297 msg
= (aslmsg
)asl_msg_from_string(str
);
1298 if (str
!= buf
) free(str
);
1299 filter_and_print(msg
, ql
, f
, pfmt
, pflags
);
1306 address
.sin_addr
.s_addr
= 0;
1311 sort_compare_key(const void *a
, const void *b
, const char *key
)
1314 const char *va
, *vb
;
1317 if (key
== NULL
) return 0;
1322 va
= asl_get(*ma
, key
);
1323 vb
= asl_get(*mb
, key
);
1325 if (va
== NULL
) return -1;
1326 if (vb
== NULL
) return 1;
1328 if (sort_numeric
== 1)
1332 if (na
< nb
) return -1;
1333 if (na
> nb
) return 1;
1337 return strcmp(va
, vb
);
1341 sort_compare(const void *a
, const void *b
)
1345 if (sort_key
== NULL
) return 0;
1347 cmp
= sort_compare_key(a
, b
, sort_key
);
1348 if ((cmp
== 0) && (sort_key_2
!= NULL
)) cmp
= sort_compare_key(a
, b
, sort_key_2
);
1354 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
)
1356 asl_search_result_t
*res
;
1359 if (pflags
& FORMAT_XML
) print_xml_header(f
);
1367 if (batch
== 0) more
= 0;
1369 if ((dbselect
== DB_SELECT_ASL
) || (dbselect
== DB_SELECT_STORE
)) res
= store_query(ql
, qmin
, batch
, dir
, cmax
);
1370 else if (dbselect
== DB_SELECT_FILES
) res
= file_query(ql
, qmin
, batch
, dir
, cmax
);
1371 else if (dbselect
== DB_SELECT_SYSLOGD
) res
= syslogd_query(ql
, qmin
, batch
, dir
, cmax
);
1372 else if (dbselect
== DB_SELECT_LEGACY
) res
= legacy_query(ql
, qmin
, batch
, dir
, cmax
);
1374 if ((dir
>= 0) && (*cmax
> qmin
)) qmin
= *cmax
+ 1;
1375 else if ((dir
< 0) && (*cmax
< qmin
)) qmin
= *cmax
- 1;
1383 if ((batch
> 0) && (res
->count
< batch
)) more
= 0;
1384 total
+= res
->count
;
1385 if ((count
> 0) && (total
>= count
)) more
= 0;
1390 i
= res
->count
- tail
;
1395 if (sort_key
!= NULL
)
1397 qsort(res
->msg
, res
->count
, sizeof(asl_msg_t
*), sort_compare
);
1400 if ((f
!= NULL
) || (export
!= NULL
))
1402 for (; i
< res
->count
; i
++) printmsg(f
, (aslmsg
)(res
->msg
[i
]), pfmt
, pflags
);
1405 aslresponse_free((aslresponse
)res
);
1409 if ((pflags
& COMPRESS_DUPS
) && (last_printmsg_count
> 0))
1411 fprintf(f
, "--- last message repeated %d time%s ---\n", last_printmsg_count
, (last_printmsg_count
== 1) ? "" : "s");
1412 free(last_printmsg_str
);
1413 last_printmsg_str
= NULL
;
1414 last_printmsg_count
= 0;
1417 if (pflags
& FORMAT_XML
) print_xml_trailer(f
);
1425 op
= ASL_QUERY_OP_NULL
;
1427 if (o
== NULL
) return op
;
1429 for (i
= 0; o
[i
] != '\0'; i
++)
1431 if (o
[i
] == MOD_CASE_FOLD
) op
|= ASL_QUERY_OP_CASEFOLD
;
1432 else if (o
[i
] == MOD_REGEX
) op
|= ASL_QUERY_OP_REGEX
;
1433 else if (o
[i
] == MOD_NUMERIC
) op
|= ASL_QUERY_OP_NUMERIC
;
1434 else if (o
[i
] == MOD_SUBSTRING
) op
|= ASL_QUERY_OP_SUBSTRING
;
1435 else if (o
[i
] == MOD_PREFIX
) op
|= ASL_QUERY_OP_PREFIX
;
1436 else if (o
[i
] == MOD_SUFFIX
) op
|= ASL_QUERY_OP_SUFFIX
;
1438 else if (!strncasecmp(o
+i
, OP_EQ
, sizeof(OP_EQ
)))
1440 op
|= ASL_QUERY_OP_EQUAL
;
1441 i
+= (sizeof(OP_EQ
) - 2);
1443 else if (!strncasecmp(o
+i
, OP_NE
, sizeof(OP_NE
)))
1445 op
|= ASL_QUERY_OP_NOT_EQUAL
;
1446 i
+= (sizeof(OP_NE
) - 2);
1448 else if (!strncasecmp(o
+i
, OP_GT
, sizeof(OP_GT
)))
1450 op
|= ASL_QUERY_OP_GREATER
;
1451 i
+= (sizeof(OP_GT
) - 2);
1453 else if (!strncasecmp(o
+i
, OP_GE
, sizeof(OP_GE
)))
1455 op
|= ASL_QUERY_OP_GREATER_EQUAL
;
1456 i
+= (sizeof(OP_GE
) - 2);
1458 else if (!strncasecmp(o
+i
, OP_LT
, sizeof(OP_LT
)))
1460 op
|= ASL_QUERY_OP_LESS
;
1461 i
+= (sizeof(OP_LT
) - 2);
1463 else if (!strncasecmp(o
+i
, OP_LE
, sizeof(OP_LE
)))
1465 op
|= ASL_QUERY_OP_LESS_EQUAL
;
1466 i
+= (sizeof(OP_LE
) - 2);
1470 fprintf(stderr
, "invalid option: %s\n", o
);
1476 if (op
& ASL_QUERY_OP_NUMERIC
)
1478 if (op
& ASL_QUERY_OP_CASEFOLD
)
1480 fprintf(stderr
, "warning: case fold modifier has no effect with numeric comparisons\n");
1481 op
&= ~ASL_QUERY_OP_CASEFOLD
;
1484 if (op
& ASL_QUERY_OP_REGEX
)
1486 fprintf(stderr
, "warning: regex modifier has no effect with numeric comparisons\n");
1487 op
&= ~ASL_QUERY_OP_REGEX
;
1490 if (op
& ASL_QUERY_OP_SUBSTRING
)
1492 fprintf(stderr
, "warning: substring modifier has no effect with numeric comparisons\n");
1493 op
&= ~ASL_QUERY_OP_SUBSTRING
;
1496 if (op
& ASL_QUERY_OP_PREFIX
)
1498 fprintf(stderr
, "warning: prefix modifier has no effect with numeric comparisons\n");
1499 op
&= ~ASL_QUERY_OP_PREFIX
;
1502 if (op
& ASL_QUERY_OP_SUFFIX
)
1504 fprintf(stderr
, "warning: suffix modifier has no effect with numeric comparisons\n");
1505 op
&= ~ASL_QUERY_OP_SUFFIX
;
1509 if (op
& ASL_QUERY_OP_REGEX
)
1511 if (op
& ASL_QUERY_OP_SUBSTRING
)
1513 fprintf(stderr
, "warning: substring modifier has no effect with regular expression comparisons\n");
1514 op
&= ~ASL_QUERY_OP_SUBSTRING
;
1517 if (op
& ASL_QUERY_OP_PREFIX
)
1519 fprintf(stderr
, "warning: prefix modifier has no effect with regular expression comparisons\n");
1520 op
&= ~ASL_QUERY_OP_PREFIX
;
1523 if (op
& ASL_QUERY_OP_SUFFIX
)
1525 fprintf(stderr
, "warning: suffix modifier has no effect with regular expression comparisons\n");
1526 op
&= ~ASL_QUERY_OP_SUFFIX
;
1534 add_op(asl_msg_t
*q
, char *key
, char *op
, char *val
, uint32_t flags
)
1539 if (key
== NULL
) return -1;
1540 if (q
== NULL
) return -1;
1543 if (strcmp(key
, ASL_KEY_TIME
) == 0)
1545 qval
= (const char *)val
;
1547 else if ((strcmp(key
, ASL_KEY_LEVEL
) == 0) && (_isanumber(val
) == 0))
1549 /* Convert level strings to numeric values */
1550 qval
= asl_string_to_char_level(val
);
1553 fprintf(stderr
, "invalid value for \"Level\"key: %s\n", val
);
1558 o
= ASL_QUERY_OP_NULL
;
1559 if (val
== NULL
) o
= ASL_QUERY_OP_TRUE
;
1564 if (o
== ASL_QUERY_OP_NULL
) return -1;
1567 fprintf(stderr
, "no value supplied for operator %s %s\n", key
, op
);
1571 if ((qval
== NULL
) && (o
& ASL_QUERY_OP_NUMERIC
) && (_isanumber(val
) == 0))
1573 fprintf(stderr
, "non-numeric value supplied for numeric operator %s %s %s\n", key
, op
, val
);
1579 if (qval
!= NULL
) asl_msg_set_key_val_op(q
, key
, qval
, o
);
1580 else asl_msg_set_key_val_op(q
, key
, val
, o
);
1586 add_db_file(const char *name
)
1591 if (dbselect
== DB_SELECT_LEGACY
)
1593 fprintf(stderr
, "syslog can only read one legacy format database\n");
1594 fprintf(stderr
, "can't combine legacy and non-legacy databases in a single search\n");
1598 /* shouldn't happen */
1599 if (name
== NULL
) return DB_SELECT_ASL
;
1602 status
= asl_file_open_read(name
, &s
);
1603 if (status
!= ASL_STATUS_OK
)
1605 fprintf(stderr
, "data store file %s open failed: %s \n", name
, asl_core_error(status
));
1611 fprintf(stderr
, "data store file %s open failed\n", name
);
1615 if (s
->flags
& ASL_FILE_FLAG_LEGACY_STORE
)
1617 if (db_files
!= NULL
)
1619 fprintf(stderr
, "syslog can only read a single legacy format database\n");
1620 fprintf(stderr
, "can't combine legacy and non-legacy databases in a single search\n");
1625 return DB_SELECT_LEGACY
;
1628 db_files
= asl_file_list_add(db_files
, s
);
1629 return DB_SELECT_FILES
;
1633 add_db_dir(const char *name
)
1636 struct dirent
*dent
;
1642 * Try opening as a data store
1644 status
= asl_store_open_read(name
, &store
);
1647 if (name
== NULL
) return DB_SELECT_ASL
;
1648 if (!strcmp(name
, PATH_ASL_STORE
)) return DB_SELECT_ASL
;
1649 return DB_SELECT_STORE
;
1653 * Open all readable files
1658 fprintf(stderr
, "%s: %s\n", name
, strerror(errno
));
1662 while ((dent
= readdir(dp
)) != NULL
)
1664 if (dent
->d_name
[0] == '.') continue;
1667 asprintf(&path
, "%s/%s", name
, dent
->d_name
);
1670 * asl_file_open_read will fail if path is NULL,
1671 * if the file is not an ASL store file,
1672 * or if it isn't readable.
1675 status
= asl_file_open_read(path
, &s
);
1676 if (path
!= NULL
) free(path
);
1677 if ((status
!= ASL_STATUS_OK
) || (s
== NULL
)) continue;
1679 db_files
= asl_file_list_add(db_files
, s
);
1684 return DB_SELECT_FILES
;
1688 main(int argc
, char *argv
[])
1691 int i
, j
, n
, watch
, status
, pflags
, iamroot
, user_tflag
, export_preserve_id
, saw_dash_d
, since_boot
;
1692 int notify_file
, notify_token
;
1693 asl_search_result_t
*qlist
;
1696 const char *exportname
;
1697 uint32_t flags
, tail_count
, batch
, encode
;
1698 uint64_t qmin
, cmax
;
1706 batch
= FETCH_BATCH
;
1707 pflags
= FORMAT_STD
| COMPRESS_DUPS
;
1708 encode
= ASL_ENCODE_SAFE
;
1711 export_preserve_id
= 0;
1715 i
= asl_store_location();
1716 if (i
== ASL_STORE_LOCATION_MEMORY
) dbselect
= DB_SELECT_SYSLOGD
;
1718 if (getuid() == 0) iamroot
= 1;
1720 for (i
= 1; i
< argc
; i
++)
1722 if ((!strcmp(argv
[i
], "-help")) || (!strcmp(argv
[i
], "--help")))
1728 if ((!strcmp(argv
[i
], "-time")) || (!strcmp(argv
[i
], "--time")))
1731 printf("%llu\n", qmin
);
1735 if ((!strcmp(argv
[i
], "-config")) || (!strcmp(argv
[i
], "--config")))
1737 syslog_config(argc
, argv
);
1741 if (!strcmp(argv
[i
], "-s"))
1743 syslog_send(argc
, argv
);
1747 if (!strcmp(argv
[i
], "-c"))
1749 syslog_remote_control(argc
, argv
);
1754 qlist
= (asl_search_result_t
*)calloc(1, sizeof(asl_search_result_t
));
1755 if (qlist
== NULL
) exit(1);
1757 for (i
= 1; i
< argc
; i
++)
1759 if (!strcmp(argv
[i
], "-f"))
1763 for (j
= i
+ 1; j
< argc
; j
++)
1765 if (!strcmp(argv
[j
], "-"))
1767 dbselect
= DB_SELECT_SYSLOGD
;
1771 else if (argv
[j
][0] == '-')
1777 dbselect
= add_db_file(argv
[j
]);
1783 else if (!strcmp(argv
[i
], "-d"))
1787 if (saw_dash_d
< argc
)
1789 for (j
= saw_dash_d
; j
< argc
; j
++)
1791 if (!strcmp(argv
[j
], "store"))
1793 dbselect
= add_db_dir(PATH_ASL_STORE
);
1796 else if (!strcmp(argv
[j
], "archive"))
1798 dbselect
= add_db_dir(PATH_ASL_ARCHIVE
);
1801 else if (argv
[j
][0] == '-')
1807 dbselect
= add_db_dir(argv
[j
]);
1814 fprintf(stderr
, "missing directory name following -d flag\n");
1818 else if (!strcmp(argv
[i
], "-b"))
1820 batch
= atoi(argv
[++i
]);
1822 else if (!strcmp(argv
[i
], "-B"))
1826 else if (!strcmp(argv
[i
], "-w"))
1830 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1833 if (!strcmp(argv
[i
], "all"))
1835 tail_count
= (uint32_t)-1;
1837 else if (!strcmp(argv
[i
], "boot"))
1843 tail_count
= atoi(argv
[i
]);
1847 else if (!strcmp(argv
[i
], "-sort"))
1849 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1854 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1857 sort_key_2
= argv
[i
];
1862 sort_key
= ASL_KEY_MSG_ID
;
1867 else if (!strcmp(argv
[i
], "-nsort"))
1869 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1874 if (((i
+ 1) < argc
) && (argv
[i
+ 1][0] != '-'))
1877 sort_key_2
= argv
[i
];
1882 sort_key
= ASL_KEY_MSG_ID
;
1888 else if (!strcmp(argv
[i
], "-u"))
1893 else if ((!strcmp(argv
[i
], "-x")) || (!strcmp(argv
[i
], "-X")))
1895 if ((i
+ 1) >= argc
)
1897 aslresponse_free(qlist
);
1902 if (!strcmp(argv
[i
], "-x")) export_preserve_id
= 1;
1904 exportname
= argv
[++i
];
1906 else if (!strcmp(argv
[i
], "-E"))
1908 if ((i
+ 1) >= argc
)
1910 aslresponse_free(qlist
);
1917 if (!strcmp(argv
[i
], "vis")) encode
= ASL_ENCODE_ASL
;
1918 else if (!strcmp(argv
[i
], "safe")) encode
= ASL_ENCODE_SAFE
;
1919 else if (!strcmp(argv
[i
], "none")) encode
= ASL_ENCODE_NONE
;
1920 else if ((argv
[i
][0] >= '0') && (argv
[i
][0] <= '9') && (argv
[i
][1] == '\0')) encode
= atoi(argv
[i
]);
1922 else if (!strcmp(argv
[i
], "-F"))
1924 if ((i
+ 1) >= argc
)
1926 aslresponse_free(qlist
);
1933 if (!strcmp(argv
[i
], "raw"))
1935 pflags
= FORMAT_RAW
;
1936 if (user_tflag
== 0) tfmt
= "sec";
1938 else if (!strcmp(argv
[i
], "std"))
1940 pflags
= FORMAT_STD
| COMPRESS_DUPS
;
1942 else if (!strcmp(argv
[i
], "bsd"))
1944 pflags
= FORMAT_LEGACY
| COMPRESS_DUPS
;
1946 else if (!strcmp(argv
[i
], "xml"))
1948 pflags
= FORMAT_XML
;
1956 else if (!strcmp(argv
[i
], "-T"))
1958 if ((i
+ 1) >= argc
)
1960 aslresponse_free(qlist
);
1969 else if (!strcmp(argv
[i
], "-nodc"))
1971 pflags
= pflags
& ~COMPRESS_DUPS
;
1973 else if (!strcmp(argv
[i
], "-o"))
1977 if (qlist
->count
== 0)
1979 qlist
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
1983 qlist
->msg
= (asl_msg_t
**)reallocf(qlist
->msg
, (qlist
->count
+ 1) * sizeof(asl_msg_t
*));
1986 if (qlist
->msg
== NULL
) exit(1);
1988 cq
= asl_msg_new(ASL_TYPE_QUERY
);
1989 qlist
->msg
[qlist
->count
] = cq
;
1992 else if (!strcmp(argv
[i
], "-n"))
1994 flags
= ASL_QUERY_OP_NOT
;
1996 else if (!strcmp(argv
[i
], "-C"))
1998 if (qlist
->count
== 0)
2000 qlist
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
2001 if (qlist
->msg
== NULL
) exit(1);
2003 cq
= asl_msg_new(ASL_TYPE_QUERY
);
2004 qlist
->msg
[qlist
->count
] = cq
;
2008 status
= add_op(cq
, ASL_KEY_FACILITY
, OP_EQ
, FACILITY_CONSOLE
, flags
);
2013 aslresponse_free(qlist
);
2017 else if (!strcmp(argv
[i
], "-k"))
2020 for (n
= i
; n
< argc
; n
++)
2022 if (!strcmp(argv
[n
], "-o")) break;
2023 if (!strcmp(argv
[n
], "-n")) break;
2024 if (!strcmp(argv
[n
], "-k")) break;
2027 fprintf(stderr
, "invalid sequence: -k");
2028 for (j
= i
; j
<= n
; j
++) fprintf(stderr
, " %s", argv
[j
]);
2029 fprintf(stderr
, "\n");
2042 if (qlist
->count
== 0)
2044 qlist
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
2045 if (qlist
->msg
== NULL
) exit(1);
2047 cq
= asl_msg_new(ASL_TYPE_QUERY
);
2048 qlist
->msg
[qlist
->count
] = cq
;
2053 if (n
== 1) status
= add_op(cq
, argv
[i
], NULL
, NULL
, flags
);
2054 else if (n
== 2) status
= add_op(cq
, argv
[i
], OP_EQ
, argv
[i
+1], flags
);
2055 else status
= add_op(cq
, argv
[i
], argv
[i
+1], argv
[i
+2], flags
);
2060 aslresponse_free(qlist
);
2068 fprintf(stderr
, "syslog: unknown option \"%s\"\n", argv
[i
]);
2069 fprintf(stderr
, "run \"syslog -help\" for usage\n");
2079 * Catch and report some cases where watch (-w) doesn't work
2083 if (sort_key
!= NULL
)
2085 fprintf(stderr
, "Warning: -w flag has no effect with -sort flag\n");
2089 if (dbselect
== DB_SELECT_FILES
)
2091 if (saw_dash_d
== 0)
2093 fprintf(stderr
, "Warning: -w flag not supported for a set of one or more files\n");
2097 fprintf(stderr
, "Warning: directory \"%s\" is not an ASL data store\n", argv
[saw_dash_d
]);
2098 fprintf(stderr
, " -w flag not supported for a set of one or more files\n");
2105 if (exportname
!= NULL
)
2109 fprintf(stderr
, "Warning: -w flag has no effect with -x export flag\n");
2113 status
= asl_file_open_write(exportname
, 0644, -1, -1, &export
);
2114 if (status
!= ASL_STATUS_OK
)
2116 aslresponse_free(qlist
);
2117 fprintf(stderr
, "export file open failed: %s\n", asl_core_error(status
));
2122 * allow the string cache to be unlimited to maximize string dup compression
2123 * preserve message IDs
2125 export
->flags
= ASL_FILE_FLAG_UNLIMITED_CACHE
;
2126 if (export_preserve_id
!= 0) export
->flags
|= ASL_FILE_FLAG_PRESERVE_MSG_ID
;
2137 /* set starting point */
2138 if (since_boot
== 1)
2140 /* search back for last "BOOT_TIME (ut_type == 2) record */
2141 asl_search_result_t
*bt
;
2144 bt
= (asl_search_result_t
*)calloc(1, sizeof(asl_search_result_t
));
2147 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
2151 bt
->msg
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
2152 if (bt
->msg
== NULL
)
2154 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
2158 bq
= asl_new(ASL_TYPE_QUERY
);
2161 fprintf(stderr
, "\ncan't allocate memory - exiting\n");
2165 asl_set_query(bq
, "ut_type", "2", ASL_QUERY_OP_EQUAL
);
2167 bt
->msg
[0] = (asl_msg_t
*)bq
;
2170 search_once(NULL
, NULL
, 0, bt
, -1, &qmin
, 1, 1, -1, 0);
2175 if (qmin
> 0) qmin
--;
2178 else if (watch
== 1)
2180 /* go back tail_count records from last record */
2182 search_once(NULL
, NULL
, 0, qlist
, qmin
, &cmax
, 1, 1, -1, 0);
2184 if (cmax
>= tail_count
) qmin
= cmax
- tail_count
;
2190 if ((watch
== 1) && (dbselect
== DB_SELECT_ASL
))
2192 status
= notify_register_file_descriptor("com.apple.system.logger.message", ¬ify_file
, 0, ¬ify_token
);
2193 if (status
!= NOTIFY_STATUS_OK
) notify_token
= -1;
2196 /* output should be line buffered */
2197 if (outfile
!= NULL
) setlinebuf(outfile
);
2199 search_once(outfile
, pfmt
, pflags
, qlist
, qmin
+ 1, &cmax
, 0, 0, 1, tail_count
);
2203 if (dbselect
== DB_SELECT_SYSLOGD
)
2205 #ifdef CONFIG_IPHONE
2206 syslogd_direct_watch(outfile
, pfmt
, pflags
, qlist
);
2208 fprintf(stderr
, "Warning: -w flag cannot be used when querying syslogd directly\n");
2212 else if (notify_token
== -1)
2217 if (cmax
> qmin
) qmin
= cmax
;
2218 search_once(outfile
, pfmt
, pflags
, qlist
, qmin
+ 1, &cmax
, 0, batch
, 1, 0);
2223 while (read(notify_file
, &i
, 4) == 4)
2225 if (cmax
> qmin
) qmin
= cmax
;
2226 search_once(outfile
, pfmt
, pflags
, qlist
, qmin
+ 1, &cmax
, 0, batch
, 1, 0);
2231 if (db_files
!= NULL
) asl_file_list_close(db_files
);
2232 if (store
!= NULL
) asl_store_close(store
);
2233 if (export
!= NULL
) asl_file_close(export
);
2235 aslresponse_free(qlist
);