]>
git.cameronkatri.com Git - pw-darwin.git/blob - pw/pw_log.c
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * David L. Nugent. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static const char rcsid
[] =
42 static FILE *logfile
= NULL
;
45 pw_log(struct userconf
* cnf
, int mode
, int which
, char const * fmt
,...)
49 const char *cp
, *name
;
52 char nfmt
[256], sname
[32];
54 if (cnf
->logfile
== NULL
|| cnf
->logfile
[0] == '\0') {
58 if (logfile
== NULL
) {
59 /* With umask==0 we need to control file access modes on create */
60 fd
= open(cnf
->logfile
, O_WRONLY
| O_CREAT
| O_APPEND
, 0600);
64 logfile
= fdopen(fd
, "a");
65 if (logfile
== NULL
) {
70 if ((name
= getenv("LOGNAME")) == NULL
&&
71 (name
= getenv("USER")) == NULL
) {
72 strcpy(sname
, "unknown");
75 * Since "name" will be embedded in a printf-like format,
76 * we must sanitize it:
78 * Limit its length so other information in the message
81 * Squeeze out embedded whitespace for the benefit of
84 * Escape embedded % characters with another %
86 for (i
= 0, cp
= name
;
87 *cp
!= '\0' && i
< (int)sizeof(sname
) - 1; cp
++) {
89 if (i
< (int)sizeof(sname
) - 2) {
95 } else if (!isspace(*cp
)) {
97 } /* else do nothing */
100 strcpy(sname
, "unknown");
107 /* ISO 8601 International Standard Date format */
108 strftime(nfmt
, sizeof nfmt
, "%Y-%m-%d %T ", t
);
109 rlen
= sizeof(nfmt
) - strlen(nfmt
);
110 if (rlen
<= 0 || snprintf(nfmt
+ strlen(nfmt
), rlen
,
111 "[%s:%s%s] %s\n", sname
, Which
[which
], Modes
[mode
],
113 warnx("log format overflow, user name=%s", sname
);
116 vfprintf(logfile
, nfmt
, argp
);