5 * Copyright 1994 Matthew Dillon (dillon@apollo.backplane.com)
6 * Copyright 2009 James Pryor <profjim@jimpryor.net>
7 * May be distributed under the GNU General Public License
12 Prototype
void logn(int level
, const char *ctl
, ...);
13 Prototype
void logfd(int level
, int fd
, const char *ctl
, ...);
14 Prototype
void fdprintf(int fd
, const char *ctl
, ...);
15 Prototype
int ChangeUser(const char *user
, short dochdir
);
16 Prototype
void vlog(int level
, int fd
, const char *ctl
, va_list va
);
17 Prototype
void startlogger(void);
18 Prototype
void initsignals(void);
19 Prototype
char Hostname
[64];
21 int slog(char *buf
, const char *ctl
, int nmax
, va_list va
, short useDate
);
26 logn(int level
, const char *ctl
, ...)
31 vlog(level
, 2, ctl
, va
);
36 logfd(int level
, int fd
, const char *ctl
, ...)
41 vlog(level
, fd
, ctl
, va
);
46 fdprintf(int fd
, const char *ctl
, ...)
52 vsnprintf(buf
, sizeof(buf
), ctl
, va
);
53 write(fd
, buf
, strlen(buf
));
58 vlog(int level
, int fd
, const char *ctl
, va_list va
)
63 static short useDate
= 1;
65 if (level
<= LogLevel
) {
66 vsnprintf(buf
,sizeof(buf
), ctl
, va
);
67 if (ForegroundOpt
== 1)
68 /* when -d or -f, we always (and only) log to stderr
69 * fd will be 2 except when 2 is bound to a execing subprocess, then it will be 8
71 write(fd
, buf
, strlen(buf
));
73 if (LoggerOpt
== 0) syslog(level
, "%s", buf
);
75 if ((logfd
= open(LogFile
,O_WRONLY
|O_CREAT
|O_APPEND
,0600)) >= 0) {
76 write(logfd
, buf
, n
= slog(buf
, ctl
, sizeof(buf
), va
, useDate
));
77 useDate
= (n
&& buf
[n
-1] == '\n');
81 fdprintf(fd
, "failed to open logfile '%s' reason: %s\n",
92 slog(char *buf
, const char *ctl
, int nmax
, va_list va
, short useDate
)
94 time_t t
= time(NULL
);
95 struct tm
*tp
= localtime(&t
);
100 strftime(hdr
, 128, LOG_DATE_FMT
, tp
);
101 if (!gethostname(Hostname
, 63))
102 Hostname
[63] = 0; // if hostname is larger than buffer, gethostname() doesn't promise to null-terminate it
104 Hostname
[0] = 0; // gethostname() call failed
105 snprintf(buf
, 194, hdr
, Hostname
);
107 vsnprintf(buf
+ strlen(buf
), nmax
, ctl
, va
);
112 ChangeUser(const char *user
, short dochdir
)
117 * Obtain password entry and change privilages
120 if ((pas
= getpwnam(user
)) == 0) {
121 logn(LOG_ERR
, "failed to get uid for %s\n", user
);
124 setenv("USER", pas
->pw_name
, 1);
125 setenv("HOME", pas
->pw_dir
, 1);
126 setenv("SHELL", "/bin/sh", 1);
129 * Change running state to the user in question
132 if (initgroups(user
, pas
->pw_gid
) < 0) {
133 logn(LOG_ERR
, "initgroups failed: %s %s\n", user
, strerror(errno
));
136 if (setregid(pas
->pw_gid
, pas
->pw_gid
) < 0) {
137 logn(LOG_ERR
, "setregid failed: %s %d\n", user
, pas
->pw_gid
);
140 if (setreuid(pas
->pw_uid
, pas
->pw_uid
) < 0) {
141 logn(LOG_ERR
, "setreuid failed: %s %d\n", user
, pas
->pw_uid
);
145 if (chdir(pas
->pw_dir
) < 0) {
146 logn(LOG_ERR
, "chdir failed: %s %s\n", user
, pas
->pw_dir
);
147 if (chdir(TempDir
) < 0) {
148 logn(LOG_ERR
, "chdir failed: %s %s\n", user
, TempDir
);
162 openlog(LOG_IDENT
, LOG_CONS
|LOG_PID
,LOG_CRON
);
164 else { /* test logfile */
165 if ((logfd
= open(LogFile
,O_WRONLY
|O_CREAT
|O_APPEND
,0600)) >= 0)
168 errx(errno
, "failed to open logfile '%s' reason: %s",
177 signal(SIGHUP
,SIG_IGN
); /* JP: hmm.. but, if kill -HUP original
178 * version - has died. ;(