Unix Socket Operation not Permit (Solved)
-
Bekomme wenn ich ich meine Unixsockets Klasse anspreche kommt Initialisierung immer ein:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permittedauch wenn ich das Programm als root ausführe.
#include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <thread> struct lSocket { lSocket(const char *socketpath) : socketpath_(socketpath) { if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } lsockaddr.sun_family=AF_UNIX; strcpy(lsockaddr.sun_path, socketpath); unlink(lsockaddr.sun_path); len = strlen(lsockaddr.sun_path) + sizeof(lsockaddr.sun_family); if (bind(s, (struct sockaddr *)&lsockaddr, len) == -1) { perror("bind"); exit(1); } if (listen(s, 5) == -1) { perror("listen"); exit(1); } }const char *socketpath_; void runDaemon(){ while(1){ rs=sizeof(rsockaddr); if ((sc = accept(s, (struct sockaddr *)&rsockaddr, (socklen_t*)&rs)) == -1) { perror("accept"); exit(1); } } } const char *getData(){ int r = recv(sc, data, 100, 0); if(r < 0){ return 0; } return data; } void sendData(const char *data){ send(sc,data, strlen(data), 0); } private: char data[255]; int s,sc,rs,len; sockaddr_un lsockaddr,rsockaddr; }; class UnixSocket { public: UnixSocket(const char *path){ local = new lSocket(path); std::thread([local] () { local->runDaemon(); }); } ~UnixSocket(){ delete local; } const char *getData(){ return local->getData(); } void sendData(const char *data){ local->sendData(data); } private: lSocket *local; };
conf.cpp
Conf::Conf() : UnixSocket("/tmp/conf.socket"){ while(1){ for(const char *command=getData(); command!=0;){ std::cout << command << std::endl; } } } int main(){ Conf *conf = new Conf(); }
conf.h
class Conf : public UnixSocket{ public: Conf(); };
-
Fehlermeldung gdb:
0x00007ffff71173a5 in __GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 64 ../nptl/sysdeps/unix/sysv/linux/raise.c: Datei oder Verzeichnis nicht gefunden. in ../nptl/sysdeps/unix/sysv/linux/raise.c (gdb) bt #0 0x00007ffff71173a5 in __GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 #1 0x00007ffff711ab0b in __GI_abort () at abort.c:92 #2 0x00007ffff7751d7d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3 0x00007ffff774ff26 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #4 0x00007ffff774ff53 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x00007ffff775004e in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff76fca3e in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #7 0x00007ffff7706cb7 in std::thread::_M_start_thread(std::shared_ptr<std::thread::_Impl_base>) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #8 0x0000000000407234 in std::thread::thread<UnixSocket::UnixSocket(char const*)::{lambda()#1}>(UnixSocket::UnixSocket(char const*)::{lambda()#1}&&) () #9 0x00000000004070f2 in UnixSocket::UnixSocket(char const*) ()
-
Kannst du das Beispiel mal vervollständigen, so dass man den Fehler selber nachvollziehen kann?
-
Ruf den GCC mal mit -pthread auf.
Sonst funktioniert <thread> nicht und gibt genau diesen Fehler.
-
danke das wars