Skip to content

Commit

Permalink
Добавлена обработка исключений
Browse files Browse the repository at this point in the history
  • Loading branch information
khromenokroman committed Aug 13, 2024
1 parent c2e9588 commit d930c7e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
38 changes: 35 additions & 3 deletions src/client/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
#include "client.hpp"

int main([[maybe_unused]]int arc, [[maybe_unused]]char **argv) {
radio::client::Client client("127.0.0.1", 1993);
client.send_message("ls -l");
/**
* @brief Helper function to check if the program is running as root.
*
* This function checks if the current user ID is 0, which corresponds to the root user.
* If the program is running as root, an exception is thrown..
* It is not generally recommended to run non-system-essential programs as root.
* Doing so can lead to accidental system-wide changes.
*/
void check_if_not_root();


int main(int argc, char **argv) {

if (argc < 4) {
::fmt::print(stderr, "Can't found ip server/port or file for list radio\n"
"Usage: {} <ip server> <port_number> <file list radio>\n", argv[0]);
return 1;
}

std::string ip_server = argv[1];
try {
check_if_not_root();

radio::client::Client client(argv[1], std::stoi(argv[2]));
client.send_message("ls -l");
} catch (std::exception const &ex) {
::fmt::print(stderr, "Exception occurred: {}\n", ex.what());
return -1;
}
return 0;
}

void check_if_not_root() {
if (getuid() == 0) {
throw std::runtime_error("The program should not be run as root");
}
}
13 changes: 9 additions & 4 deletions src/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ int main(int argc, char **argv) {
::fmt::print(stderr, "Can't found port server\nUsage: {} <port_number>\n", argv[0]);
return 1;
}
int port = std::stoi(argv[1]);

check_if_not_root();
try {
check_if_not_root();

int port = std::stoi(argv[1]);
radio::server::Server server(port);
server.run();
radio::server::Server server(port);
server.run();
}catch (std::exception const& ex){
::fmt::print(stderr, "Exception occurred: {}\n", ex.what());
return -1;
}
return 0;
}

Expand Down

0 comments on commit d930c7e

Please sign in to comment.