blob: 9865d4304f1beafa4675a25aabd4b23d2f962d90 [file] [log] [blame]
William Roberts29d238d2013-02-08 09:45:26 +09001/*
2 * Copyright 2012, Samsung Telecommunications of America
3 * Copyright (C) 2014 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Written by William Roberts <w.roberts@sta.samsung.com>
18 */
19
20#ifndef _LIBAUDIT_H_
21#define _LIBAUDIT_H_
22
23#include <stdint.h>
24#include <sys/cdefs.h>
25#include <sys/socket.h>
26#include <sys/types.h>
27
28#include <linux/netlink.h>
29#include <linux/audit.h>
30
31__BEGIN_DECLS
32
33#define MAX_AUDIT_MESSAGE_LENGTH 8970
34
35typedef enum {
Mark Salyzyn247d6822017-01-03 14:00:19 -080036 GET_REPLY_BLOCKING = 0,
William Roberts29d238d2013-02-08 09:45:26 +090037 GET_REPLY_NONBLOCKING
38} reply_t;
39
William Roberts29d238d2013-02-08 09:45:26 +090040/* type == AUDIT_SIGNAL_INFO */
41struct audit_sig_info {
42 uid_t uid;
43 pid_t pid;
44 char ctx[0];
45};
46
47struct audit_message {
48 struct nlmsghdr nlh;
49 char data[MAX_AUDIT_MESSAGE_LENGTH];
50};
51
52/**
53 * Opens a connection to the Audit netlink socket
54 * @return
55 * A valid fd on success or < 0 on error with errno set.
56 * Returns the same errors as man 2 socket.
57 */
Mark Salyzyn247d6822017-01-03 14:00:19 -080058extern int audit_open(void);
William Roberts29d238d2013-02-08 09:45:26 +090059
60/**
61 * Closes the fd returned from audit_open()
62 * @param fd
63 * The fd to close
64 */
65extern void audit_close(int fd);
66
67/**
68 *
69 * @param fd
70 * The fd returned by a call to audit_open()
71 * @param rep
72 * The response struct to store the response in.
73 * @param block
74 * Whether or not to block on IO
75 * @param peek
76 * Whether or not we are to remove the message from
77 * the queue when we do a read on the netlink socket.
78 * @return
79 * This function returns 0 on success, else -errno.
80 */
Mark Salyzyn247d6822017-01-03 14:00:19 -080081extern int audit_get_reply(int fd, struct audit_message *rep, reply_t block,
82 int peek);
William Roberts29d238d2013-02-08 09:45:26 +090083
84/**
Mark Salyzyn247d6822017-01-03 14:00:19 -080085 * Sets a pid to receive audit netlink events from the kernel
William Roberts29d238d2013-02-08 09:45:26 +090086 * @param fd
87 * The fd returned by a call to audit_open()
88 * @param pid
Mark Salyzyn247d6822017-01-03 14:00:19 -080089 * The pid whom to set as the receiver of audit messages
William Roberts29d238d2013-02-08 09:45:26 +090090 * @return
91 * This function returns 0 on success, -errno on error.
92 */
Mark Salyzyn247d6822017-01-03 14:00:19 -080093extern int audit_setup(int fd, pid_t pid);
94
95/**
96 * Sets the rate limit to receive audit netlink events from the kernel
97 * @param fd
98 * The fd returned by a call to audit_open()
99 * @param max_rate
100 * The cap of the maximum number of audit messages a second
101 * @return
102 * This function returns 0 on success, -errno on error.
103 */
104
105/* Guidelines to follow for dynamic rate_limit */
106#define AUDIT_RATE_LIMIT_DEFAULT 20 /* acceptable burst rate */
107#define AUDIT_RATE_LIMIT_BURST_DURATION 10 /* number of seconds of burst */
108#define AUDIT_RATE_LIMIT_MAX 5 /* acceptable sustained rate */
109
110extern int audit_rate_limit(int fd, unsigned rate_limit);
William Roberts29d238d2013-02-08 09:45:26 +0900111
112__END_DECLS
113
114#endif