2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/types.h>
26 #include <pagemap/pagemap.h>
28 int pm_kernel_create(pm_kernel_t **ker_out) {
35 ker = calloc(1, sizeof(*ker));
39 ker->kpagecount_fd = open("/proc/kpagecount", O_RDONLY);
40 if (ker->kpagecount_fd < 0) {
46 ker->kpageflags_fd = open("/proc/kpageflags", O_RDONLY);
47 if (ker->kpageflags_fd < 0) {
49 close(ker->kpagecount_fd);
54 ker->pagesize = getpagesize();
62 int pm_kernel_pids(pm_kernel_t *ker, pid_t **pids_out, size_t *len) {
65 pid_t pid, *pids, *new_pids;
66 size_t pids_count, pids_size;
69 proc = opendir("/proc");
73 pids = malloc(INIT_PIDS * sizeof(pid_t));
78 pids_count = 0; pids_size = INIT_PIDS;
80 while ((dir = readdir(proc))) {
81 if (sscanf(dir->d_name, "%d", &pid) < 1)
84 if (pids_count >= pids_size) {
85 new_pids = realloc(pids, 2 * pids_size * sizeof(pid_t));
93 pids_size = 2 * pids_size;
96 pids[pids_count] = pid;
103 new_pids = realloc(pids, pids_count * sizeof(pid_t));
110 *pids_out = new_pids;
116 int pm_kernel_count(pm_kernel_t *ker, uint64_t pfn, uint64_t *count_out) {
119 if (!ker || !count_out)
122 off = lseek64(ker->kpagecount_fd, pfn * sizeof(uint64_t), SEEK_SET);
123 if (off == (off_t)-1)
125 if (read(ker->kpagecount_fd, count_out, sizeof(uint64_t)) <
126 (ssize_t)sizeof(uint64_t))
132 int pm_kernel_flags(pm_kernel_t *ker, uint64_t pfn, uint64_t *flags_out) {
135 if (!ker || !flags_out)
138 off = lseek64(ker->kpageflags_fd, pfn * sizeof(uint64_t), SEEK_SET);
139 if (off == (off_t)-1)
141 if (read(ker->kpageflags_fd, flags_out, sizeof(uint64_t)) <
142 (ssize_t)sizeof(uint64_t))
148 int pm_kernel_destroy(pm_kernel_t *ker) {
152 close(ker->kpagecount_fd);
153 close(ker->kpageflags_fd);