2 * Temporary debug code to find processes locking internal cryptsetup devices.
3 * This code is intended to run only in debug mode.
5 * inspired by psmisc/fuser proc scanning code
7 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
33 #include "libcryptsetup.h"
36 #define MAX_PATHNAME 1024
37 #define MAX_SHORTNAME 64
39 static int numeric_name(const char *name)
41 return (name[0] < '0' || name[0] > '9') ? 0 : 1;
44 static int check_pid(const pid_t pid, const char *dev_name, const char *short_dev_name)
46 char dirpath[MAX_SHORTNAME], fdpath[MAX_SHORTNAME], linkpath[MAX_PATHNAME];
48 struct dirent *direntry;
52 snprintf(dirpath, sizeof(dirpath), "/proc/%d/fd", pid);
54 if (!(dirp = opendir(dirpath)))
57 while ((direntry = readdir(dirp))) {
58 if (!numeric_name(direntry->d_name))
61 snprintf(fdpath, sizeof(fdpath), "/proc/%d/fd/%s", pid, direntry->d_name);
63 if ((len = readlink(fdpath, linkpath, MAX_PATHNAME-1)) < 0)
67 if (!strcmp(dev_name, linkpath)) {
72 if (!strcmp(short_dev_name, linkpath)) {
81 static int read_proc_info(const pid_t pid, pid_t *ppid, char *name, int max_size)
83 char path[MAX_SHORTNAME], info[max_size], c;
86 snprintf(path, sizeof(path), "/proc/%u/stat", pid);
87 if ((fd = open(path, O_RDONLY)) < 0)
90 if (read(fd, info, max_size) > 0 &&
91 sscanf(info, "%d %s %c %d", &xpid, name, &c, ppid) == 4)
102 static void report_proc(const pid_t pid, const char *dev_name)
104 char name[MAX_PATHNAME], name2[MAX_PATHNAME];
107 if (read_proc_info(pid, &ppid, name, MAX_PATHNAME) &&
108 read_proc_info(ppid, &ppid2, name2, MAX_PATHNAME))
109 log_dbg("WARNING: Process PID %u %s [PPID %u %s] spying on internal device %s.",
110 pid, name, ppid, name2, dev_name);
113 void debug_processes_using_device(const char *dm_name)
115 char short_dev_name[MAX_SHORTNAME], dev_name[MAX_PATHNAME];
117 struct dirent *proc_dentry;
121 if (crypt_get_debug_level() != CRYPT_LOG_DEBUG)
124 snprintf(dev_name, sizeof(dev_name), "/dev/mapper/%s", dm_name);
125 if (stat(dev_name, &st) || !S_ISBLK(st.st_mode))
127 snprintf(short_dev_name, sizeof(short_dev_name), "/dev/dm-%u", minor(st.st_rdev));
129 if (!(proc_dir = opendir("/proc")))
132 while ((proc_dentry = readdir(proc_dir))) {
133 if (!numeric_name(proc_dentry->d_name))
136 pid = atoi(proc_dentry->d_name);
137 switch(check_pid(pid, dev_name, short_dev_name)) {
138 case 1: report_proc(pid, dev_name);
140 case 2: report_proc(pid, short_dev_name);