| Nick Kralevich | be5e446 | 2019-04-09 10:59:39 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2019 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #include <android-base/parseint.h> | 
 | 18 | #include <error.h> | 
 | 19 | #include <stdio.h> | 
 | 20 | #include <stdlib.h> | 
 | 21 | #include <unistd.h> | 
 | 22 | #include "libaudit.h" | 
 | 23 |  | 
 | 24 | static void usage(const char* cmdline) { | 
 | 25 |     fprintf(stderr, "Usage: %s [-r rate]\n", cmdline); | 
 | 26 | } | 
 | 27 |  | 
 | 28 | static void do_update_rate(uint32_t rate) { | 
 | 29 |     int fd = audit_open(); | 
 | 30 |     if (fd == -1) { | 
 | 31 |         error(EXIT_FAILURE, errno, "Unable to open audit socket"); | 
 | 32 |     } | 
 | 33 |     int result = audit_rate_limit(fd, rate); | 
 | 34 |     close(fd); | 
 | 35 |     if (result < 0) { | 
 | 36 |         fprintf(stderr, "Can't update audit rate limit: %d\n", result); | 
 | 37 |         exit(EXIT_FAILURE); | 
 | 38 |     } | 
 | 39 | } | 
 | 40 |  | 
 | 41 | int main(int argc, char* argv[]) { | 
 | 42 |     uint32_t rate = 0; | 
 | 43 |     bool update_rate = false; | 
 | 44 |     int opt; | 
 | 45 |  | 
 | 46 |     while ((opt = getopt(argc, argv, "r:")) != -1) { | 
 | 47 |         switch (opt) { | 
 | 48 |             case 'r': | 
 | 49 |                 if (!android::base::ParseUint<uint32_t>(optarg, &rate)) { | 
 | 50 |                     error(EXIT_FAILURE, errno, "Invalid Rate"); | 
 | 51 |                 } | 
 | 52 |                 update_rate = true; | 
 | 53 |                 break; | 
 | 54 |             default: /* '?' */ | 
 | 55 |                 usage(argv[0]); | 
 | 56 |                 exit(EXIT_FAILURE); | 
 | 57 |         } | 
 | 58 |     } | 
 | 59 |  | 
 | 60 |     // In the future, we may add other options to auditctl | 
 | 61 |     // so this if statement will expand. | 
 | 62 |     // if (!update_rate && !update_backlog && !update_whatever) ... | 
 | 63 |     if (!update_rate) { | 
 | 64 |         fprintf(stderr, "Nothing to do\n"); | 
 | 65 |         usage(argv[0]); | 
 | 66 |         exit(EXIT_FAILURE); | 
 | 67 |     } | 
 | 68 |  | 
 | 69 |     if (update_rate) { | 
 | 70 |         do_update_rate(rate); | 
 | 71 |     } | 
 | 72 |  | 
 | 73 |     return 0; | 
 | 74 | } |