[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    kdelibs/kdecore [POSSIBLY UNSAFE]
From:       Oswald Buddenhagen <ossi () kde ! org>
Date:       2004-08-18 8:41:27
Message-ID: 20040818084127.B49719985 () office ! kde ! org
[Download RAW message or body]

CVS commit by ossi: 

centralizing kdmExec(). four core apps needing it ought to be enough.


  M +70 -1     kapplication.cpp   1.672 [POSSIBLY UNSAFE: printf]
  M +21 -2     kapplication.h   1.313


--- kdelibs/kdecore/kapplication.h  #1.312:1.313
@@ -339,4 +339,23 @@ public:
 
   /**
+   * Execute a KDM remote control command.
+   * @param cmd the command to execute. FIXME: undocumented yet.
+   *  If NULL, close the socket passed in @p fd only.
+   * @param ret the result buffer. If NULL, a local buffer will be used.
+   * @param fd a pointer to the socket's file descriptor.
+   *  @li If NULL, a socket will be opened for this command only.
+   *  @li If *fd < 0, open a new socket and store it in *fd.
+   *  @li If *fd >= 0, use that socket.
+   * @return result:
+   *  @li If true, the command was successfully executed.
+   *   @p ret might contain addional results.
+   *  @li If false and @p ret is empty, a communication error occurred
+   *   (most probably KDM is not running).
+   *  @li If false and @p ret is non-empty, it contains the error message
+   *   from KDM.
+   */
+  static bool kdmExec( const char *cmd, QCString *ret = 0, int *fd = 0 );
+
+  /**
    * Propagates the network address of the session manager in the
    * SESSION_MANAGER environment variable so that child processes can

--- kdelibs/kdecore/kapplication.cpp  #1.671:1.672
@@ -87,4 +87,6 @@
 #endif
 #include <sys/wait.h>
+#include <sys/socket.h>
+#include <sys/un.h>
 
 #include "kwin.h"
@@ -1067,4 +1069,71 @@ bool KApplication::requestShutDown(
 }
 
+bool KApplication::kdmExec( const char *cmd, QCString *buf, int *fdp )
+{
+    static char *ctl, *dpy;
+    char *ptr;
+    int fd, tl;
+    unsigned len = 0;
+    bool ret = false;
+    struct sockaddr_un sa;
+    QCString lbuf;
+
+    if (!cmd) {
+        if (fdp && *fdp >= 0) {
+            ::close( *fdp );
+            *fdp = -1;
+        }
+        return true;
+    }
+    if (!buf)
+        buf = &lbuf;
+    if (!fdp || (fd = *fdp) < 0) {
+        if (!dpy && !(dpy = ::getenv( "DISPLAY" )))
+            return false;
+        if (!ctl && !(ctl = ::getenv( "DM_CONTROL" )))
+            return false;
+        if ((fd = ::socket( PF_UNIX, SOCK_STREAM, 0 )) < 0)
+            return false;
+        sa.sun_family = AF_UNIX;
+        if ((ptr = strchr( dpy, ':' )))
+            ptr = strchr( ptr, '.' );
+        snprintf( sa.sun_path, sizeof(sa.sun_path),
+                  "%s/dmctl-%.*s/socket", ctl, ptr ? ptr - dpy : 512, dpy );
+        if (::connect( fd, (struct sockaddr *)&sa, sizeof(sa) ))
+            goto bust;
+    }
+    if (::write( fd, cmd, (tl = strlen( cmd )) ) != tl) {
+      bust:
+        if (fdp)
+            *fdp = -1;
+        ::close( fd );
+        buf->resize( 0 );
+        return false;
+    }
+    for (;;) {
+        if (buf->size() < 128)
+            buf->resize( 128 );
+        else if (buf->size() < len * 2)
+            buf->resize( len * 2 );
+        if ((tl = ::read( fd, buf->data() + len, buf->size() - len)) <= 0) {
+            if (tl < 0 && errno == EINTR)
+                continue;
+            goto bust;
+        }
+        len += tl;
+        if ((*buf)[len - 1] == '\n') {
+            (*buf)[len - 1] = 0;
+            if (len > 2 && (*buf)[0] == 'o' && (*buf)[1] == 'k' && (*buf)[2] < 32)
+                ret = true;
+            break;
+        }
+    }
+    if (fdp)
+        *fdp = fd;
+    else
+        ::close( fd );
+    return ret;
+}
+
 void KApplication::propagateSessionManager()
 {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic