Commit Graph

53 Commits

Author SHA1 Message Date
Dmitry Wagin
9bd7345212 libc: Some enhancements to syslog(3)
- Defined MAXLINE constant (8192 octets by default instead 2048) for
  centralized limit setting up. It sets maximum number of characters of
  the syslog message. RFC5424 doesn't limit maximum size of the message.
  Named after MAXLINE in syslogd(8).
- Fixed size of fmt_cpy buffer up to MAXLINE for rendering formatted
  (%m) messages.
- Introduced autoexpansion of sending socket buffer up to MAXLINE.

MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D27205
2021-03-23 12:49:58 -04:00
Bryan Drewery
2933cd3184 syslog(3): Send proper NILVALUE if gethostname(3) fails.
RFC5424 defines NILVALUE as '-'. Replace its usage with a macro and
separate out the fields to be more clear. fputs(3) is used in some
places to avoid hiding possible format string problems in a macro.

Reviewed by:	cem, vangyzen (earlier version)
Sponsored by:	Dell EMC
2020-08-14 00:18:18 +00:00
John Baldwin
c1920558b3 Clean up the vcs ID strings in libc's gen/ directory.
- Move CSRG IDs into __SCCSID().
- When a file has been copied, consistently use 'From: <tag>' for strings
  referencing the version of the source file copied from in the license
  block comment.
- Some of the 'From:' tags were using $FreeBSD$ that was being expanded on
  each checkout.  Fix those to hardcode the FreeBSD tag from the file that
  was copied at the time of the copy.
- When multiple strings are present list them in "chronological" order,
  so CSRG (__SCCSID) before FreeBSD (__FBSDID).  If a file came from
  OtherBSD and contains a CSRG ID from the OtherBSD file, use the order
  CSRG -> OtherBSD -> FreeBSD.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D15831
2018-07-03 17:31:45 +00:00
Ed Schouten
8129693e78 Let syslog(3) use RFC 5424.
With r332099 changing syslogd(8) to parse RFC 5424 formatted syslog
messages, go ahead and also change the syslog(3) libc function to
generate them. Compared to RFC 3164, RFC 5424 has various advantages,
such as sub-second precision for log entry timestamps.

As this change could have adverse effects when not updating syslogd(8)
or using a different system logging daemon, add a notice to UPDATING and
increase __FreeBSD_version.

Differential Revision:	https://reviews.freebsd.org/D14926
2018-04-06 13:00:45 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Konstantin Belousov
f3990417c5 Do not leak syslog_mutex on cancellation.
Make syslog(3) resilent to cancellation occuring in supported deferred
mode.  Code must unlock syslog_mutex on cancel, install the cleanup
handler.

Diagnosed and tested by:	eugen
Discussed with:	dchagin
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2017-06-17 11:29:59 +00:00
Warner Losh
fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Xin LI
52e05d9a24 Reinstitate send() after syslogd restarts.
In r228193 the test of CONNPRIV have been moved to before the _usleep
and send in vsyslog().  When syslogd restarts, this would prevent the
message being logged after the disconnect/connect dance for
scenario #1.

PR:		194751
Submitted by:	Peter Creath <pjcreath+freebsd gmail com>
Reviewed By:	glebius
MFC after:	2 weeks
Differential Revision: https://reviews.freebsd.org/D1227
2014-11-25 20:59:22 +00:00
Jilles Tjoelker
0b89df4a57 syslog: Use SOCK_CLOEXEC instead of separate fcntl() call. 2013-10-20 21:04:44 +00:00
Jilles Tjoelker
05eb11cbc4 libc: Use O_CLOEXEC for various internal file descriptors.
This fixes a race condition where another thread may fork() before CLOEXEC
is set, unintentionally passing the descriptor to the child process.

This commit only adds O_CLOEXEC flags to open() or openat() calls where no
fcntl(fd, F_SETFD, FD_CLOEXEC) follows. The separate fcntl() call still
leaves a race window so it should be fixed later.
2012-09-29 11:54:34 +00:00
Eitan Adler
43be4ab038 Remove the assert added in r237286
The use of assertions in libraries is not widely accepted.

Requested by:	bde, scottl
Approved by:	cperciva
MFC after:	3 days
X-MFC-With:	r237286
2012-06-24 07:03:22 +00:00
Eitan Adler
bf36cf8e1b Don't close an uninitialized descriptor. [1]
Add a sanity check for the validity of the passed fd.

PR:		kern/139080 [1]
Submitted by:	Andrey Simonenko <simon@comsys.ntu-kpi.kiev.ua> [1]
Reviewed by:	pjd (briefly)
Approved by:	cperciva
MFC after:	1 week
2012-06-20 06:38:41 +00:00
David E. O'Brien
0582474525 Tweak the r137233 fix to r136283 -- Code was making two send() attempts
vs. the comment documented "If we are working with a privileged socket,
then take only one attempt".  Make the code match.

Furthermore, critical privileged applications that [over] log a vast amount
can look like a DoS to this code.  Given it's unlikely the single reattempted
send() will succeeded, avoid usurping the scheduler in a library API for a
single non-critical facility in critical applications.

Obtained from:	Juniper Networks
Discussed with:	glebius
2011-12-02 01:06:33 +00:00
Sergey Kandaurov
af47829540 Use FD_CLOEXEC explicitly.
MFC after:	3 days
2011-03-30 14:15:18 +00:00
Warner Losh
c879ae3536 Per Regents of the University of Calfornia letter, remove advertising
clause.

# If I've done so improperly on a file, please let me know.
2007-01-09 00:28:16 +00:00
Poul-Henning Kamp
5b1deb3ce8 Return length from fwopen() file callback instead of zero.
The symptom is that syslog() fails to log anything but the "ident"
string if LOG_PERROR is specified to openlog(3) and the extensible
printf is in action.

For unclear, likely quaint historical reasons, syslog uses fwopen()
on a stack buffer, rather than using the more straightforward
and faster snprintf().

Along the way, fflush(3) is called, and since the callback writer
function returns zero instead of the length "written", __SERR
naturally gets set on the filedescriptor.

The extensible printf, in difference from the normal printf refuses
to output anything to an __SERR marked filedescriptor, and thus
the actual syslog message is supressed.

MFC:	after 2 weeks
2006-08-07 20:12:18 +00:00
David Xu
3a31b448c0 Save errno before calling pthread_mutex_lock because pthread_mutex_lock
may change errno unexpectly.
2006-05-11 09:10:33 +00:00
Daniel Eischen
ed0b0abcf7 Make syslog() use the internal (non-cancellation point) _usleep().
Prior to this it was calling the cancellable usleep() while holding
a lock.
2005-08-03 00:45:58 +00:00
Stefan Farfeleder
fd42c4d829 Use prototypes in the function definitions. 2005-04-08 21:15:38 +00:00
Gleb Smirnoff
9657520621 Make syslog(3) thread safe.
PR:		bin/72394
Submitted by:	Dan Nelson
Reviewed by:	deischen
MFC after:	2 weeks
2004-12-30 16:05:11 +00:00
Gleb Smirnoff
8ba85e7741 Restore standard behavior: log to console only when normal logging
failed, not always.

PR:		bin/75356
Submitted by:	Mark Knight <markk knigma org>
Pointy hat to:	glebius
MFC after:	3 days
2004-12-24 09:48:44 +00:00
Gleb Smirnoff
240d5a9b1c Protect against local flooder of /var/run/log. Do not loop forever in
syslog(3) if we are a priveleged program (sshd, su, etc.).

- Make syslogd open an additional socket /var/run/logpriv, with 0600
  permissions.
- In libc, try to use this socket.
- Do not loop forever if we are using this socket (partial backout of 1.31)

Reviewed by:	dwmalone, Andrea Campi <andrea webcom it>
Approved by:	julian (mentor)
MFC after:	1 month
2004-11-04 23:09:57 +00:00
Gleb Smirnoff
2e89951b6f When send()ing to syslogd return ENOBUFS keep trying until success.
This fixes a case, when DoSed syslogd completely loses messages.

PR:		bin/72366
Discussed with:	dwmalone, millert@OpenBSD.org
Approved by:	julian (mentor)
Obtained from:	OpenBSD (rev. 1.17, 1.21 by millert)
MFC after:	3 months
2004-10-08 21:15:21 +00:00
Diomidis Spinellis
857b57eae1 Remove a trailing newline, to behave as documented in syslog(3):
"A trailing newline is added if none is present."

The code in syslogd, stderr, and console output always adds a newline
at the EOL.  However, the existing code never actually removed a
trailing newline, and apparently relied on syslogd to convert it
into a space character.  Thus, the existing newline was converted
to a trailing space at the EOL by syslogd, while stderr, and console
output resulted in an empty line.

MFC after:	2 weeks
2004-05-10 17:12:52 +00:00
Alfred Perlstein
e6cfb1ccd3 Handle %%m properly in syslog format string. Previously it would expand
the %m into the errno and then vfprintf would expand the % and the first
character of the strerror(3) return causing possible data corruption.
2003-02-10 08:31:28 +00:00
Ruslan Ermilov
bedff4e805 Reset LogTag to NULL in closelog(3). This fixes mysterious crashes
caused by dynamic PAM modules that call openlog(3) and closelog(3),
e.g. ports/security/pam_pwdfile.

What happened here is that the module first registered its "ident"
with openlog(3), then PAM library unloaded module with dlclose(3),
and the next call to syslog(3) resulted in SIGSEGV.

MFC after:	3 days
2002-11-14 12:40:14 +00:00
Alfred Perlstein
a82bbc730e Assume __STDC__, remove non-__STDC__ code.
Submitted by: keramida
2002-05-28 17:03:12 +00:00
Mark Murray
4cd0119367 Do not use __progname directly (except in [gs]etprogname(3)).
Also, make an internal _getprogname() that is used only inside
libc. For libc, getprogname(3) is a weak symbol in case a
function of the same name is defined in userland.
2002-03-29 22:43:43 +00:00
David Malone
94998878b7 If syslog fails to talk to syslogd, then it tries to write a message
to the console in a final attempt to log something. Make this final
attempt non-blocking so that a blocking console doesn't end up
blocking process which attempt to syslog something.

In particular, this means you should be able to su and fix the
problem if the console becomes blocking.

MFC after:	3 weeks
2002-03-09 11:38:01 +00:00
David E. O'Brien
b231cb3945 * Remove __P and convert to ANSI prototypes.
* Remove 'register'.  (some functions had 7+ register functions...)
* Fix SCM ID's.
2002-02-01 01:32:19 +00:00
David Malone
ab09ef008f Use ctime_r to build the timestamp for syslog, so that we don't
clobber a ctime buffer which is passed in.

PR:		34022
Submitted by:	Hartmut Brandt <brandt@fokus.gmd.de>
MFC after:	2 weeks
2002-01-25 21:49:34 +00:00
Daniel Eischen
d201fe46e3 Remove _THREAD_SAFE and make libc thread-safe by default by
adding (weak definitions to) stubs for some of the pthread
functions.  If the threads library is linked in, the real
pthread functions will pulled in.

Use the following convention for system calls wrapped by the
threads library:
	__sys_foo - actual system call
	_foo - weak definition to __sys_foo
	foo - weak definition to __sys_foo

Change all libc uses of system calls wrapped by the threads
library from foo to _foo.  In order to define the prototypes
for _foo(), we introduce namespace.h and un-namespace.h
(suggested by bde).  All files that need to reference these
system calls, should include namespace.h before any standard
includes, then include un-namespace.h after the standard
includes and before any local includes.  <db.h> is an exception
and shouldn't be included in between namespace.h and
un-namespace.h  namespace.h will define foo to _foo, and
un-namespace.h will undefine foo.

Try to eliminate some of the recursive calls to MT-safe
functions in libc/stdio in preparation for adding a mutex
to FILE.  We have recursive mutexes, but would like to avoid
using them if possible.

Remove uneeded includes of <errno.h> from a few files.

Add $FreeBSD$ to a few files in order to pass commitprep.

Approved by:	-arch
2001-01-24 13:01:12 +00:00
Jason Evans
9233c4d942 Simplify sytem call renaming. Instead of _foo() <-- _libc_foo <-- foo(),
just use _foo() <-- foo().  In the case of a libpthread that doesn't do
call conversion (such as linuxthreads and our upcoming libpthread), this
is adequate.  In the case of libc_r, we still need three names, which are
now _thread_sys_foo() <-- _foo() <-- foo().

Convert all internal libc usage of: aio_suspend(), close(), fsync(), msync(),
nanosleep(), open(), fcntl(), read(), and write() to _foo() instead of foo().

Remove all internal libc usage of: creat(), pause(), sleep(), system(),
tcdrain(), wait(), and waitpid().

Make thread cancellation fully POSIX-compliant.

Suggested by:	deischen
2000-01-27 23:07:25 +00:00
Jason Evans
929273386f Add three-tier symbol naming in support of POSIX thread cancellation
points.  For library functions, the pattern is __sleep() <--
_libc_sleep() <-- sleep().  The arrows represent weak aliases.  For
system calls, the pattern is _read() <-- _libc_read() <-- read().
2000-01-12 09:23:48 +00:00
Peter Wemm
7f3dea244c $Id$ -> $FreeBSD$ 1999-08-28 00:22:10 +00:00
Brian Somers
8d41a9efa3 Go back to version 1.16 - it was correct the way it was.
Pointed out by:	bde
1998-05-01 19:41:12 +00:00
Brian Somers
e79dc52bbf connect() returns -1 on error - not 0. 1998-05-01 01:16:39 +00:00
Brian Somers
0b3b961e55 We don't need to NUL terminate our sun_path.
Pointed out by: Garrett Wollman <wollman@khavrinen.lcs.mit.edu>
1998-03-06 03:10:49 +00:00
Brian Somers
0d41e7b820 Nul terminate sockaddr_un::sun_path
Suggested by: Theo de Raadt <deraadt@openbsd.org>
1998-03-06 02:12:02 +00:00
Brian Somers
d584948ecd Make SyslogAddr a sockaddr_un rather than a sockaddr.
This wasn't a problem in practice as PATH_LOG and PATH_OLDLOG
are both < sizeof sockaddr::sa_data.
1998-03-05 22:17:59 +00:00
John Polstra
cf49f43912 Add backward compatibility so that static executables built on
modern FreeBSD systems will syslog properly on older systems that
still name the logging socket "/dev/log".  This includes pre-2.2
versions of FreeBSD as well as BSD/OS systems.  If the connect to
"/var/run/log" fails, the function now tries to connect to
"/dev/log" as a fallback.
1997-03-20 16:28:27 +00:00
Peter Wemm
adf6ad9e69 Merge from Lite2:
filesystem include updates, duplicate group suppression, cleanups,
  filesystem whiteout support (unionfs), bidir popen().
1997-03-11 11:52:33 +00:00
Peter Wemm
7e546392b5 Revert $FreeBSD$ to $Id$ 1997-02-22 15:12:41 +00:00
Jordan K. Hubbard
1130b656e5 Make the long-awaited change from $Id$ to $FreeBSD$
This will make a number of things easier in the future, as well as (finally!)
avoiding the Id-smashing problem which has plagued developers for so long.

Boy, I'm glad we're not using sup anymore.  This update would have been
insane otherwise.
1997-01-14 07:20:47 +00:00
Jordan K. Hubbard
51295a4d3e General -Wall warning cleanup, part I.
Submitted-By: Kent Vander Velden <graphix@iastate.edu>
1996-07-12 18:57:58 +00:00
Peter Wemm
7e6ace8561 If the send() to the AF_UNIX socket to the syslogd fails, attempt to
reconnect once using the saved openlog() parameters.

This helps one of the system startup race conditions. If syslogd takes too
long to get going, some daemons can fail the connection and forever log
to the console even though the syslogd is running.  That is ..unfortunate..
1996-03-02 19:56:16 +00:00
Poul-Henning Kamp
8b10240709 Minor cleanup, mostly unused vars and missing #includes. 1995-10-22 14:37:11 +00:00
Peter Wemm
fc370289ab Add $Id$ since this version of the file has diverged from the BSD base a
fair bit.  I forgot to add it when I made the fixes some time ago.
1995-10-21 07:05:01 +00:00
Peter Wemm
11e67a9f2e Fix the problem that I aroused with the last commit..
What was happening, is if syslogd was not running, syslog() would do
a strcat("\r\n") on a non-null-terminated buffer, and write it to the console.

This meant that sometimes extra characters could be written to the console
during boot, depending on the stack contents.

This totally avoids the potential problem by using writev() like the rest
of the does, and avoid modifying the buffer after the trouble we've gone to
to carefully protect it.

This is actually a trivial fix, in spite of the long commit message.. :-)
It only appeared during boot and shutdown with syslogd stopped.
1995-10-05 00:11:15 +00:00
Peter Wemm
7c8e2aa48c Fix security bugs with a "new approach", using stdio's powerful buffer
control hooks.
It is similar to an unrolled multi-part snprintf(), in that a "FILE *" is
attached to a string buffer.  There is also an optimisation for the case
where the syslog format string does not contain %m, which should improve
performance of "informational" logging, like from ftpd.
1995-09-15 13:53:39 +00:00