Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 17 | #include <ctype.h> |
| 18 | |
Mark Salyzyn | b332f1c | 2015-08-19 13:53:20 -0700 | [diff] [blame^] | 19 | #include <string> |
| 20 | |
| 21 | #include <base/stringprintf.h> |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 22 | |
| 23 | #include "LogWhiteBlackList.h" |
| 24 | |
| 25 | // White and Black list |
| 26 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 27 | Prune::Prune(uid_t uid, pid_t pid) : mUid(uid), mPid(pid) { |
| 28 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 29 | |
| 30 | int Prune::cmp(uid_t uid, pid_t pid) const { |
| 31 | if ((mUid == uid_all) || (mUid == uid)) { |
| 32 | if (mPid == pid_all) { |
| 33 | return 0; |
| 34 | } |
| 35 | return pid - mPid; |
| 36 | } |
| 37 | return uid - mUid; |
| 38 | } |
| 39 | |
| 40 | void Prune::format(char **strp) { |
| 41 | if (mUid != uid_all) { |
Chih-Hung Hsieh | 634118e | 2014-09-12 16:02:28 -0700 | [diff] [blame] | 42 | if (mPid != pid_all) { |
| 43 | asprintf(strp, "%u/%u", mUid, mPid); |
| 44 | } else { |
| 45 | asprintf(strp, "%u", mUid); |
| 46 | } |
| 47 | } else if (mPid != pid_all) { |
| 48 | asprintf(strp, "/%u", mPid); |
| 49 | } else { // NB: mPid == pid_all can not happen if mUid == uid_all |
| 50 | asprintf(strp, "/"); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
Mark Salyzyn | 7718778 | 2015-05-12 15:21:31 -0700 | [diff] [blame] | 54 | PruneList::PruneList() : mWorstUidEnabled(true) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | PruneList::~PruneList() { |
| 58 | PruneCollection::iterator it; |
| 59 | for (it = mNice.begin(); it != mNice.end();) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 60 | it = mNice.erase(it); |
| 61 | } |
| 62 | for (it = mNaughty.begin(); it != mNaughty.end();) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 63 | it = mNaughty.erase(it); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | int PruneList::init(char *str) { |
Mark Salyzyn | 1a01f96 | 2014-05-06 13:49:28 -0700 | [diff] [blame] | 68 | mWorstUidEnabled = true; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 69 | PruneCollection::iterator it; |
| 70 | for (it = mNice.begin(); it != mNice.end();) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 71 | it = mNice.erase(it); |
| 72 | } |
| 73 | for (it = mNaughty.begin(); it != mNaughty.end();) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 74 | it = mNaughty.erase(it); |
| 75 | } |
| 76 | |
| 77 | if (!str) { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | mWorstUidEnabled = false; |
| 82 | |
| 83 | for(; *str; ++str) { |
| 84 | if (isspace(*str)) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | PruneCollection *list; |
| 89 | if ((*str == '~') || (*str == '!')) { // ~ supported, ! undocumented |
| 90 | ++str; |
| 91 | // special case, translates to worst UID at priority in blacklist |
| 92 | if (*str == '!') { |
| 93 | mWorstUidEnabled = true; |
| 94 | ++str; |
| 95 | if (!*str) { |
| 96 | break; |
| 97 | } |
| 98 | if (!isspace(*str)) { |
| 99 | return 1; |
| 100 | } |
| 101 | continue; |
| 102 | } |
| 103 | if (!*str) { |
| 104 | return 1; |
| 105 | } |
| 106 | list = &mNaughty; |
| 107 | } else { |
| 108 | list = &mNice; |
| 109 | } |
| 110 | |
| 111 | uid_t uid = Prune::uid_all; |
| 112 | if (isdigit(*str)) { |
| 113 | uid = 0; |
| 114 | do { |
| 115 | uid = uid * 10 + *str++ - '0'; |
| 116 | } while (isdigit(*str)); |
| 117 | } |
| 118 | |
| 119 | pid_t pid = Prune::pid_all; |
| 120 | if (*str == '/') { |
| 121 | ++str; |
| 122 | if (isdigit(*str)) { |
| 123 | pid = 0; |
| 124 | do { |
| 125 | pid = pid * 10 + *str++ - '0'; |
| 126 | } while (isdigit(*str)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if ((uid == Prune::uid_all) && (pid == Prune::pid_all)) { |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | if (*str && !isspace(*str)) { |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | // insert sequentially into list |
| 139 | PruneCollection::iterator it = list->begin(); |
| 140 | while (it != list->end()) { |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 141 | Prune &p = *it; |
| 142 | int m = uid - p.mUid; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 143 | if (m == 0) { |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 144 | if (p.mPid == p.pid_all) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 145 | break; |
| 146 | } |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 147 | if ((pid == p.pid_all) && (p.mPid != p.pid_all)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 148 | it = list->erase(it); |
| 149 | continue; |
| 150 | } |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 151 | m = pid - p.mPid; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 152 | } |
Mark Salyzyn | 22e287d | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 153 | if (m <= 0) { |
| 154 | if (m < 0) { |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 155 | list->insert(it, Prune(uid,pid)); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 156 | } |
| 157 | break; |
| 158 | } |
| 159 | ++it; |
| 160 | } |
| 161 | if (it == list->end()) { |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 162 | list->push_back(Prune(uid,pid)); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 163 | } |
| 164 | if (!*str) { |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | void PruneList::format(char **strp) { |
| 173 | if (*strp) { |
| 174 | free(*strp); |
| 175 | *strp = NULL; |
| 176 | } |
| 177 | |
| 178 | static const char nice_format[] = " %s"; |
| 179 | const char *fmt = nice_format + 1; |
| 180 | |
Mark Salyzyn | b332f1c | 2015-08-19 13:53:20 -0700 | [diff] [blame^] | 181 | std::string string; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 182 | |
| 183 | if (mWorstUidEnabled) { |
Mark Salyzyn | b332f1c | 2015-08-19 13:53:20 -0700 | [diff] [blame^] | 184 | string = "~!"; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 185 | fmt = nice_format; |
| 186 | } |
| 187 | |
| 188 | PruneCollection::iterator it; |
| 189 | |
| 190 | for (it = mNice.begin(); it != mNice.end(); ++it) { |
| 191 | char *a = NULL; |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 192 | (*it).format(&a); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 193 | |
Mark Salyzyn | b332f1c | 2015-08-19 13:53:20 -0700 | [diff] [blame^] | 194 | string += android::base::StringPrintf(fmt, a); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 195 | fmt = nice_format; |
| 196 | |
| 197 | free(a); |
| 198 | } |
| 199 | |
| 200 | static const char naughty_format[] = " ~%s"; |
| 201 | fmt = naughty_format + (*fmt != ' '); |
| 202 | for (it = mNaughty.begin(); it != mNaughty.end(); ++it) { |
| 203 | char *a = NULL; |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 204 | (*it).format(&a); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 205 | |
Mark Salyzyn | b332f1c | 2015-08-19 13:53:20 -0700 | [diff] [blame^] | 206 | string += android::base::StringPrintf(fmt, a); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 207 | fmt = naughty_format; |
| 208 | |
| 209 | free(a); |
| 210 | } |
| 211 | |
Mark Salyzyn | b332f1c | 2015-08-19 13:53:20 -0700 | [diff] [blame^] | 212 | *strp = strdup(string.c_str()); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // ToDo: Lists are in sorted order, Prune->cmp() returns + or - |
| 216 | // If there is scaling issues, resort to a better algorithm than linear |
| 217 | // based on these assumptions. |
| 218 | |
| 219 | bool PruneList::naughty(LogBufferElement *element) { |
| 220 | PruneCollection::iterator it; |
| 221 | for (it = mNaughty.begin(); it != mNaughty.end(); ++it) { |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 222 | if (!(*it).cmp(element)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | bool PruneList::nice(LogBufferElement *element) { |
| 230 | PruneCollection::iterator it; |
| 231 | for (it = mNice.begin(); it != mNice.end(); ++it) { |
Mark Salyzyn | e0ed16c | 2015-08-19 13:31:31 -0700 | [diff] [blame] | 232 | if (!(*it).cmp(element)) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | return false; |
| 237 | } |