1 /*
2 * Copyright (C) 2023 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 "KernelConfigs.h"
18
19 #include <android-base/logging.h>
20
21 #include <map>
22 #include <string>
23
24 #include <zlib.h>
25 #include "vintf/KernelConfigParser.h"
26
27 #define BUFFER_SIZE sysconf(_SC_PAGESIZE)
28
29 namespace android {
30 namespace kernelconfigs {
31
LoadKernelConfigs(std::map<std::string,std::string> * configs)32 status_t LoadKernelConfigs(std::map<std::string, std::string>* configs) {
33 vintf::KernelConfigParser parser;
34 gzFile f = gzopen("/proc/config.gz", "rb");
35 if (f == NULL) {
36 LOG(ERROR) << "Could not open /proc/config.gz: " << errno;
37 return -errno;
38 }
39
40 char buf[BUFFER_SIZE];
41 int len;
42 while ((len = gzread(f, buf, sizeof buf)) > 0) {
43 parser.process(buf, len);
44 }
45 status_t err = OK;
46 if (len < 0) {
47 int errnum;
48 const char* errmsg = gzerror(f, &errnum);
49 LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg;
50 err = (errnum == Z_ERRNO ? -errno : errnum);
51 }
52 parser.finish();
53 gzclose(f);
54 *configs = std::move(parser.configs());
55 return err;
56 }
57
58 } // namespace kernelconfigs
59 } // namespace android
60