diff --git a/common/exit_codes.h b/common/exit_codes.h new file mode 100644 index 00000000..e8da6342 --- /dev/null +++ b/common/exit_codes.h @@ -0,0 +1,33 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Guillaume Meunier + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +namespace wivrn_exit_code +{ +enum +{ + success = EXIT_SUCCESS, + unknown_error = EXIT_FAILURE, + cannot_create_pipe, + cannot_create_socketpair, + cannot_connect_to_avahi, +}; +} diff --git a/server/avahi_publisher.cpp b/server/avahi_publisher.cpp index 0cc072ed..575840b2 100644 --- a/server/avahi_publisher.cpp +++ b/server/avahi_publisher.cpp @@ -9,21 +9,23 @@ #include #include -namespace +const std::error_category & avahi_error_category() { -struct : std::error_category -{ - const char * name() const noexcept override + static struct : std::error_category { - return "avahi"; - } + const char * name() const noexcept override + { + return "avahi"; + } - std::string message(int condition) const override - { - return avahi_strerror(condition); - } -} avahi_error_category; -} // namespace + std::string message(int condition) const override + { + return avahi_strerror(condition); + } + } _avahi_error_category; + + return _avahi_error_category; +} void avahi_publisher::alt_name() { @@ -40,7 +42,7 @@ void avahi_publisher::create_service(AvahiClient * client) if (!entry_group) { throw std::system_error(avahi_client_errno(client), - avahi_error_category, + avahi_error_category(), "Cannot create entry group, ensure disable-user-service-publishing is unset in avahi daemon config"); } } @@ -148,7 +150,7 @@ avahi_publisher::avahi_publisher(const AvahiPoll * poll_api, const std::string & &error); if (!avahi_client) - throw std::system_error(error, avahi_error_category, "Cannot create avahi client"); + throw std::system_error(error, avahi_error_category(), "Cannot create avahi client"); } avahi_publisher::~avahi_publisher() diff --git a/server/avahi_publisher.h b/server/avahi_publisher.h index a63d82d7..1fb5ca1b 100644 --- a/server/avahi_publisher.h +++ b/server/avahi_publisher.h @@ -4,8 +4,11 @@ #include #include #include +#include #include +const std::error_category & avahi_error_category(); + class avahi_publisher { AvahiEntryGroup * entry_group{}; diff --git a/server/main.cpp b/server/main.cpp index 473de9aa..b8027a2e 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -11,6 +11,7 @@ #include "active_runtime.h" #include "avahi_publisher.h" #include "driver/configuration.h" +#include "exit_codes.h" #include "hostname.h" #include "start_application.h" #include "version.h" @@ -616,7 +617,7 @@ int inner_main(int argc, char * argv[], bool show_instructions) if (pipe(stdin_pipe_fds) < 0) { perror("pipe"); - return EXIT_FAILURE; + return wivrn_exit_code::cannot_create_pipe; } fcntl(stdin_pipe_fds[0], F_SETFD, FD_CLOEXEC); fcntl(stdin_pipe_fds[1], F_SETFD, FD_CLOEXEC); @@ -625,7 +626,7 @@ int inner_main(int argc, char * argv[], bool show_instructions) if (socketpair(AF_UNIX, SOCK_DGRAM, 0, control_pipe_fds) < 0) { perror("socketpair"); - return EXIT_FAILURE; + return wivrn_exit_code::cannot_create_socketpair; } fcntl(control_pipe_fds[0], F_SETFD, FD_CLOEXEC); fcntl(control_pipe_fds[1], F_SETFD, FD_CLOEXEC); @@ -672,7 +673,7 @@ int inner_main(int argc, char * argv[], bool show_instructions) std::filesystem::remove(socket_path(), ec); #endif - return EXIT_SUCCESS; + return wivrn_exit_code::success; } int main(int argc, char * argv[]) @@ -701,9 +702,15 @@ int main(int argc, char * argv[]) { return inner_main(argc, argv, not *no_instructions); } + catch (std::system_error & e) + { + std::cerr << e.what() << std::endl; + if (e.code().category() == avahi_error_category()) + return wivrn_exit_code::cannot_connect_to_avahi; + } catch (std::exception & e) { std::cerr << e.what() << std::endl; - return EXIT_FAILURE; + return wivrn_exit_code::unknown_error; } }