1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
This file is part of Android File Transfer For Linux.
Copyright (C) 2015-2020 Vladimir Menshakov
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <mtp/usb/DeviceBusyException.h>
#include <mtp/log.h>
#ifdef __linux__
# include <fcntl.h>
# include <signal.h>
# include <unistd.h>
# include <linux/limits.h>
//mtp
# include <Exception.h>
# include <mtp/backend/linux/usb/Directory.h>
#endif
namespace mtp { namespace usb
{
#ifdef __linux__
namespace
{
std::string ReadLink(const std::string &path)
{
char buf[NAME_MAX];
auto pathSize = readlink(path.c_str(), buf, sizeof(buf));
if (pathSize >= 0)
return std::string(buf, pathSize);
debug("Readlink ", path, ": ", posix::Exception::GetErrorMessage(errno));
return buf;
}
}
#endif
DeviceBusyException::DeviceBusyException(int fd, const std::string &msg):
std::runtime_error(msg)
{
try
{
//only linux for now
#ifdef __linux__
auto myPid = getpid();
do
{
if (fd < 0)
break;
debug("trying to find processes which are holding fd ", fd);
#if 0 //apple
char filePath[PATH_MAX];
if (fcntl(fd, F_GETPATH, filePath) == -1)
{
error("error getting file path for fd ", fd, ": ", Exception::GetErrorMessage(errno));
break;
}
#else
std::string filePath = ReadLink("/proc/self/fd/" + std::to_string(fd));
if (filePath.empty())
break;
#endif
debug("mapped ", fd, " to ", filePath);
Directory proc("/proc");
while(true)
{
auto pidPath = proc.Read();
if (pidPath.empty())
break;
int pid;
if (sscanf(pidPath.c_str(), "%d", &pid) != 1)
continue;
if (pid == myPid)
continue;
// debug("got process dir ", pidPath);
auto fdsRoot = "/proc/" + pidPath + "/fd";
if (access(fdsRoot.c_str(), R_OK) != 0)
{
// debug("skipping inaccessible path ", fdsRoot);
continue;
}
try
{
Directory fds(fdsRoot);
while(true)
{
auto fdPath = fds.Read();
if (fdPath.empty())
break;
if (fdPath[0] == '.')
continue;
auto fdTarget = ReadLink(fdsRoot + "/" + fdPath);
if (fdTarget.empty())
continue;
//debug("read mapping to ", fdTarget);
if (fdTarget == filePath)
{
debug("process ", pid, " is holding file descriptor to ", filePath);
auto image = ReadLink("/proc/" + pidPath + "/exe");
Processes.push_back({ pid, image });
}
}
}
catch(const std::exception & ex)
{ debug("error reading ", fdsRoot, ": ", ex.what()); }
}
}
while(false);
#endif
} catch(const std::exception &ex)
{ debug("DeviceBusyException error: ", ex.what()); }
}
void DeviceBusyException::Kill() const
{ Kill(Processes); }
void DeviceBusyException::Kill(const std::vector<ProcessDescriptor> & processes)
{
for(auto & desc : processes)
{
try
{ Kill(desc); }
catch(const std::exception & ex)
{ error("Kill: ", ex.what()); }
}
}
void DeviceBusyException::Kill(const ProcessDescriptor & desc)
{
#ifdef __linux__
if (kill(desc.Id, SIGTERM) != 0)
throw posix::Exception("kill(" + std::to_string(desc.Id) + " (" + desc.Name + "), SIGTERM)");
sleep(1);
kill(desc.Id, SIGKILL); //assuming we can do it
#else
throw std::runtime_error("not implemented");
#endif
}
}}
|