2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
22 #include "bus-internal.h"
23 #include "bus-message.h"
26 #include "capability-util.h"
28 #include "format-util.h"
29 #include "locale-util.h"
31 #include "string-util.h"
33 #include "terminal-util.h"
36 static char *indent(unsigned level
, unsigned flags
) {
42 if (flags
& BUS_MESSAGE_DUMP_SUBTREE_ONLY
&& level
> 0)
45 if (flags
& BUS_MESSAGE_DUMP_WITH_HEADER
)
48 p
= new(char, n
+ level
*8 + 1);
52 if (flags
& BUS_MESSAGE_DUMP_WITH_HEADER
) {
57 memset(p
+ i
, ' ', level
*8);
63 int bus_message_dump(sd_bus_message
*m
, FILE *f
, unsigned flags
) {
72 if (flags
& BUS_MESSAGE_DUMP_WITH_HEADER
) {
74 "%s%s%s Type=%s%s%s Endian=%c Flags=%u Version=%u Priority=%"PRIi64
,
75 m
->header
->type
== SD_BUS_MESSAGE_METHOD_ERROR
? ansi_highlight_red() :
76 m
->header
->type
== SD_BUS_MESSAGE_METHOD_RETURN
? ansi_highlight_green() :
77 m
->header
->type
!= SD_BUS_MESSAGE_SIGNAL
? ansi_highlight() : "", special_glyph(TRIANGULAR_BULLET
), ansi_normal(),
78 ansi_highlight(), bus_message_type_to_string(m
->header
->type
), ansi_normal(),
84 /* Display synthetic message serial number in a more readable
85 * format than (uint32_t) -1 */
86 if (BUS_MESSAGE_COOKIE(m
) == 0xFFFFFFFFULL
)
87 fprintf(f
, " Cookie=-1");
89 fprintf(f
, " Cookie=%" PRIu64
, BUS_MESSAGE_COOKIE(m
));
91 if (m
->reply_cookie
!= 0)
92 fprintf(f
, " ReplyCookie=%" PRIu64
, m
->reply_cookie
);
97 fprintf(f
, " Sender=%s%s%s", ansi_highlight(), m
->sender
, ansi_normal());
99 fprintf(f
, " Destination=%s%s%s", ansi_highlight(), m
->destination
, ansi_normal());
101 fprintf(f
, " Path=%s%s%s", ansi_highlight(), m
->path
, ansi_normal());
103 fprintf(f
, " Interface=%s%s%s", ansi_highlight(), m
->interface
, ansi_normal());
105 fprintf(f
, " Member=%s%s%s", ansi_highlight(), m
->member
, ansi_normal());
107 if (m
->sender
|| m
->destination
|| m
->path
|| m
->interface
|| m
->member
)
110 if (sd_bus_error_is_set(&m
->error
))
113 " ErrorMessage=%s\"%s\"%s\n",
114 ansi_highlight_red(), strna(m
->error
.name
), ansi_normal(),
115 ansi_highlight_red(), strna(m
->error
.message
), ansi_normal());
117 if (m
->monotonic
!= 0)
118 fprintf(f
, " Monotonic="USEC_FMT
, m
->monotonic
);
119 if (m
->realtime
!= 0)
120 fprintf(f
, " Realtime="USEC_FMT
, m
->realtime
);
122 fprintf(f
, " SequenceNumber=%"PRIu64
, m
->seqnum
);
124 if (m
->monotonic
!= 0 || m
->realtime
!= 0 || m
->seqnum
!= 0)
127 bus_creds_dump(&m
->creds
, f
, true);
130 r
= sd_bus_message_rewind(m
, !(flags
& BUS_MESSAGE_DUMP_SUBTREE_ONLY
));
132 return log_error_errno(r
, "Failed to rewind: %m");
134 if (!(flags
& BUS_MESSAGE_DUMP_SUBTREE_ONLY
)) {
135 _cleanup_free_
char *prefix
= NULL
;
137 prefix
= indent(0, flags
);
141 fprintf(f
, "%sMESSAGE \"%s\" {\n", prefix
, strempty(m
->root_container
.signature
));
145 _cleanup_free_
char *prefix
= NULL
;
146 const char *contents
= NULL
;
161 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
163 return log_error_errno(r
, "Failed to peek type: %m");
169 r
= sd_bus_message_exit_container(m
);
171 return log_error_errno(r
, "Failed to exit container: %m");
175 prefix
= indent(level
, flags
);
179 fprintf(f
, "%s};\n", prefix
);
183 prefix
= indent(level
, flags
);
187 if (bus_type_is_container(type
) > 0) {
188 r
= sd_bus_message_enter_container(m
, type
, contents
);
190 return log_error_errno(r
, "Failed to enter container: %m");
192 if (type
== SD_BUS_TYPE_ARRAY
)
193 fprintf(f
, "%sARRAY \"%s\" {\n", prefix
, contents
);
194 else if (type
== SD_BUS_TYPE_VARIANT
)
195 fprintf(f
, "%sVARIANT \"%s\" {\n", prefix
, contents
);
196 else if (type
== SD_BUS_TYPE_STRUCT
)
197 fprintf(f
, "%sSTRUCT \"%s\" {\n", prefix
, contents
);
198 else if (type
== SD_BUS_TYPE_DICT_ENTRY
)
199 fprintf(f
, "%sDICT_ENTRY \"%s\" {\n", prefix
, contents
);
206 r
= sd_bus_message_read_basic(m
, type
, &basic
);
208 return log_error_errno(r
, "Failed to get basic: %m");
214 case SD_BUS_TYPE_BYTE
:
215 fprintf(f
, "%sBYTE %s%u%s;\n", prefix
, ansi_highlight(), basic
.u8
, ansi_normal());
218 case SD_BUS_TYPE_BOOLEAN
:
219 fprintf(f
, "%sBOOLEAN %s%s%s;\n", prefix
, ansi_highlight(), true_false(basic
.i
), ansi_normal());
222 case SD_BUS_TYPE_INT16
:
223 fprintf(f
, "%sINT16 %s%i%s;\n", prefix
, ansi_highlight(), basic
.s16
, ansi_normal());
226 case SD_BUS_TYPE_UINT16
:
227 fprintf(f
, "%sUINT16 %s%u%s;\n", prefix
, ansi_highlight(), basic
.u16
, ansi_normal());
230 case SD_BUS_TYPE_INT32
:
231 fprintf(f
, "%sINT32 %s%i%s;\n", prefix
, ansi_highlight(), basic
.s32
, ansi_normal());
234 case SD_BUS_TYPE_UINT32
:
235 fprintf(f
, "%sUINT32 %s%u%s;\n", prefix
, ansi_highlight(), basic
.u32
, ansi_normal());
238 case SD_BUS_TYPE_INT64
:
239 fprintf(f
, "%sINT64 %s%"PRIi64
"%s;\n", prefix
, ansi_highlight(), basic
.s64
, ansi_normal());
242 case SD_BUS_TYPE_UINT64
:
243 fprintf(f
, "%sUINT64 %s%"PRIu64
"%s;\n", prefix
, ansi_highlight(), basic
.u64
, ansi_normal());
246 case SD_BUS_TYPE_DOUBLE
:
247 fprintf(f
, "%sDOUBLE %s%g%s;\n", prefix
, ansi_highlight(), basic
.d64
, ansi_normal());
250 case SD_BUS_TYPE_STRING
:
251 fprintf(f
, "%sSTRING \"%s%s%s\";\n", prefix
, ansi_highlight(), basic
.string
, ansi_normal());
254 case SD_BUS_TYPE_OBJECT_PATH
:
255 fprintf(f
, "%sOBJECT_PATH \"%s%s%s\";\n", prefix
, ansi_highlight(), basic
.string
, ansi_normal());
258 case SD_BUS_TYPE_SIGNATURE
:
259 fprintf(f
, "%sSIGNATURE \"%s%s%s\";\n", prefix
, ansi_highlight(), basic
.string
, ansi_normal());
262 case SD_BUS_TYPE_UNIX_FD
:
263 fprintf(f
, "%sUNIX_FD %s%i%s;\n", prefix
, ansi_highlight(), basic
.i
, ansi_normal());
267 assert_not_reached("Unknown basic type.");
271 if (!(flags
& BUS_MESSAGE_DUMP_SUBTREE_ONLY
)) {
272 _cleanup_free_
char *prefix
= NULL
;
274 prefix
= indent(0, flags
);
278 fprintf(f
, "%s};\n\n", prefix
);
284 static void dump_capabilities(
289 int (*has
)(sd_bus_creds
*c
, int capability
)) {
291 unsigned long i
, last_cap
;
305 fprintf(f
, "%s%s=%s", terse
? " " : "", name
, terse
? "" : ansi_highlight());
306 last_cap
= cap_last_cap();
314 fprintf(f
, terse
? "\n " : "\n ");
316 fprintf(f
, "%s", strna(capability_to_name(i
)));
331 fputs(ansi_normal(), f
);
334 int bus_creds_dump(sd_bus_creds
*c
, FILE *f
, bool terse
) {
335 uid_t owner
, audit_loginuid
;
336 uint32_t audit_sessionid
;
337 char **cmdline
= NULL
, **well_known
= NULL
;
338 const char *prefix
, *color
, *suffix
, *s
;
354 color
= ansi_highlight();
357 suffix
= strjoina(off
, "\n");
360 if (c
->mask
& SD_BUS_CREDS_PID
)
361 fprintf(f
, "%sPID=%s"PID_FMT
"%s", prefix
, color
, c
->pid
, suffix
);
362 if (c
->mask
& SD_BUS_CREDS_TID
)
363 fprintf(f
, "%sTID=%s"PID_FMT
"%s", prefix
, color
, c
->tid
, suffix
);
364 if (c
->mask
& SD_BUS_CREDS_PPID
) {
366 fprintf(f
, "%sPPID=%sn/a%s", prefix
, color
, suffix
);
368 fprintf(f
, "%sPPID=%s"PID_FMT
"%s", prefix
, color
, c
->ppid
, suffix
);
370 if (c
->mask
& SD_BUS_CREDS_TTY
)
371 fprintf(f
, "%sTTY=%s%s%s", prefix
, color
, strna(c
->tty
), suffix
);
373 if (terse
&& ((c
->mask
& (SD_BUS_CREDS_PID
|SD_BUS_CREDS_TID
|SD_BUS_CREDS_PPID
|SD_BUS_CREDS_TTY
))))
376 if (c
->mask
& SD_BUS_CREDS_UID
)
377 fprintf(f
, "%sUID=%s"UID_FMT
"%s", prefix
, color
, c
->uid
, suffix
);
378 if (c
->mask
& SD_BUS_CREDS_EUID
)
379 fprintf(f
, "%sEUID=%s"UID_FMT
"%s", prefix
, color
, c
->euid
, suffix
);
380 if (c
->mask
& SD_BUS_CREDS_SUID
)
381 fprintf(f
, "%sSUID=%s"UID_FMT
"%s", prefix
, color
, c
->suid
, suffix
);
382 if (c
->mask
& SD_BUS_CREDS_FSUID
)
383 fprintf(f
, "%sFSUID=%s"UID_FMT
"%s", prefix
, color
, c
->fsuid
, suffix
);
384 r
= sd_bus_creds_get_owner_uid(c
, &owner
);
386 fprintf(f
, "%sOwnerUID=%s"UID_FMT
"%s", prefix
, color
, owner
, suffix
);
387 if (c
->mask
& SD_BUS_CREDS_GID
)
388 fprintf(f
, "%sGID=%s"GID_FMT
"%s", prefix
, color
, c
->gid
, suffix
);
389 if (c
->mask
& SD_BUS_CREDS_EGID
)
390 fprintf(f
, "%sEGID=%s"GID_FMT
"%s", prefix
, color
, c
->egid
, suffix
);
391 if (c
->mask
& SD_BUS_CREDS_SGID
)
392 fprintf(f
, "%sSGID=%s"GID_FMT
"%s", prefix
, color
, c
->sgid
, suffix
);
393 if (c
->mask
& SD_BUS_CREDS_FSGID
)
394 fprintf(f
, "%sFSGID=%s"GID_FMT
"%s", prefix
, color
, c
->fsgid
, suffix
);
396 if (c
->mask
& SD_BUS_CREDS_SUPPLEMENTARY_GIDS
) {
399 fprintf(f
, "%sSupplementaryGIDs=%s", prefix
, color
);
400 for (i
= 0; i
< c
->n_supplementary_gids
; i
++)
401 fprintf(f
, "%s" GID_FMT
, i
> 0 ? " " : "", c
->supplementary_gids
[i
]);
402 fprintf(f
, "%s", suffix
);
405 if (terse
&& ((c
->mask
& (SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_SUID
|SD_BUS_CREDS_FSUID
|
406 SD_BUS_CREDS_GID
|SD_BUS_CREDS_EGID
|SD_BUS_CREDS_SGID
|SD_BUS_CREDS_FSGID
|
407 SD_BUS_CREDS_SUPPLEMENTARY_GIDS
)) || r
>= 0))
410 if (c
->mask
& SD_BUS_CREDS_COMM
)
411 fprintf(f
, "%sComm=%s%s%s", prefix
, color
, c
->comm
, suffix
);
412 if (c
->mask
& SD_BUS_CREDS_TID_COMM
)
413 fprintf(f
, "%sTIDComm=%s%s%s", prefix
, color
, c
->tid_comm
, suffix
);
414 if (c
->mask
& SD_BUS_CREDS_EXE
)
415 fprintf(f
, "%sExe=%s%s%s", prefix
, color
, strna(c
->exe
), suffix
);
417 if (terse
&& (c
->mask
& (SD_BUS_CREDS_EXE
|SD_BUS_CREDS_COMM
|SD_BUS_CREDS_TID_COMM
)))
420 r
= sd_bus_creds_get_cmdline(c
, &cmdline
);
424 fprintf(f
, "%sCommandLine=%s", prefix
, color
);
425 STRV_FOREACH(i
, cmdline
) {
432 fprintf(f
, "%s", suffix
);
433 } else if (r
!= -ENODATA
)
434 fprintf(f
, "%sCommandLine=%sn/a%s", prefix
, color
, suffix
);
436 if (c
->mask
& SD_BUS_CREDS_SELINUX_CONTEXT
)
437 fprintf(f
, "%sLabel=%s%s%s", prefix
, color
, c
->label
, suffix
);
438 if (c
->mask
& SD_BUS_CREDS_DESCRIPTION
)
439 fprintf(f
, "%sDescription=%s%s%s", prefix
, color
, c
->description
, suffix
);
441 if (terse
&& (c
->mask
& (SD_BUS_CREDS_SELINUX_CONTEXT
|SD_BUS_CREDS_DESCRIPTION
)))
444 if (c
->mask
& SD_BUS_CREDS_CGROUP
)
445 fprintf(f
, "%sCGroup=%s%s%s", prefix
, color
, c
->cgroup
, suffix
);
447 r
= sd_bus_creds_get_unit(c
, &s
);
449 fprintf(f
, "%sUnit=%s%s%s", prefix
, color
, strna(s
), suffix
);
451 v
= sd_bus_creds_get_slice(c
, &s
);
453 fprintf(f
, "%sSlice=%s%s%s", prefix
, color
, strna(s
), suffix
);
455 q
= sd_bus_creds_get_user_unit(c
, &s
);
457 fprintf(f
, "%sUserUnit=%s%s%s", prefix
, color
, strna(s
), suffix
);
459 w
= sd_bus_creds_get_user_slice(c
, &s
);
461 fprintf(f
, "%sUserSlice=%s%s%s", prefix
, color
, strna(s
), suffix
);
463 z
= sd_bus_creds_get_session(c
, &s
);
465 fprintf(f
, "%sSession=%s%s%s", prefix
, color
, strna(s
), suffix
);
467 if (terse
&& ((c
->mask
& SD_BUS_CREDS_CGROUP
) || r
!= -ENODATA
|| q
!= -ENODATA
|| v
!= -ENODATA
|| w
!= -ENODATA
|| z
!= -ENODATA
))
470 r
= sd_bus_creds_get_audit_login_uid(c
, &audit_loginuid
);
472 fprintf(f
, "%sAuditLoginUID=%s"UID_FMT
"%s", prefix
, color
, audit_loginuid
, suffix
);
473 else if (r
!= -ENODATA
)
474 fprintf(f
, "%sAuditLoginUID=%sn/a%s", prefix
, color
, suffix
);
475 q
= sd_bus_creds_get_audit_session_id(c
, &audit_sessionid
);
477 fprintf(f
, "%sAuditSessionID=%s%"PRIu32
"%s", prefix
, color
, audit_sessionid
, suffix
);
478 else if (q
!= -ENODATA
)
479 fprintf(f
, "%sAuditSessionID=%sn/a%s", prefix
, color
, suffix
);
481 if (terse
&& (r
!= -ENODATA
|| q
!= -ENODATA
))
484 if (c
->mask
& SD_BUS_CREDS_UNIQUE_NAME
)
485 fprintf(f
, "%sUniqueName=%s%s%s", prefix
, color
, c
->unique_name
, suffix
);
487 if (sd_bus_creds_get_well_known_names(c
, &well_known
) >= 0) {
490 fprintf(f
, "%sWellKnownNames=%s", prefix
, color
);
491 STRV_FOREACH(i
, well_known
) {
498 fprintf(f
, "%s", suffix
);
501 if (terse
&& (c
->mask
& SD_BUS_CREDS_UNIQUE_NAME
|| well_known
))
504 dump_capabilities(c
, f
, "EffectiveCapabilities", terse
, sd_bus_creds_has_effective_cap
);
505 dump_capabilities(c
, f
, "PermittedCapabilities", terse
, sd_bus_creds_has_permitted_cap
);
506 dump_capabilities(c
, f
, "InheritableCapabilities", terse
, sd_bus_creds_has_inheritable_cap
);
507 dump_capabilities(c
, f
, "BoundingCapabilities", terse
, sd_bus_creds_has_bounding_cap
);
513 * For details about the file format, see:
515 * http://wiki.wireshark.org/Development/LibpcapFileFormat
518 typedef struct _packed_ pcap_hdr_s
{
519 uint32_t magic_number
; /* magic number */
520 uint16_t version_major
; /* major version number */
521 uint16_t version_minor
; /* minor version number */
522 int32_t thiszone
; /* GMT to local correction */
523 uint32_t sigfigs
; /* accuracy of timestamps */
524 uint32_t snaplen
; /* max length of captured packets, in octets */
525 uint32_t network
; /* data link type */
528 typedef struct _packed_ pcaprec_hdr_s
{
529 uint32_t ts_sec
; /* timestamp seconds */
530 uint32_t ts_usec
; /* timestamp microseconds */
531 uint32_t incl_len
; /* number of octets of packet saved in file */
532 uint32_t orig_len
; /* actual length of packet */
535 int bus_pcap_header(size_t snaplen
, FILE *f
) {
538 .magic_number
= 0xa1b2c3d4U
,
541 .thiszone
= 0, /* UTC */
543 .network
= 231, /* D-Bus */
550 assert((size_t) (uint32_t) snaplen
== snaplen
);
552 hdr
.snaplen
= (uint32_t) snaplen
;
554 fwrite(&hdr
, 1, sizeof(hdr
), f
);
556 return fflush_and_check(f
);
559 int bus_message_pcap_frame(sd_bus_message
*m
, size_t snaplen
, FILE *f
) {
560 struct bus_body_part
*part
;
561 pcaprec_hdr_t hdr
= {};
571 assert((size_t) (uint32_t) snaplen
== snaplen
);
573 if (m
->realtime
!= 0)
574 timeval_store(&tv
, m
->realtime
);
576 assert_se(gettimeofday(&tv
, NULL
) >= 0);
578 hdr
.ts_sec
= tv
.tv_sec
;
579 hdr
.ts_usec
= tv
.tv_usec
;
580 hdr
.orig_len
= BUS_MESSAGE_SIZE(m
);
581 hdr
.incl_len
= MIN(hdr
.orig_len
, snaplen
);
583 /* write the pcap header */
584 fwrite(&hdr
, 1, sizeof(hdr
), f
);
586 /* write the dbus header */
587 w
= MIN(BUS_MESSAGE_BODY_BEGIN(m
), snaplen
);
588 fwrite(m
->header
, 1, w
, f
);
591 /* write the dbus body */
592 MESSAGE_FOREACH_PART(part
, i
, m
) {
596 w
= MIN(part
->size
, snaplen
);
597 fwrite(part
->data
, 1, w
, f
);
601 return fflush_and_check(f
);