Add a singleton class to ensure that dhcpcd-qt runs once per user per display.
authorRoy Marples <roy@marples.name>
Tue, 5 May 2015 23:02:51 +0000 (23:02 +0000)
committerRoy Marples <roy@marples.name>
Tue, 5 May 2015 23:02:51 +0000 (23:02 +0000)
Dang those pesky session managers!

src/dhcpcd-qt/dhcpcd-qt.pro
src/dhcpcd-qt/dhcpcd-singleton.cpp [new file with mode: 0644]
src/dhcpcd-qt/dhcpcd-singleton.h [new file with mode: 0644]
src/dhcpcd-qt/main.cpp

index fc9a9af559a95b79f48a7f1cabfea64c3a61a911..38c37da7ee447fe1b522f7db77dcbdc1f412cc59 100644 (file)
@@ -5,13 +5,13 @@ HEADERS=              dhcpcd-qt.h dhcpcd-about.h dhcpcd-preferences.h \
                        dhcpcd-wi.h dhcpcd-ifmenu.h \
                        dhcpcd-ssid.h \
                        dhcpcd-ssidmenu.h dhcpcd-ssidmenuwidget.h \
-                       dhcpcd-ipv4validator.h
+                       dhcpcd-ipv4validator.h dhcpcd-singleton.h
 SOURCES=               main.cpp dhcpcd-qt.cpp dhcpcd-about.cpp \
                        dhcpcd-preferences.cpp dhcpcd-wi.cpp \
                        dhcpcd-ifmenu.cpp \
                        dhcpcd-ssid.cpp \
                        dhcpcd-ssidmenu.cpp dhcpcd-ssidmenuwidget.cpp \
-                       dhcpcd-ipv4validator.cpp
+                       dhcpcd-ipv4validator.cpp dhcpcd-singleton.cpp
 
 INCLUDEPATH+=          ../../
 INCLUDEPATH+=          ../libdhcpcd/
diff --git a/src/dhcpcd-qt/dhcpcd-singleton.cpp b/src/dhcpcd-qt/dhcpcd-singleton.cpp
new file mode 100644 (file)
index 0000000..baa17f8
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * dhcpcd-qt
+ * Copyright 2014-2015 Roy Marples <roy@marples.name>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/file.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <cerrno>
+#include <iostream>
+#include <string>
+
+#include "dhcpcd-singleton.h"
+
+using namespace std;
+
+DhcpcdSingleton::DhcpcdSingleton()
+{
+
+       fd = -1;
+}
+
+DhcpcdSingleton::~DhcpcdSingleton()
+{
+
+}
+
+bool DhcpcdSingleton::lock()
+{
+       string file;
+       const char *display;
+
+       file = "/tmp/.dhcpcd-qt-";
+       file += getlogin();
+       display = getenv("DISPLAY");
+       if (display && *display != '\0' && strchr(display, '/') == NULL) {
+               file += '.';
+               file += display;
+       }
+       file += ".lock";
+       fd = open(file.c_str(), O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
+       if (fd == -1) {
+               cerr << "dhcpcd-qt: " << "open: " << file << ": "
+                   << strerror(errno) << endl;
+               return false;
+       }
+       if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
+               if (errno != EAGAIN || 1 == 1)
+                       cerr << "dhcpcd-qt: " << "flock: " << file << ": "
+                           << strerror(errno) << endl;
+               return false;
+       }
+       return true;
+}
diff --git a/src/dhcpcd-qt/dhcpcd-singleton.h b/src/dhcpcd-qt/dhcpcd-singleton.h
new file mode 100644 (file)
index 0000000..a75c170
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * dhcpcd-qt
+ * Copyright 2014-2015 Roy Marples <roy@marples.name>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef DHCPCD_SINGLETON_H
+#define DHCPCD_SINGLETON_H
+
+class DhcpcdSingleton
+{
+public:
+       DhcpcdSingleton();
+       ~DhcpcdSingleton();
+
+       bool lock();
+
+private:
+       int fd;
+};
+
+#endif
index 95ffc9c93066e7bb234f99453afa1619df89dfc4..f2ffebd79328b3aaebb8dcc1236ca13b63eb5194 100644 (file)
 #include <QtGui>
 
 #include "dhcpcd-qt.h"
+#include "dhcpcd-singleton.h"
 
 int
 main(int argc, char **argv)
 {
+       /* Ensure we are only started the once by pesky session managers. */
+       DhcpcdSingleton singleton;
+       if (!singleton.lock())
+               return -1;
 
        QApplication app(argc, argv);