History

pidfds: add coredump_code field to pidfd_info

The struct pidfd_info currently exposes in a field called coredump_signal the signal number (si_signo) that triggered the dump (for example, 11 for SIGSEGV). However, it is also valuable to understand the reason why that signal was sent. This additional context is provided by the signal code (si_code), such as 2 for SEGV_ACCERR. Add a new field to struct pidfd_info called coredump_code with the value of si_code for the benefit of sysadmins who pipe core dumps to user-space programs for later analysis. The following snippet illustrates a simplified C program that consumes coredump_signal and coredump_code, and then logs core dump signals and codes to a file: int pidfd = (int)atoi(argv[1]); struct pidfd_info info = { .mask = PIDFD_INFO_EXIT | PIDFD_INFO_COREDUMP, }; if (ioctl(pidfd, PIDFD_GET_INFO, &info) == 0) if (info.mask & PIDFD_INFO_COREDUMP) fprintf(f, "PID=%d, si_signo: %d si_code: %d\n", info.pid, info.coredump_signal, info.coredump_code); Assuming the program is installed under /usr/local/bin/core-logger, core dump processing can be enabled by setting /proc/sys/kernel/core_pattern to '|/usr/local/bin/dumpstuff %F'. systemd-coredump(8) already uses pidfds to process core dumps, and it could be extended to include the values of coredump_code too.

Emanuele Rocca committed 7 months ago

Commit 701f7f41 parent 3fc66a12 files · +12 −4Browse files

Unified · Split

2 files
  1. fs/pidfs.c +8 −4
  2. include/uapi/linux/pidfd.h +4 −0
fs/pidfs.c+8−4
@@ -57,6 +57,7 @@ struct pidfs_attr { · 56 unmodified lines
57 };57 };
58 __u32 coredump_mask;58 __u32 coredump_mask;
59 __u32 coredump_signal;59 __u32 coredump_signal;
60 __u32 coredump_code;
60};61};
61 62
62static struct rhashtable pidfs_ino_ht;63static struct rhashtable pidfs_ino_ht;
@@ -333,7 +334,8 @@ static __u32 pidfs_coredump_mask(unsigned long mm_flags) · 270 unmodified lines
333 PIDFD_INFO_EXIT | \334 PIDFD_INFO_EXIT | \
334 PIDFD_INFO_COREDUMP | \335 PIDFD_INFO_COREDUMP | \
335 PIDFD_INFO_SUPPORTED_MASK | \336 PIDFD_INFO_SUPPORTED_MASK | \
336 PIDFD_INFO_COREDUMP_SIGNAL)337 PIDFD_INFO_COREDUMP_SIGNAL | \
338 PIDFD_INFO_COREDUMP_CODE)
337 339
338static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)340static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg)
339{341{
@@ -347,7 +349,7 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg) · 7 unmodified lines
347 const struct cred *c;349 const struct cred *c;
348 __u64 mask;350 __u64 mask;
349 351
350 BUILD_BUG_ON(sizeof(struct pidfd_info) != PIDFD_INFO_SIZE_VER2);352 BUILD_BUG_ON(sizeof(struct pidfd_info) != PIDFD_INFO_SIZE_VER3);
351 353
352 if (!uinfo)354 if (!uinfo)
353 return -EINVAL;355 return -EINVAL;
@@ -380,9 +382,10 @@ static long pidfd_info(struct file *file, unsigned int cmd, unsigned long arg) · 26 unmodified lines
380 if (mask & PIDFD_INFO_COREDUMP) {382 if (mask & PIDFD_INFO_COREDUMP) {
381 if (test_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask)) {383 if (test_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask)) {
382 smp_rmb();384 smp_rmb();
383 kinfo.mask |= PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL;385 kinfo.mask |= PIDFD_INFO_COREDUMP | PIDFD_INFO_COREDUMP_SIGNAL | PIDFD_INFO_COREDUMP_CODE;
384 kinfo.coredump_mask = attr->coredump_mask;386 kinfo.coredump_mask = attr->coredump_mask;
385 kinfo.coredump_signal = attr->coredump_signal;387 kinfo.coredump_signal = attr->coredump_signal;
388 kinfo.coredump_code = attr->coredump_code;
386 }389 }
387 }390 }
388 391
@@ -755,8 +758,9 @@ void pidfs_coredump(const struct coredump_params *cprm) · 366 unmodified lines
755 PIDFD_COREDUMPED;758 PIDFD_COREDUMPED;
756 /* If coredumping is set to skip we should never end up here. */759 /* If coredumping is set to skip we should never end up here. */
757 VFS_WARN_ON_ONCE(attr->coredump_mask & PIDFD_COREDUMP_SKIP);760 VFS_WARN_ON_ONCE(attr->coredump_mask & PIDFD_COREDUMP_SKIP);
758 /* Expose the signal number that caused the coredump. */761 /* Expose the signal number and code that caused the coredump. */
759 attr->coredump_signal = cprm->siginfo->si_signo;762 attr->coredump_signal = cprm->siginfo->si_signo;
763 attr->coredump_code = cprm->siginfo->si_code;
760 smp_wmb();764 smp_wmb();
761 set_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);765 set_bit(PIDFS_ATTR_BIT_COREDUMP, &attr->attr_mask);
762}766}
include/uapi/linux/pidfd.h+4−0
@@ -29,10 +29,12 @@ · 28 unmodified lines
29#define PIDFD_INFO_COREDUMP (1UL << 4) /* Only returned if requested. */29#define PIDFD_INFO_COREDUMP (1UL << 4) /* Only returned if requested. */
30#define PIDFD_INFO_SUPPORTED_MASK (1UL << 5) /* Want/got supported mask flags */30#define PIDFD_INFO_SUPPORTED_MASK (1UL << 5) /* Want/got supported mask flags */
31#define PIDFD_INFO_COREDUMP_SIGNAL (1UL << 6) /* Always returned if PIDFD_INFO_COREDUMP is requested. */31#define PIDFD_INFO_COREDUMP_SIGNAL (1UL << 6) /* Always returned if PIDFD_INFO_COREDUMP is requested. */
32#define PIDFD_INFO_COREDUMP_CODE (1UL << 7) /* Always returned if PIDFD_INFO_COREDUMP is requested. */
32 33
33#define PIDFD_INFO_SIZE_VER0 64 /* sizeof first published struct */34#define PIDFD_INFO_SIZE_VER0 64 /* sizeof first published struct */
34#define PIDFD_INFO_SIZE_VER1 72 /* sizeof second published struct */35#define PIDFD_INFO_SIZE_VER1 72 /* sizeof second published struct */
35#define PIDFD_INFO_SIZE_VER2 80 /* sizeof third published struct */36#define PIDFD_INFO_SIZE_VER2 80 /* sizeof third published struct */
37#define PIDFD_INFO_SIZE_VER3 88 /* sizeof fourth published struct */
36 38
37/*39/*
38 * Values for @coredump_mask in pidfd_info.40 * Values for @coredump_mask in pidfd_info.
@@ -99,6 +101,8 @@ struct pidfd_info { · 60 unmodified lines
99 struct /* coredump info */ {101 struct /* coredump info */ {
100 __u32 coredump_mask;102 __u32 coredump_mask;
101 __u32 coredump_signal;103 __u32 coredump_signal;
104 __u32 coredump_code;
105 __u32 coredump_pad; /* align supported_mask to 8 bytes */
102 };106 };
103 __u64 supported_mask; /* Mask flags that this kernel supports */107 __u64 supported_mask; /* Mask flags that this kernel supports */
104};108};

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t