zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit 5e7d127f82af67154b388b2c9ff35a93d4442cac
parent d32f875e3b778a67d96c666da16c97a4db29cb88
Author: Johannes Lorenz <johannes89@mailueberfall.de>
Date:   Tue,  9 Dec 2014 11:20:16 +0100

Removing dead tmp files working now.

Diffstat:
Msrc/Misc/MiddleWare.cpp | 122++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 106 insertions(+), 16 deletions(-)

diff --git a/src/Misc/MiddleWare.cpp b/src/Misc/MiddleWare.cpp @@ -3,12 +3,14 @@ #include <cstring> #include <cstdio> #include <cstdlib> +#include <fstream> #include <rtosc/thread-link.h> #include <rtosc/ports.h> #include <lo/lo.h> #include <unistd.h> +#include <dirent.h> #include "../UI/Fl_Osc_Interface.h" #include "../UI/Fl_Osc_Widget.H" @@ -424,10 +426,111 @@ static Fl_Osc_Interface *genOscInterface(class MiddleWareImpl*); /* Implementation */ class MiddleWareImpl { + static constexpr const char* tmp_nam_prefix = "/tmp/zynaddsubfx_"; + //! returns file name to where UDP port is saved std::string get_tmp_nam() const { - return "/tmp/zynaddsubfx_" + to_s(getpid()); + return tmp_nam_prefix + to_s(getpid()); + } + + void create_tmp_file(unsigned server_port) + { + std::string tmp_nam = get_tmp_nam(); + if(0 == access(tmp_nam.c_str(), F_OK)) { + fprintf(stderr, "Error: Cannot overwrite file %s. " + "You should probably remove it.", tmp_nam.c_str()); + exit(EXIT_FAILURE); + } + FILE* tmp_fp = fopen(tmp_nam.c_str(), "w"); + if(!tmp_fp) + fprintf(stderr, "Warning: could not create new file %s.\n", + tmp_nam.c_str()); + else + fprintf(tmp_fp, "%u", server_port); + fclose(tmp_fp); + } + + void clean_up_tmp_nams() const + { + DIR *dir; + struct dirent *entry; + if ((dir = opendir ("/tmp/")) != nullptr) + { + while ((entry = readdir (dir)) != nullptr) + { + std::string name = std::string("/tmp/") + entry->d_name; + if(!name.compare(0, strlen(tmp_nam_prefix),tmp_nam_prefix)) + { + std::string pid = name.substr(strlen(tmp_nam_prefix)); + std::string proc_file = "/proc/" + std::move(pid) + + "/comm"; + + std::ifstream ifs(proc_file); + bool remove = false; + + if(!ifs.good()) + { + fprintf(stderr, "Note: trying to remove %s - the " + "process does not exist anymore.\n", + name.c_str()); + remove = true; + } + else + { + std::string comm_name; + ifs >> comm_name; + if(comm_name == "zynaddsubfx") + fprintf(stderr, "Note: detected running " + "zynaddsubfx with PID %s.\n", + name.c_str() + strlen(tmp_nam_prefix)); + else { + fprintf(stderr, "Note: trying to remove %s - the " + "PID is owned by\n another " + "process: %s\n", + name.c_str(), comm_name.c_str()); + remove = true; + } + } + + + if(remove) + { + // make sure this file contains only one unsigned + unsigned udp_port; + std::ifstream ifs2(name); + if(!ifs2.good()) + fprintf(stderr, "Warning: could not open %s.\n", + name.c_str()); + else + { + ifs2 >> udp_port; + if(ifs.good()) + fprintf(stderr, "Warning: could not remove " + "%s, \n it has not been " + "written by zynaddsubfx\n", + name.c_str()); + else + { + if(std::remove(name.c_str()) != 0) + fprintf(stderr, "Warning: can not remove " + "%s.\n", name.c_str()); + } + } + } + + /* one might want to connect to zyn here, + but it is not necessary: + lo_address la = lo_address_new(nullptr, udp_port.c_str()); + if(lo_send(la, "/echo", nullptr) <= 0) + fputs("Note: found crashed file %s\n", stderr); + lo_address_free(la);*/ + } + } + closedir (dir); + } else { + fputs("Warning: can not read /tmp.\n", stderr); + } } public: @@ -596,21 +699,8 @@ MiddleWareImpl::MiddleWareImpl(void) lo_server_add_method(server, NULL, NULL, handler_function, NULL); fprintf(stderr, "lo server running on %d\n", lo_server_get_port(server)); - { - std::string tmp_nam = get_tmp_nam(); - if(0 == access(tmp_nam.c_str(), F_OK)) { - fprintf(stderr, "Error: Cannot overwrite file %s. " - "You should probably remove it.", tmp_nam.c_str()); - exit(EXIT_FAILURE); - } - FILE* tmp_fp = fopen(tmp_nam.c_str(), "w"); - if(!tmp_fp) - fprintf(stderr, "Warning: could not create new file %s.\n", - tmp_nam.c_str()); - else - fprintf(tmp_fp, "%u", (unsigned)lo_server_get_port(server)); - fclose(tmp_fp); - } + clean_up_tmp_nams(); + create_tmp_file((unsigned)lo_server_get_port(server)); //dummy callback for starters cb = [](void*, const char*){};