LCOV - code coverage report
Current view: top level - basic - audit-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 31 38 81.6 %
Date: 2019-08-22 15:41:25 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <linux/netlink.h>
       5             : #include <stdio.h>
       6             : #include <sys/socket.h>
       7             : 
       8             : #include "alloc-util.h"
       9             : #include "audit-util.h"
      10             : #include "fd-util.h"
      11             : #include "fileio.h"
      12             : #include "macro.h"
      13             : #include "parse-util.h"
      14             : #include "process-util.h"
      15             : #include "user-util.h"
      16             : 
      17           2 : int audit_session_from_pid(pid_t pid, uint32_t *id) {
      18           2 :         _cleanup_free_ char *s = NULL;
      19             :         const char *p;
      20             :         uint32_t u;
      21             :         int r;
      22             : 
      23           2 :         assert(id);
      24             : 
      25             :         /* We don't convert ENOENT to ESRCH here, since we can't
      26             :          * really distinguish between "audit is not available in the
      27             :          * kernel" and "the process does not exist", both which will
      28             :          * result in ENOENT. */
      29             : 
      30           2 :         p = procfs_file_alloca(pid, "sessionid");
      31             : 
      32           2 :         r = read_one_line_file(p, &s);
      33           2 :         if (r < 0)
      34           0 :                 return r;
      35             : 
      36           2 :         r = safe_atou32(s, &u);
      37           2 :         if (r < 0)
      38           0 :                 return r;
      39             : 
      40           2 :         if (!audit_session_is_valid(u))
      41           1 :                 return -ENODATA;
      42             : 
      43           1 :         *id = u;
      44           1 :         return 0;
      45             : }
      46             : 
      47           2 : int audit_loginuid_from_pid(pid_t pid, uid_t *uid) {
      48           2 :         _cleanup_free_ char *s = NULL;
      49             :         const char *p;
      50             :         uid_t u;
      51             :         int r;
      52             : 
      53           2 :         assert(uid);
      54             : 
      55           2 :         p = procfs_file_alloca(pid, "loginuid");
      56             : 
      57           2 :         r = read_one_line_file(p, &s);
      58           2 :         if (r < 0)
      59           0 :                 return r;
      60             : 
      61           2 :         r = parse_uid(s, &u);
      62           2 :         if (r == -ENXIO) /* the UID was -1 */
      63           1 :                 return -ENODATA;
      64           1 :         if (r < 0)
      65           0 :                 return r;
      66             : 
      67           1 :         *uid = u;
      68           1 :         return 0;
      69             : }
      70             : 
      71           3 : bool use_audit(void) {
      72             :         static int cached_use = -1;
      73             : 
      74           3 :         if (cached_use < 0) {
      75             :                 int fd;
      76             : 
      77           1 :                 fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_AUDIT);
      78           1 :                 if (fd < 0) {
      79           0 :                         cached_use = !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT, EPERM);
      80           0 :                         if (!cached_use)
      81           0 :                                 log_debug_errno(errno, "Won't talk to audit: %m");
      82             :                 } else {
      83           1 :                         cached_use = true;
      84           1 :                         safe_close(fd);
      85             :                 }
      86             :         }
      87             : 
      88           3 :         return cached_use;
      89             : }

Generated by: LCOV version 1.14