Skip to content

Commit

Permalink
apply_scgi: try to avoid removing socket opened by another instance
Browse files Browse the repository at this point in the history
This change prevents a rTorrent instance that may be erroneously
launched from removing the socket of the existing rTorrent instance.
  • Loading branch information
jesec committed May 1, 2021
1 parent 45d6e3c commit 1031770
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/command_network.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (C) 2005-2011, Jari Sundell <[email protected]>

#include <cstdio>
#include <filesystem>
#include <fstream>
#include <functional>
#include <limits>
#include <torrent/common.h>
Expand Down Expand Up @@ -194,8 +196,25 @@ apply_scgi(const std::string& arg, int type) {
default:
path = torrent::utils::path_expand(arg);

// Try to avoid removing socket opened by another instance
if (std::filesystem::is_socket(path) &&
std::filesystem::exists("/proc/net/unix")) {
try {
auto list = std::ifstream("/proc/net/unix");
std::string entry;
while (std::getline(list, entry)) {
if (entry.find(path) != std::string::npos) {
throw torrent::input_error("Socket already in use.");
}
}
} catch (std::ios_base::failure&) {
// do nothing.
}
}

unlink(path.c_str());
scgi->open_named(path);

break;
}

Expand Down

0 comments on commit 1031770

Please sign in to comment.