2 * Copyright (c) 2004 Apple Computer, 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@
31 #include <sys/socket.h>
32 #include <sys/sysctl.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
38 #include <asl_private.h>
40 #define MOD_CASE_FOLD 'C'
42 #define MOD_SUBSTRING 'S'
43 #define MOD_PREFIX 'A'
44 #define MOD_SUFFIX 'Z'
45 #define MOD_NUMERIC 'N'
54 #define ASL_QUERY_OP_NOT 0x1000
58 #define SEARCH_MATCH 1
60 #define PROC_NOT_FOUND -1
61 #define PROC_NOT_UNIQUE -2
67 #define forever for(;;)
69 #define SEND_FORMAT_LEGACY 0
70 #define SEND_FORMAT_ASL 1
72 #define PRINT_LOCALTIME 0x00000001
73 #define PRINT_LEGACY_FMT 0x00000002
74 #define PRINT_STD_FMT 0x00000004
76 #define ASL_FILTER_MASK_PACEWNID 0xff
77 #define ASL_FILTER_MASK_PACEWNI 0x7f
78 #define ASL_FILTER_MASK_PACEWN 0x3f
79 #define ASL_FILTER_MASK_PACEW 0x1f
80 #define ASL_FILTER_MASK_PACE 0x0f
81 #define ASL_FILTER_MASK_PAC 0x07
84 /* BEGIN PRIVATE API */
85 #define _PATH_ASL_PRUNE "/var/run/asl_prune"
86 #define _PATH_SYSLOGD_PID "/var/run/syslog.pid"
89 uint32_t notify_get_state(int token
, int *state
);
90 uint32_t notify_set_state(int token
, int state
);
91 uint32_t notify_register_plain(const char *name
, int *out_token
);
93 extern char *asl_msg_to_string(aslmsg msg
, uint32_t *len
);
94 extern asl_msg_t
*asl_msg_from_string(const char *buf
);
95 extern int asl_msg_cmp(asl_msg_t
*a
, asl_msg_t
*b
);
96 extern time_t asl_parse_time(const char *in
);
99 static const char *myname
= "syslog";
104 fprintf(stderr
, "usage:\n");
105 fprintf(stderr
, "%s -s [-r host] [-l level] message...\n", myname
);
106 fprintf(stderr
, " send a message\n");
107 fprintf(stderr
, "\n");
108 fprintf(stderr
, "%s -s [-r host] -k key val [key val]...\n", myname
);
109 fprintf(stderr
, " send a message with the given keys and values\n");
110 fprintf(stderr
, "\n");
111 fprintf(stderr
, "%s -c process [filter]\n", myname
);
112 fprintf(stderr
, " get (set if filter is specified) syslog filter for process (pid or name)\n");
113 fprintf(stderr
, " level may be any combination of the characters \"p a c e w n i d\"\n");
114 fprintf(stderr
, " p = Emergency (\"Panic\")\n");
115 fprintf(stderr
, " a = Alert\n");
116 fprintf(stderr
, " c = Critical\n");
117 fprintf(stderr
, " e = Error\n");
118 fprintf(stderr
, " w = Warning\n");
119 fprintf(stderr
, " n = Notice\n");
120 fprintf(stderr
, " i = Info\n");
121 fprintf(stderr
, " d = Debug\n");
122 fprintf(stderr
, " a minus sign preceeding a single letter means \"up to\" that level\n");
123 fprintf(stderr
, "\n");
124 fprintf(stderr
, "%s -p [-k key [[op] val]]... [-o -k key [[op] val]] ...]...\n", myname
);
125 fprintf(stderr
, " -p prune datastore according to input expression (see below)\n");
126 fprintf(stderr
, "\n");
127 fprintf(stderr
, "%s [-w] [-F format] [-u] [-k key [[op] val]]... [-o -k key [[op] val]] ...]...\n", myname
);
128 fprintf(stderr
, " -w watch file (^C to quit)\n");
129 fprintf(stderr
, " -F output format may be \"std\", \"raw\", or \"bsd\"\n");
130 fprintf(stderr
, " format may also be a string containing variables of the form\n");
131 fprintf(stderr
, " $Key or $(Key) - use the latter for non-whitespace delimited variables\n");
132 fprintf(stderr
, " -u force printing of all timestamps using UTC\n");
133 fprintf(stderr
, " -k key/value match\n");
134 fprintf(stderr
, " if no operator or value is given, checks for the existance of the key\n");
135 fprintf(stderr
, " if no operator is given, default is \"%s\"\n", OP_EQ
);
136 fprintf(stderr
, " -o begins a new query\n");
137 fprintf(stderr
, " queries are \'OR\'ed together\n");
138 fprintf(stderr
, "operators are zero or more modifiers followed by a comparison\n");
139 fprintf(stderr
, " %s equal\n", OP_EQ
);
140 fprintf(stderr
, " %s not equal\n", OP_NE
);
141 fprintf(stderr
, " %s greater than\n", OP_GT
);
142 fprintf(stderr
, " %s greater or equal\n", OP_GE
);
143 fprintf(stderr
, " %s less than\n", OP_LT
);
144 fprintf(stderr
, " %s less or equal\n", OP_LE
);
145 fprintf(stderr
, "optional modifiers for operators\n");
146 fprintf(stderr
, " %c case-fold\n", MOD_CASE_FOLD
);
147 fprintf(stderr
, " %c regular expression\n", MOD_REGEX
);
148 fprintf(stderr
, " %c substring\n", MOD_SUBSTRING
);
149 fprintf(stderr
, " %c prefix\n", MOD_PREFIX
);
150 fprintf(stderr
, " %c suffix\n", MOD_SUFFIX
);
151 fprintf(stderr
, " %c numeric comparison\n", MOD_NUMERIC
);
155 notify_status_string(int status
)
157 if (status
== NOTIFY_STATUS_OK
) return "OK";
158 if (status
== NOTIFY_STATUS_INVALID_NAME
) return "Process not registered";
159 if (status
== NOTIFY_STATUS_NOT_AUTHORIZED
) return "Not authorized";
160 return "Operation failed";
164 asl_level_string(int level
)
166 if (level
== ASL_LEVEL_EMERG
) return ASL_STRING_EMERG
;
167 if (level
== ASL_LEVEL_ALERT
) return ASL_STRING_ALERT
;
168 if (level
== ASL_LEVEL_CRIT
) return ASL_STRING_CRIT
;
169 if (level
== ASL_LEVEL_ERR
) return ASL_STRING_ERR
;
170 if (level
== ASL_LEVEL_WARNING
) return ASL_STRING_WARNING
;
171 if (level
== ASL_LEVEL_NOTICE
) return ASL_STRING_NOTICE
;
172 if (level
== ASL_LEVEL_INFO
) return ASL_STRING_INFO
;
173 if (level
== ASL_LEVEL_DEBUG
) return ASL_STRING_DEBUG
;
178 procinfo(char *pname
, int *pid
, int *uid
)
181 int i
, status
, nprocs
;
183 struct kinfo_proc
*procs
, *newprocs
;
190 mib
[2] = KERN_PROC_ALL
;
194 status
= sysctl(mib
, miblen
, NULL
, &size
, NULL
, 0);
198 newprocs
= realloc(procs
, size
);
201 if (procs
!= NULL
) free(procs
);
202 return PROC_NOT_FOUND
;
206 status
= sysctl(mib
, miblen
, procs
, &size
, NULL
, 0);
207 } while ((status
== -1) && (errno
== ENOMEM
));
211 if (procs
!= NULL
) free(procs
);
212 return PROC_NOT_FOUND
;
215 if (size
% sizeof(struct kinfo_proc
) != 0)
217 if (procs
!= NULL
) free(procs
);
218 return PROC_NOT_FOUND
;
221 if (procs
== NULL
) return PROC_NOT_FOUND
;
223 nprocs
= size
/ sizeof(struct kinfo_proc
);
227 /* Search for a pid */
228 for (i
= 0; i
< nprocs
; i
++)
230 if (*pid
== procs
[i
].kp_proc
.p_pid
)
232 *uid
= procs
[i
].kp_eproc
.e_ucred
.cr_uid
;
237 return PROC_NOT_FOUND
;
240 *pid
= PROC_NOT_FOUND
;
242 for (i
= 0; i
< nprocs
; i
++)
244 if (!strcmp(procs
[i
].kp_proc
.p_comm
, pname
))
246 if (*pid
!= PROC_NOT_FOUND
)
249 return PROC_NOT_UNIQUE
;
252 *pid
= procs
[i
].kp_proc
.p_pid
;
253 *uid
= procs
[i
].kp_eproc
.e_ucred
.cr_uid
;
258 if (*pid
== PROC_NOT_FOUND
) return PROC_NOT_FOUND
;
264 rcontrol_get_string(const char *prefix
, int pid
, int *val
)
269 status
= NOTIFY_STATUS_OK
;
271 if (pid
== RC_SYSLOGD
)
273 status
= notify_register_plain(NOTIFY_SYSTEM_ASL_FILTER
, &t
);
275 else if (pid
== RC_MASTER
)
277 status
= notify_register_plain(NOTIFY_SYSTEM_MASTER
, &t
);
282 asprintf(&name
, "%s.%d", prefix
, pid
);
283 if (name
== NULL
) return NOTIFY_STATUS_FAILED
;
285 status
= notify_register_plain(name
, &t
);
289 if (status
!= NOTIFY_STATUS_OK
) return status
;
292 status
= notify_get_state(t
, &x
);
301 rcontrol_set_string(const char *prefix
, int pid
, int filter
)
306 status
= NOTIFY_STATUS_OK
;
308 if (pid
== RC_SYSLOGD
)
310 status
= notify_register_plain(NOTIFY_SYSTEM_ASL_FILTER
, &t
);
312 else if (pid
== RC_MASTER
)
314 status
= notify_register_plain(NOTIFY_SYSTEM_MASTER
, &t
);
319 asprintf(&name
, "%s.%d", prefix
, pid
);
320 if (name
== NULL
) return NOTIFY_STATUS_FAILED
;
322 status
= notify_register_plain(name
, &t
);
326 if (status
!= NOTIFY_STATUS_OK
) return status
;
327 status
= notify_set_state(t
, filter
);
328 if ((pid
== RC_SYSLOGD
) && (status
== NOTIFY_STATUS_OK
)) status
= notify_post(NOTIFY_SYSTEM_ASL_FILTER
);
334 asl_string_to_filter(char *s
)
338 if (s
== NULL
) return 0;
339 if (s
[0] == '\0') return 0;
341 if ((s
[0] >= '0') && (s
[0] <= '9')) return ASL_FILTER_MASK(atoi(s
));
345 if ((s
[1] == 'P') || (s
[1] == 'p')) i
= ASL_LEVEL_EMERG
;
346 else if ((s
[1] == 'A') || (s
[1] == 'a')) i
= ASL_LEVEL_ALERT
;
347 else if ((s
[1] == 'C') || (s
[1] == 'c')) i
= ASL_LEVEL_CRIT
;
348 else if ((s
[1] == 'E') || (s
[1] == 'e')) i
= ASL_LEVEL_ERR
;
349 else if ((s
[1] == 'X') || (s
[1] == 'x')) i
= ASL_LEVEL_ERR
;
350 else if ((s
[1] == 'W') || (s
[1] == 'w')) i
= ASL_LEVEL_WARNING
;
351 else if ((s
[1] == 'N') || (s
[1] == 'n')) i
= ASL_LEVEL_NOTICE
;
352 else if ((s
[1] == 'I') || (s
[1] == 'i')) i
= ASL_LEVEL_INFO
;
353 else if ((s
[1] == 'D') || (s
[1] == 'd')) i
= ASL_LEVEL_DEBUG
;
354 else i
= atoi(s
+ 1);
355 f
= ASL_FILTER_MASK_UPTO(i
);
360 for (i
= 0; s
[i
] != '\0'; i
++)
362 if ((s
[i
] == 'P') || (s
[i
] == 'p')) f
|= ASL_FILTER_MASK_EMERG
;
363 else if ((s
[i
] == 'A') || (s
[i
] == 'a')) f
|= ASL_FILTER_MASK_ALERT
;
364 else if ((s
[i
] == 'C') || (s
[i
] == 'c')) f
|= ASL_FILTER_MASK_CRIT
;
365 else if ((s
[i
] == 'E') || (s
[i
] == 'e')) f
|= ASL_FILTER_MASK_ERR
;
366 else if ((s
[i
] == 'X') || (s
[i
] == 'x')) f
|= ASL_FILTER_MASK_ERR
;
367 else if ((s
[i
] == 'W') || (s
[i
] == 'w')) f
|= ASL_FILTER_MASK_WARNING
;
368 else if ((s
[i
] == 'N') || (s
[i
] == 'n')) f
|= ASL_FILTER_MASK_NOTICE
;
369 else if ((s
[i
] == 'I') || (s
[i
] == 'i')) f
|= ASL_FILTER_MASK_INFO
;
370 else if ((s
[i
] == 'D') || (s
[i
] == 'd')) f
|= ASL_FILTER_MASK_DEBUG
;
377 asl_filter_string(int f
)
379 static char str
[1024];
382 memset(str
, 0, sizeof(str
));
385 if ((f
== ASL_FILTER_MASK_PACEWNID
) != 0)
387 strcat(str
, "Emergency - Debug");
391 if ((f
== ASL_FILTER_MASK_PACEWNI
) != 0)
393 strcat(str
, "Emergency - Info");
397 if ((f
== ASL_FILTER_MASK_PACEWN
) != 0)
399 strcat(str
, "Emergency - Notice");
403 if ((f
== ASL_FILTER_MASK_PACEW
) != 0)
405 strcat(str
, "Emergency - Warning");
409 if ((f
== ASL_FILTER_MASK_PACE
) != 0)
411 strcat(str
, "Emergency - Error");
415 if ((f
== ASL_FILTER_MASK_PAC
) != 0)
417 strcat(str
, "Emergency - Critical");
421 if ((f
& ASL_FILTER_MASK_EMERG
) != 0)
423 strcat(str
, "Emergency");
427 if ((f
& ASL_FILTER_MASK_ALERT
) != 0)
429 if (i
> 0) strcat(str
, ", ");
430 strcat(str
, "Alert");
434 if ((f
& ASL_FILTER_MASK_CRIT
) != 0)
436 if (i
> 0) strcat(str
, ", ");
437 strcat(str
, "Critical");
441 if ((f
& ASL_FILTER_MASK_ERR
) != 0)
443 if (i
> 0) strcat(str
, ", ");
444 strcat(str
, "Error");
448 if ((f
& ASL_FILTER_MASK_WARNING
) != 0)
450 if (i
> 0) strcat(str
, ", ");
451 strcat(str
, "Warning");
455 if ((f
& ASL_FILTER_MASK_NOTICE
) != 0)
457 if (i
> 0) strcat(str
, ", ");
458 strcat(str
, "Notice");
462 if ((f
& ASL_FILTER_MASK_INFO
) != 0)
464 if (i
> 0) strcat(str
, ", ");
469 if ((f
& ASL_FILTER_MASK_DEBUG
) != 0)
471 if (i
> 0) strcat(str
, ", ");
472 strcat(str
, "Debug");
476 if (i
== 0) sprintf(str
, "Off");
482 rcontrol_get(const char *prefix
, int pid
)
492 if (pid
== RC_SYSLOGD
) name
= "ASL Data Store";
494 status
= rcontrol_get_string(NULL
, pid
, &filter
);
495 if (status
== NOTIFY_STATUS_OK
)
497 printf("%s filter mask: %s\n", name
, asl_filter_string(filter
));
501 printf("Unable to determine %s filter mask\n", name
);
505 status
= rcontrol_get_string(prefix
, pid
, &filter
);
506 if (status
== NOTIFY_STATUS_OK
)
508 printf("Process %d syslog filter mask: %s\n", pid
, asl_filter_string(filter
));
512 printf("Unable to determine syslog filter mask for pid %d\n", pid
);
517 rcontrol_set(const char *prefix
, int pid
, int filter
)
525 if (pid
== RC_SYSLOGD
) name
= "ASL Data Store";
526 status
= rcontrol_set_string(NULL
, pid
, filter
);
528 if (status
== NOTIFY_STATUS_OK
)
530 printf("Set %s syslog filter mask: %s\n", name
, asl_filter_string(filter
));
534 printf("Unable to set %s syslog filter mask: %s\n", name
, notify_status_string(status
));
538 status
= rcontrol_set_string(prefix
, pid
, filter
);
539 if (status
== NOTIFY_STATUS_OK
)
541 printf("Set process %d syslog filter mask set: %s\n", pid
, asl_filter_string(filter
));
545 printf("Unable to set syslog filter mask for pid %d: %s\n", pid
, notify_status_string(status
));
550 rsend(aslmsg msg
, char *rhost
)
559 struct sockaddr_in dst
;
561 char myname
[MAXHOSTNAMELEN
+ 1];
563 if (msg
== NULL
) return 0;
565 h
= gethostbyname(rhost
);
566 if (h
== NULL
) return -1;
568 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
569 if (s
<= 0) return -1;
571 memset(&dst
, 0, sizeof(struct sockaddr_in
));
572 memcpy(&(dst
.sin_addr
.s_addr
), h
->h_addr_list
[0], 4);
573 dst
.sin_family
= AF_INET
;
575 dst
.sin_len
= sizeof(struct sockaddr_in
);
577 level
= ASL_LEVEL_DEBUG
;
579 val
= asl_get(msg
, ASL_KEY_LEVEL
);
580 if (val
!= NULL
) level
= atoi(val
);
582 memset(>ime
, 0, sizeof(struct tm
));
586 gmtime_r(&tick
, >ime
);
588 /* Canonical form: YYYY.MM.DD hh:mm:ss UTC */
589 asprintf(×tr
, "%d.%02d.%02d %02d:%02d:%02d UTC", gtime
.tm_year
+ 1900, gtime
.tm_mon
+ 1, gtime
.tm_mday
, gtime
.tm_hour
, gtime
.tm_min
, gtime
.tm_sec
);
593 asl_set(msg
, ASL_KEY_TIME
, timestr
);
597 if (gethostname(myname
, MAXHOSTNAMELEN
) == 0) asl_set(msg
, ASL_KEY_HOST
, myname
);
600 str
= asl_msg_to_string(msg
, &len
);
601 if (str
== NULL
) return -1;
603 asprintf(&out
, "%10u %s\n", len
+1, str
);
605 if (out
== NULL
) return -1;
607 sendto(s
, out
, len
+12, 0, (const struct sockaddr
*)&dst
, sizeof(struct sockaddr_in
));
615 rlegacy(char *msg
, int level
, char *rhost
)
622 struct sockaddr_in dst
;
624 char myname
[MAXHOSTNAMELEN
+ 1];
626 if (msg
== NULL
) return 0;
628 h
= gethostbyname(rhost
);
629 if (h
== NULL
) return -1;
631 s
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
632 if (s
<= 0) return -1;
634 memset(&dst
, 0, sizeof(struct sockaddr_in
));
635 memcpy(&(dst
.sin_addr
.s_addr
), h
->h_addr_list
[0], 4);
636 dst
.sin_family
= AF_INET
;
638 dst
.sin_len
= sizeof(struct sockaddr_in
);
641 ltime
= ctime(&tick
);
644 gethostname(myname
, MAXHOSTNAMELEN
);
646 asprintf(&out
, "<%d>%s %s syslog[%d]: %s", level
, ltime
+4, myname
, getpid(), msg
);
648 sendto(s
, out
, len
, 0, (const struct sockaddr
*)&dst
, sizeof(struct sockaddr_in
));
660 if (s
== NULL
) return 0;
663 if ((s
[0] == '-') || (s
[0] == '+')) i
= 1;
665 if (s
[i
] == '\0') return 0;
667 for (; s
[i
] != '\0'; i
++)
669 if (!isdigit(s
[i
])) return 0;
676 asl_string_to_level(const char *s
)
678 if (s
== NULL
) return -1;
680 if ((s
[0] >= '0') && (s
[0] <= '7') && (s
[1] == '\0')) return atoi(s
);
682 if (!strncasecmp(s
, "em", 2)) return ASL_LEVEL_EMERG
;
683 else if (!strncasecmp(s
, "p", 1)) return ASL_LEVEL_EMERG
;
684 else if (!strncasecmp(s
, "a", 1)) return ASL_LEVEL_ALERT
;
685 else if (!strncasecmp(s
, "c", 1)) return ASL_LEVEL_CRIT
;
686 else if (!strncasecmp(s
, "er", 2)) return ASL_LEVEL_ERR
;
687 else if (!strncasecmp(s
, "x", 1)) return ASL_LEVEL_ERR
;
688 else if (!strncasecmp(s
, "w", 1)) return ASL_LEVEL_WARNING
;
689 else if (!strncasecmp(s
, "n", 1)) return ASL_LEVEL_NOTICE
;
690 else if (!strncasecmp(s
, "i", 1)) return ASL_LEVEL_INFO
;
691 else if (!strncasecmp(s
, "d", 1)) return ASL_LEVEL_DEBUG
;
697 syslog_remote_control(int argc
, char *argv
[])
699 int pid
, uid
, status
, mask
;
702 if ((argc
< 3) || (argc
> 4))
704 fprintf(stderr
, "usage:\n");
705 fprintf(stderr
, "%s -c process [mask]\n", myname
);
706 fprintf(stderr
, " get (set if mask is specified) syslog filter mask for process (pid or name)\n");
707 fprintf(stderr
, " process may be pid or process name\n");
708 fprintf(stderr
, " use \"-c 0\" to get master syslog filter mask\n");
709 fprintf(stderr
, " use \"-c 0 off\" to disable master syslog filter mask\n");
710 fprintf(stderr
, "\n");
717 status
= PROC_NOT_FOUND
;
719 if ((!strcmp(argv
[2], "syslogd")) || (!strcmp(argv
[2], "syslog")))
725 else if (_isanumber(argv
[2]) != 0)
728 status
= procinfo(NULL
, &pid
, &uid
);
732 status
= procinfo(argv
[2], &pid
, &uid
);
735 if (status
== PROC_NOT_FOUND
)
737 fprintf(stderr
, "%s: process not found\n", argv
[2]);
741 if (status
== PROC_NOT_UNIQUE
)
743 fprintf(stderr
, "%s: multiple processes found\n", argv
[2]);
744 fprintf(stderr
, "use pid to identify a process uniquely\n");
748 if (pid
== 0) pid
= RC_MASTER
;
750 prefix
= NOTIFY_PREFIX_USER
;
751 if (uid
== 0) prefix
= NOTIFY_PREFIX_SYSTEM
;
755 if ((pid
== RC_MASTER
) && (!strcasecmp(argv
[3], "off"))) mask
= 0;
756 else if ((pid
== RC_SYSLOGD
) && (!strcasecmp(argv
[3], "off"))) mask
= 0;
759 mask
= asl_string_to_filter(argv
[3]);
762 printf("unknown syslog mask: %s\n", argv
[3]);
767 rcontrol_set(prefix
, pid
, mask
);
771 rcontrol_get(prefix
, pid
);
778 syslog_send(int argc
, char *argv
[])
780 int i
, start
, kv
, len
, rfmt
, rlevel
;
783 char tmp
[64], *str
, *rhost
;
787 rfmt
= SEND_FORMAT_ASL
;
791 for (i
= 1; i
< argc
; i
++)
793 if (!strcmp(argv
[i
], "-s")) start
= i
+1;
794 else if (!strcmp(argv
[i
], "-k")) kv
= 1;
795 else if (!strcmp(argv
[i
], "-r"))
799 rfmt
= SEND_FORMAT_LEGACY
;
801 else if (!strcmp(argv
[i
], "-R"))
806 else if (!strcmp(argv
[i
], "-l"))
808 rlevel
= asl_string_to_level(argv
[++i
]);
811 fprintf(stderr
, "Unknown level: %s\n", argv
[i
]);
818 asl
= asl_open(myname
, "syslog", 0);
819 asl_set_filter(asl
, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG
));
821 m
= asl_new(ASL_TYPE_MSG
);
822 asl_set(m
, ASL_KEY_SENDER
, myname
);
824 sprintf(tmp
, "%d", rlevel
);
825 asl_set(m
, ASL_KEY_LEVEL
, tmp
);
832 for (i
= start
; i
< argc
; i
++) len
+= (strlen(argv
[i
]) + 1);
833 str
= calloc(len
+ 1, 1);
834 for (i
= start
; i
< argc
; i
++)
836 strcat(str
, argv
[i
]);
837 if ((i
+1) < argc
) strcat(str
, " ");
839 asl_set(m
, ASL_KEY_MSG
, str
);
843 for (i
= start
+ 1; i
< argc
; i
+= 2) asl_set(m
, argv
[i
], argv
[i
+ 1]);
850 else if (rfmt
== SEND_FORMAT_ASL
)
854 else if ((rfmt
== SEND_FORMAT_LEGACY
) && (str
!= NULL
))
856 rlegacy(str
, rlevel
, rhost
);
861 if (str
!= NULL
) free(str
);
869 printmsg(FILE *f
, asl_msg_t
*msg
, char *mstr
, char *fmt
, int pflags
)
873 int i
, j
, l
, paren
, oval
;
876 if ((pflags
& PRINT_STD_FMT
) || (pflags
& PRINT_LEGACY_FMT
))
878 /* LEGACY: Mth dd hh:mm:ss host sender[pid]: message */
879 /* STD: Mth dd hh:mm:ss host sender[pid] <Level>: message */
882 v
= asl_get(msg
, ASL_KEY_TIME
);
886 fprintf(f
, "***Time unknown ");
890 tick
= asl_parse_time(v
);
892 if (t
== NULL
) fprintf(f
, "***Time unknown ");
896 fprintf(f
, "%s ", t
+ 4);
901 v
= asl_get(msg
, ASL_KEY_HOST
);
902 if (v
!= NULL
) fprintf(f
, "%s ", v
);
905 v
= asl_get(msg
, ASL_KEY_SENDER
);
906 if (v
!= NULL
) fprintf(f
, "%s", v
);
909 v
= asl_get(msg
, ASL_KEY_PID
);
910 if ((v
!= NULL
) && (v
[0] != '-')) fprintf(f
, "[%s]", v
);
912 v
= asl_get(msg
, ASL_KEY_LEVEL
);
914 if (_isanumber((char *)v
)) i
= atoi(v
);
915 if (pflags
& PRINT_STD_FMT
) fprintf(f
, " <%s>", asl_level_string(i
));
920 v
= asl_get(msg
, ASL_KEY_MSG
);
921 if (v
!= NULL
) fprintf(f
, "%s", v
);
929 fprintf(f
, "%s\n", mstr
);
933 for (i
= 0; fmt
[i
] != '\0'; i
++)
949 for (j
= i
; fmt
[j
] != '\0'; j
++)
952 if (fmt
[j
] == '\\') c
= fmt
[++j
];
953 else if ((paren
== 1) && (fmt
[j
] ==')')) break;
954 else if (fmt
[j
] != ' ') c
= fmt
[j
];
956 if (c
== '\0') break;
958 k
= realloc(k
, l
+ 1);
971 if ((pflags
& PRINT_LOCALTIME
) && (!strcmp(k
, ASL_KEY_TIME
)))
973 /* convert UTC time to localtime */
974 tick
= asl_parse_time(v
);
976 if (t
== NULL
) fprintf(f
, "%s", v
);
980 fprintf(f
, "%s", t
+ 4);
995 if (fmt
[i
] == '$') fprintf(f
, "$");
996 else if (fmt
[i
] == 'e') fprintf(f
, "\e");
997 else if (fmt
[i
] == 'a') fprintf(f
, "\a");
998 else if (fmt
[i
] == 'b') fprintf(f
, "\b");
999 else if (fmt
[i
] == 'f') fprintf(f
, "\f");
1000 else if (fmt
[i
] == 'n') fprintf(f
, "\n");
1001 else if (fmt
[i
] == 'r') fprintf(f
, "\r");
1002 else if (fmt
[i
] == 't') fprintf(f
, "\t");
1003 else if (fmt
[i
] == 'v') fprintf(f
, "\v");
1004 else if (fmt
[i
] == '\'') fprintf(f
, "\'");
1005 else if (fmt
[i
] == '\\') fprintf(f
, "\\");
1006 else if (isdigit(fmt
[i
]))
1008 oval
= fmt
[i
] - '0';
1009 if (isdigit(fmt
[i
+1]))
1012 oval
= (oval
* 8) + (fmt
[i
] - '0');
1013 if (isdigit(fmt
[i
+1]))
1016 oval
= (oval
* 8) + (fmt
[i
] - '0');
1025 if (fmt
[i
] == '\0') break;
1026 fputc(fmt
[i
], stdout
);
1033 getnextline(FILE *fp
, int watch
)
1040 out
= calloc(len
+ 1, 1);
1061 if (c
== '\n') return out
;
1062 if (c
== '\0') return out
;
1067 out
= realloc(out
, len
+ 1);
1078 search_next(asl_msg_t
**q
, int nq
, FILE *log
, int watch
, aslmsg
*outmsg
, char **outstr
)
1087 if (log
== NULL
) return SEARCH_EOF
;
1089 str
= getnextline(log
, watch
);
1090 if (str
== NULL
) return SEARCH_EOF
;
1092 m
= asl_msg_from_string(str
);
1100 if (q
== NULL
) match
= 1;
1101 for (i
= 0; (i
< nq
) && (match
== 0); i
++)
1103 match
= asl_msg_cmp(q
[i
], m
);
1104 if ((q
[i
]->count
> 0) && (q
[i
]->op
[0] & ASL_QUERY_OP_NOT
))
1119 return SEARCH_MATCH
;
1127 op
= ASL_QUERY_OP_NULL
;
1129 if (o
== NULL
) return op
;
1131 for (i
= 0; o
[i
] != '\0'; i
++)
1133 if (o
[i
] == MOD_CASE_FOLD
) op
|= ASL_QUERY_OP_CASEFOLD
;
1134 else if (o
[i
] == MOD_REGEX
) op
|= ASL_QUERY_OP_REGEX
;
1135 else if (o
[i
] == MOD_NUMERIC
) op
|= ASL_QUERY_OP_NUMERIC
;
1136 else if (o
[i
] == MOD_SUBSTRING
) op
|= ASL_QUERY_OP_SUBSTRING
;
1137 else if (o
[i
] == MOD_PREFIX
) op
|= ASL_QUERY_OP_PREFIX
;
1138 else if (o
[i
] == MOD_SUFFIX
) op
|= ASL_QUERY_OP_SUFFIX
;
1140 else if (!strncasecmp(o
+i
, OP_EQ
, sizeof(OP_EQ
)))
1142 op
|= ASL_QUERY_OP_EQUAL
;
1143 i
+= (sizeof(OP_EQ
) - 2);
1145 else if (!strncasecmp(o
+i
, OP_NE
, sizeof(OP_NE
)))
1147 op
|= ASL_QUERY_OP_NOT_EQUAL
;
1148 i
+= (sizeof(OP_NE
) - 2);
1150 else if (!strncasecmp(o
+i
, OP_GT
, sizeof(OP_GT
)))
1152 op
|= ASL_QUERY_OP_GREATER
;
1153 i
+= (sizeof(OP_GT
) - 2);
1155 else if (!strncasecmp(o
+i
, OP_GE
, sizeof(OP_GE
)))
1157 op
|= ASL_QUERY_OP_GREATER_EQUAL
;
1158 i
+= (sizeof(OP_GE
) - 2);
1160 else if (!strncasecmp(o
+i
, OP_LT
, sizeof(OP_LT
)))
1162 op
|= ASL_QUERY_OP_LESS
;
1163 i
+= (sizeof(OP_LT
) - 2);
1165 else if (!strncasecmp(o
+i
, OP_LE
, sizeof(OP_LE
)))
1167 op
|= ASL_QUERY_OP_LESS_EQUAL
;
1168 i
+= (sizeof(OP_LE
) - 2);
1172 fprintf(stderr
, "invalid option: %s\n", o
);
1178 if (op
& ASL_QUERY_OP_NUMERIC
)
1180 if (op
& ASL_QUERY_OP_CASEFOLD
)
1182 fprintf(stderr
, "warning: case fold modifier has no effect with numeric comparisons\n");
1183 op
&= ~ASL_QUERY_OP_CASEFOLD
;
1186 if (op
& ASL_QUERY_OP_REGEX
)
1188 fprintf(stderr
, "warning: regex modifier has no effect with numeric comparisons\n");
1189 op
&= ~ASL_QUERY_OP_REGEX
;
1192 if (op
& ASL_QUERY_OP_SUBSTRING
)
1194 fprintf(stderr
, "warning: substring modifier has no effect with numeric comparisons\n");
1195 op
&= ~ASL_QUERY_OP_SUBSTRING
;
1198 if (op
& ASL_QUERY_OP_PREFIX
)
1200 fprintf(stderr
, "warning: prefix modifier has no effect with numeric comparisons\n");
1201 op
&= ~ASL_QUERY_OP_PREFIX
;
1204 if (op
& ASL_QUERY_OP_SUFFIX
)
1206 fprintf(stderr
, "warning: suffix modifier has no effect with numeric comparisons\n");
1207 op
&= ~ASL_QUERY_OP_SUFFIX
;
1211 if (op
& ASL_QUERY_OP_REGEX
)
1213 if (op
& ASL_QUERY_OP_SUBSTRING
)
1215 fprintf(stderr
, "warning: substring modifier has no effect with regular expression comparisons\n");
1216 op
&= ~ASL_QUERY_OP_SUBSTRING
;
1219 if (op
& ASL_QUERY_OP_PREFIX
)
1221 fprintf(stderr
, "warning: prefix modifier has no effect with regular expression comparisons\n");
1222 op
&= ~ASL_QUERY_OP_PREFIX
;
1225 if (op
& ASL_QUERY_OP_SUFFIX
)
1227 fprintf(stderr
, "warning: suffix modifier has no effect with regular expression comparisons\n");
1228 op
&= ~ASL_QUERY_OP_SUFFIX
;
1236 add_op(asl_msg_t
*q
, char *key
, char *op
, char *val
, uint32_t flags
)
1240 if (key
== NULL
) return -1;
1241 if (q
== NULL
) return -1;
1243 o
= ASL_QUERY_OP_NULL
;
1247 if (o
== ASL_QUERY_OP_NULL
) return -1;
1250 fprintf(stderr
, "no value supplied for operator %s %s\n", key
, op
);
1254 if ((o
& ASL_QUERY_OP_NUMERIC
) && (_isanumber(val
) == 0))
1256 fprintf(stderr
, "non-numeric value supplied for numeric operator %s %s %s\n", key
, op
, val
);
1263 asl_set_query(q
, key
, val
, o
);
1269 main(int argc
, char *argv
[])
1271 FILE *log
, *pf
, *outfile
;
1272 int i
, j
, n
, qcount
, qn
, watch
, prune
, status
, pflags
, tflag
;
1273 asl_msg_t
**qlist
, *outmsg
;
1274 char *logname
, *outname
, *pfmt
, *outstr
;
1281 logname
= _PATH_ASL_OUT
;
1286 pflags
= PRINT_STD_FMT
;
1287 tflag
= PRINT_LOCALTIME
;
1289 for (i
= 1; i
< argc
; i
++)
1291 if (!strcmp(argv
[i
], "-s"))
1293 syslog_send(argc
, argv
);
1297 if (!strcmp(argv
[i
], "-c"))
1299 syslog_remote_control(argc
, argv
);
1304 for (i
= 1; i
< argc
; i
++)
1306 if ((!strcmp(argv
[i
], "-help")) || (!strcmp(argv
[i
], "--help")))
1311 else if (!strcmp(argv
[i
], "-w"))
1315 else if (!strcmp(argv
[i
], "-u"))
1319 else if (!strcmp(argv
[i
], "-p"))
1323 else if (!strcmp(argv
[i
], "-f"))
1325 if ((i
+ 1) >= argc
)
1331 logname
= argv
[++i
];
1333 else if (!strcmp(argv
[i
], "-F"))
1335 if ((i
+ 1) >= argc
)
1342 if (!strcmp(argv
[i
], "raw"))
1347 else if (!strcmp(argv
[i
], "std"))
1349 pflags
= PRINT_STD_FMT
;
1351 else if (!strcmp(argv
[i
], "bsd"))
1353 pflags
= PRINT_LEGACY_FMT
;
1361 else if (!strcmp(argv
[i
], "-o"))
1367 qlist
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
1371 qlist
= (asl_msg_t
**)realloc(qlist
, (qcount
+ 1) * sizeof(asl_msg_t
*));
1376 qlist
[qn
] = asl_new(ASL_TYPE_QUERY
);
1378 else if (!strcmp(argv
[i
], "-n"))
1380 flags
= ASL_QUERY_OP_NOT
;
1382 else if (!strcmp(argv
[i
], "-k"))
1385 for (n
= i
; n
< argc
; n
++)
1387 if (!strcmp(argv
[n
], "-o")) break;
1388 if (!strcmp(argv
[n
], "-n")) break;
1389 if (!strcmp(argv
[n
], "-k")) break;
1392 fprintf(stderr
, "invalid sequence: -k");
1393 for (j
= i
; j
<= n
; j
++) fprintf(stderr
, " %s", argv
[j
]);
1394 fprintf(stderr
, "\n");
1409 qlist
= (asl_msg_t
**)calloc(1, sizeof(asl_msg_t
*));
1412 qlist
[qn
] = asl_new(ASL_TYPE_QUERY
);
1416 if (n
== 1) status
= add_op(qlist
[qn
], argv
[i
], NULL
, NULL
, flags
);
1417 else if (n
== 2) status
= add_op(qlist
[qn
], argv
[i
], OP_EQ
, argv
[i
+1], flags
);
1418 else status
= add_op(qlist
[qn
], argv
[i
], argv
[i
+1], argv
[i
+2], flags
);
1421 if (status
!= 0) exit(1);
1431 fprintf(stderr
, "-w flag has no effect when pruning log file\n");
1436 fprintf(stderr
, "you must be root to prune the log file\n");
1442 fprintf(stderr
, "no queries for pruning\n");
1446 pf
= fopen(_PATH_SYSLOGD_PID
, "r");
1449 perror(_PATH_SYSLOGD_PID
);
1453 status
= fscanf(pf
, "%d", &syslogd_pid
);
1457 fprintf(stderr
, "can't read syslogd pid from %s\n", _PATH_SYSLOGD_PID
);
1461 unlink(_PATH_ASL_PRUNE
);
1462 pf
= fopen(_PATH_ASL_PRUNE
, "w");
1465 perror(_PATH_ASL_PRUNE
);
1469 for (i
= 0; i
< qcount
; i
++)
1471 outstr
= asl_msg_to_string(qlist
[i
], &j
);
1472 fprintf(pf
, "%s\n", outstr
);
1478 kill(syslogd_pid
, SIGWINCH
);
1485 if (!strcmp(logname
, "-")) log
= stdin
;
1486 else log
= fopen(logname
, "r");
1494 if (watch
== 1) fseek(log
, 0, SEEK_END
);
1502 status
= search_next(qlist
, qcount
, log
, watch
, &outmsg
, &outstr
);
1504 if (status
== SEARCH_MATCH
)
1506 printmsg(outfile
, outmsg
, outstr
, pfmt
, pflags
);
1509 if (outstr
!= NULL
) free(outstr
);
1510 if (outmsg
!= NULL
) asl_free(outmsg
);
1512 while (status
!= SEARCH_EOF
);
1516 for (i
= 0; i
< qcount
; i
++) asl_free(qlist
[i
]);
1517 if (qlist
!= NULL
) free(qlist
);