Commit Graph

109 Commits

Author SHA1 Message Date
Christian Brauner
cc73685dd0
lxc: switch to SPDX
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2019-12-04 13:48:46 +01:00
Christian Brauner
d07545c7da
compiler: -Wformat=2 hardening
Enable -Wformat plus additional format checks. Currently equivalent to
-Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2019-02-05 22:36:31 +01:00
2xsec
809d44495a
log: replace write with lxc_write_nointr
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-11-08 19:01:03 +09:00
2xsec
9b681921e5
log: fix too wide or inconsistent non-owner permissions
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-11-02 15:25:14 +09:00
Josh Soref
29fa2d33b4 spelling: subtracting
Signed-off-by: Josh Soref <jsoref@gmail.com>
2018-10-30 08:11:56 +00:00
Josh Soref
95db7d8c59 spelling: initialize
Signed-off-by: Josh Soref <jsoref@gmail.com>
2018-10-30 07:13:17 +00:00
Josh Soref
25fa6f8ccb spelling: control
Signed-off-by: Josh Soref <jsoref@gmail.com>
2018-10-30 06:54:21 +00:00
Christian Brauner
d38dd64a51
tree-wide: fix includes to fix bionic builds
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-27 01:01:33 +02:00
Christian Brauner
6333c91505
tree-wide: replace sizeof() with SIZEOF2STRLEN()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-23 12:56:13 +02:00
2xsec
de1c05f339
log: add additional info of dlog
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-09-14 18:28:44 +09:00
2xsec
8178adc6ad
log: add common functions
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-09-14 18:16:04 +09:00
2xsec
5c7bfc0231
log: support dlog
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-09-11 16:04:25 +09:00
Christian Brauner
a9d4137983
log: log_append_logfile() add new error path
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-04 14:05:28 +02:00
Christian Brauner
979a0d9354
Revert "Revert "tree-wide: use sizeof on static arrays""
This reverts commit 2fb7cf0b32.

The problem wasn't caused by the reverted commit and was fixed in

commit 0c9b1f826d ("macro: calculate buffer lengths correctly")

The full explanation can be taken from the following irc excerpt from
the #lxc-dev channel:

│19:54:47 brauner | there was a bug in one of the standard macros we used
│19:55:01 brauner | and the changes by INTTYPE_TO_STRLEN() caused the issue to surface
│19:55:03 brauner | which is good
│19:55:16 brauner | i sent a branch and stgraber merged it that fixes it
│19:57:56  Blub\0 | so...
│19:58:31  Blub\0 | still doesn't explain how it was the sizeof() patch
│20:07:14 brauner | Blub\0: so here's the long explanation
│20:07:35 brauner | Blub\0: stgraber bumped pid_max on our jenkins test builders
│20:07:53 brauner | Blub\0: because we're running *a lot* of containers
│20:07:56 brauner | in any case
│20:08:06 brauner | there was a buffer
│20:08:12 brauner | LXC_LSMATTRLEN
│20:08:59 brauner | it used to be
│20:09:03 brauner | -/* /proc/pid-to-str/attr/current = (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1) */
│20:09:03 brauner | -#define LXC_LSMATTRLEN (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
│20:09:14 brauner | which one can see is wrong
│20:09:21 brauner | before the INTTYPE patchset
│20:09:40 brauner | INTTYPE_TO_STRLEN(pid_t) was LXC_NUMSTRLEN64
│20:09:45 brauner | which gave you 21 chars
│20:09:57 brauner | so it accounted for the missing parts
│20:10:03 brauner | because the correct macro should've been
│20:10:17 brauner | +/* /proc/        = 6
│20:10:17 brauner | + *               +
│20:10:17 brauner | + * <pid-as-str>  = INTTYPE_TO_STRLEN(pid_t)
│20:10:17 brauner | + *               +
│20:10:17 brauner | + * /attr/        = 6
│20:10:17 brauner | + *               +
│20:10:17 brauner | + * /current      = 8
│20:10:17 brauner | + *               +
│20:10:17 brauner | + * \0            = 1
│20:10:17 brauner | + */
│20:10:17 brauner | +#define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 8 + 1)
│20:10:24  Blub\0 | still
│20:10:31 brauner | the issue was only seen
│20:10:39 brauner | when the pid number hit a specific maximum
│20:10:50  Blub\0 | the sizeof patch only changed instances of actual char buf[A_FIXED_NUMBER] + snprintf(buf, A_FIXED_NUMBER, ...)
│20:10:54 brauner | aka exceeded the newly shortened buffer
│20:11:42 brauner | your patch was a red herring
│20:12:03  Blub\0 | I guess
│20:12:06 brauner | it didn't cause it
│20:12:14 brauner | it just surfaced at the same time it was merged
│20:12:25  Blub\0 | so we can revert the revert then? :)
│20:12:35 brauner | yes, that was th eplan all along

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-09-01 20:25:07 +02:00
Stéphane Graber
2fb7cf0b32
Revert "tree-wide: use sizeof on static arrays"
This reverts commit 81a3bb64b4.

This commit broke all builders running with pid_max > 32768.

Reverting for now so we can bring the build farm back online.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
2018-08-31 11:58:17 -07:00
Wolfgang Bumiller
81a3bb64b4 tree-wide: use sizeof on static arrays
Instead of duplicating their lengths in read/snprintf/...
calls.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2018-08-29 21:46:39 +02:00
Christian Brauner
3a2c65f877
log: s/LXC_NUMSTRLEN64/INTTYPE_TO_STRLEN()/
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-08-28 20:14:52 +02:00
2xsec
df05fa0f04
fix thread safe issue(localtime => localtime_r)
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-08-13 16:53:30 +09:00
2xsec
03c6d26669
log: add default log priority
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-08-06 13:02:06 +09:00
2xsec
8273ba47c1
log: function cleanups
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-08-06 13:01:55 +09:00
Christian Brauner
202a334b47
log: bugfixes
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-08-04 20:49:05 +02:00
Christian Brauner
e0c198063d
log: handle EINTR in read()
We don't want to link log.{c,h} against utils.{c,h} for the sake of our static
builds init.lxc.static. This means lxc_write_nointr() will not be available. So
handle it EINTR.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-07-30 00:16:41 +02:00
Stéphane Graber
9449f425e7
Merge pull request #2446 from brauner/2018-07-02/log_prefix
Logging and cgroup fixes
2018-07-04 12:19:03 -04:00
2xsec
7d20477698
log: remove the trailing .
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-07-04 17:01:45 +09:00
Christian Brauner
ac2cecc4b8
tree-wide: remove unneeded log prefixes
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-07-03 12:48:13 +02:00
2xsec
6d1400b572
log: change ERROR macro using sterror to SYSERROR
Signed-off-by: 2xsec <dh48.jeong@samsung.com>
2018-06-26 17:27:19 +09:00
Christian Brauner
c7b1705112
log: enable per-thread container name prefix
When using the LXC API multi-thread and users initialize a log:

struct lxc_log log;
log.name = "my-log";
lxc_log_init(&log);

all threads will have the same "my-log" prefix even though thy might call
lxc_container_new() in separate threads. There is currently no easy way to
handle per-thread container name prefixes.
To handle this carry a reference to the name of the container in struct
lxc_conf and if no log.name was set, use it by default. This way each thread
will get the container it is currently working on as a log-prefix.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Reported-by: duguhaotian <duguhaotian@gmail.com>
2018-05-20 14:05:51 +02:00
Christian Brauner
9de31d5a13
tree-wide: s/strncpy()/strlcpy()/g
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2018-05-11 13:32:01 +02:00
Tycho Andersen
512845bc10 remove leading whitespace from log files
This has annoyed me for a long time, 3.0 seems like the time to fix it :).

I think the way that the log prefix was intended to be used was perhaps a
dynamic prefix per file, but we don't do that today; we include the
filename later in the log message. Instead, we use it as the tool name,
which for liblxc is always "lxc", but could also be things like
"lxc-cgroup" or whatever. There is absolutely no reason to pad this, since
it is always the same for every log file (in fact, we could probably get
rid of the prefix all together, but that seems slightly more drastic).

Instead, let's just drop this padding. Hopefully this will save thousands
of hours of slight annoyance and right scrolling in various pastebins.

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
2018-03-22 09:49:08 -06:00
Felix Abecassis
26e92b0eab log: fix infinite loop with multiple lxc.log.syslog keys
This caused the linked list of appenders to loop on itself, creating
an infinite logging loop in `__lxc_log_append`.

Signed-off-by: Felix Abecassis <fabecassis@nvidia.com>
2017-11-28 20:41:51 -08:00
Christian Brauner
be5c06ece4
log: check for i/o error with vsnprintf()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-11-07 00:54:27 +01:00
Christian Brauner
ecc94d764f Merge pull request #1860 from kilobyte/master
fix build failures on x32
2017-10-19 00:25:16 +02:00
Adam Borowski
291f64ac27 Fix a format string build failure on x32.
Both of struct timespec fields are 64-bit on any new architecture, even
32-bit ones.

Signed-off-by: Adam Borowski <kilobyte@angband.pl>
2017-10-16 15:12:07 +02:00
Christian Brauner
b0a507d7b4
log: prevent stack smashing
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-10-16 12:22:20 +02:00
Christian Brauner
1a0e70ace8
tree-wide: non-functional changes
- replace all "//" with "/* */"

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-08-24 12:45:47 +02:00
0x0916
46cc906dc0 confile: namespace lxc.log keys
* rename lxc.logfile to lxc.log.file
* renaem lxc.loglevel to lxc.log.level
* rename lxc.syslog to lxc.log.syslog

the legacy keys will be kept around until LXC 3.0 and then will be
removed.

Signed-off-by: 0x0916 <w@laoqinren.net>
2017-06-30 09:41:04 +08:00
Christian Brauner
4b73005c60
tree-wide: priority -> level
When we merged the new logging function for the api we exposed the log level
argument in the struct as "priority" which we actually requested to be changed
to "level" which somehow didn't happen and we missed it. Given the fact there
has been no new liblxc release let's fix it right now before it hits users.
Also, take the chance to change the terminology in the log from "priority" to
"level" globally. This is to prevent confusion with syslog's "priority"
argument which we also support.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-06-15 12:13:04 +02:00
0x0916
73b910a341 API: expose function lxc_log_init
Signed-off-by: 0x0916 <w@laoqinren.net>
2017-06-06 11:55:31 +08:00
Christian Brauner
86698d3885
log: fix lxc_unix_epoch_to_utc()
The conversion algorithm used uses a clever trick by letting a year start at 1
March. So we need to add 1 for January and February.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-02-27 16:02:24 +01:00
Christian Brauner
97bc24229c
tree-wide: Sic semper assertis!
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2017-01-04 15:19:46 +01:00
Stéphane Graber
f8110a8991 Merge pull request #1334 from brauner/2016-11-29/correct_date_formatting
log: make sure that date is correctly formatted
2016-11-29 13:34:02 -05:00
Christian Brauner
9d7468fdd3
log: make sure that date is correctly formatted
- single digit months, days, hours, minutes, and seconds should always be
  preceded by a 0.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 07:52:30 +01:00
Christian Brauner
eab15c1ee6
tree-wide: random macro cleanups
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 07:29:32 +01:00
Christian Brauner
d86c0d08c4
log: annotate lxc_unix_epoch_to_utc()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-29 01:26:25 +01:00
Christian Brauner
e1378d3591
log: use lxc_unix_epoch_to_utc()
This allows us to generate nice timestamps in a thread-safe manner without
relying on locale touching functions from any libc.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-28 22:22:14 +01:00
Christian Brauner
65a9df895e
log: add lxc_unix_epoch_to_utc()
Converts a unix time Epoch given by a struct timespec to a UTC string useable
in our logging functions. Maybe expanded to allow for more generic formatting.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-28 22:22:12 +01:00
Christian Brauner
c57dbb9658
log: drop all timezone conversion functions
Our log functions need to make extra sure that they are thread-safe. We had
some problems with that before. This especially involves time-conversion
functions. I don't want to find any localtime() or gmtime() functions or
relatives in here. Not even localtime_r() or gmtime_r() or relatives. They all
fiddle with global variables and locking in various libcs. They cause deadlocks
when liblxc is used multi-threaded and no matter how smart you think you are,
you __will__ cause trouble using them.
(As a short example how this can cause trouble: LXD uses forkstart to fork off
a new process that runs the container. At the same time the go runtime LXD
relies on does its own multi-threading thing which we can't control. The
fork()ing + threading then seems to mess with the locking states in these time
functions causing deadlocks.)
The current solution is to be good old unix people and use the Epoch as our
reference point and simply use the seconds and nanoseconds that have past since
then. This relies on clock_gettime() which is explicitly marked MT-Safe with no
restrictions! This way, anyone who is really strongly invested in getting the
actual time the log entry was created, can just convert it for themselves. Our
logging is mostly done for debugging purposes so don't try to make it pretty.
Pretty might cost you thread-safety.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-28 05:27:39 +01:00
Christian Brauner
450b6d3d68
log: use N/A if getpid() != gettid() when threaded
Sample log output:

    lxc 20161125201943.819 INFO     lxc_start - start.c:lxc_check_inherited:243 - Closed inherited fd: 54.
--> lxc N/A                INFO     lxc_monitor - monitor.c:lxc_monitor_sock_name:178 - using monitor sock name lxc/ad055575fe28ddd5//var/lib/lxc
    lxc 20161125201943.958 DEBUG    lxc_commands - commands.c:lxc_cmd_handler:893 - peer has disconnected
--> lxc N/A                DEBUG    lxc_commands - commands.c:lxc_cmd_get_state:579 - 'lxc-test-concurrent-0' is in 'RUNNING' state
    lxc 20161125201943.960 DEBUG    lxc_commands - commands.c:lxc_cmd_handler:893 - peer has disconnected
    lxc 20161125201944.009 INFO     lxc_start - start.c:lxc_check_inherited:243 - Closed inherited fd: 3.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 20:24:26 +01:00
Christian Brauner
0dcdbf8a67
log: fix race
localtime_r() can lead to deadlocks because it calls __tzset() and
__tzconvert() internally. The deadlock stems from an interaction between these
functions and the functions in monitor.c and commands.{c,h}. The latter
functions will write to the log independent of the container thread that is
currently running. Since the monitor fork()ed it seems to duplicate the mutex
states of the time functions mentioned above causing the deadlock.
As a short termm fix, I suggest to simply disable receiving the time when
monitor.c or command.{c,h} functions are called. This should be ok, since the
[lxc monitor] will only emit a few messages and thread-safety is currently more
important than beautiful logs. The rest of the log stays the same as it was
before.

Here is an example output from logs where I printed the pid and tid of the
process that is currently writing to the log:

            lxc 20161125170200.619 INFO     lxc_start:   18695-18695: - start.c:lxc_check_inherited:243 - Closed inherited fd: 23.
            lxc 20161125170200.640 DEBUG    lxc_start:   18677-18677: - start.c:__lxc_start:1334 - Not dropping CAP_SYS_BOOT or watching utmp.
            lxc 20161125170200.640 INFO     lxc_cgroup:  18677-18677: - cgroups/cgroup.c:cgroup_init:68 - cgroup driver cgroupfs-ng initing for lxc-test-concurrent-0

----------> lxc 20150427012246.000 INFO     lxc_monitor: 13017-18622: - monitor.c:lxc_monitor_sock_name:178 - using monitor sock name lxc/ad055575fe28ddd5//var/lib/lxc

            lxc 20161125170200.662 DEBUG    lxc_cgfsng:  18677-18677: - cgroups/cgfsng.c:filter_and_set_cpus:478 - No isolated cpus detected.
            lxc 20161125170200.662 DEBUG    lxc_cgfsng:  18677-18677: - cgroups/cgfsng.c:handle_cpuset_hierarchy:648 - "cgroup.clone_children" was already set to "1".

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 20:04:23 +01:00
Christian Brauner
6a22e86258
configure: check whether gettid() is declared
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
2016-11-25 20:03:50 +01:00