This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch main
in repository scribble.

View the commit online.

commit 918443707e528b1f974159f8e725f6d40d838924
Author: Alastair Poole <[email protected]>
AuthorDate: Mon Nov 30 22:03:35 2026 +0000

    open: avoid infinite opening.
---
 src/bin/scribble_file_open.c | 108 +++++++++++++++++++++++++++++++++++++++++++
 src/bin/scribble_file_open.h |   3 ++
 src/bin/scribble_main.c      |  42 ++++++++++++++++-
 3 files changed, 151 insertions(+), 2 deletions(-)

diff --git a/src/bin/scribble_file_open.c b/src/bin/scribble_file_open.c
index 7a7943b..3921b56 100644
--- a/src/bin/scribble_file_open.c
+++ b/src/bin/scribble_file_open.c
@@ -1,8 +1,116 @@
+#define _GNU_SOURCE
 #include "scribble_file_open.h"
 
 #include <errno.h>
+#include <limits.h>
+#include <stddef.h>
 #include <spawn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+int
+scribble_file_open_endpoint(int argc, char **argv, int *endpoint)
+{
+   struct sockaddr_un address = { .sun_family = AF_UNIX };
+   const struct timeval timeout = { .tv_sec = 5 };
+   int credentials = 1;
+   int fd, error, index;
+   socklen_t length;
+   char *cwd = NULL;
+
+   length = offsetof(struct sockaddr_un, sun_path) + 1 +
+      snprintf(address.sun_path + 1, sizeof(address.sun_path) - 1,
+               "scribble.%lu", (unsigned long)geteuid());
+   fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+   if (fd < 0) return -1;
+
+   if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &credentials, sizeof(credentials)) < 0 ||
+       setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0)
+     goto failed;
+
+   if (bind(fd, (struct sockaddr *)&address, length) == 0)
+     {
+        *endpoint = fd;
+        return 0;
+     }
+   if (errno != EADDRINUSE) goto failed;
+
+   for (index = 1; index < argc || index == 1; index++)
+     {
+        const char *path = index < argc ? argv[index] : "";
+        char *absolute = NULL;
+        ssize_t sent;
+
+        if (*path && *path != '/')
+          {
+             if (!cwd) cwd = getcwd(NULL, 0);
+             if (!cwd || asprintf(&absolute, "%s/%s", cwd, path) < 0) goto failed;
+             path = absolute;
+          }
+        if (strlen(path) >= PATH_MAX)
+          {
+             free(absolute);
+             errno = ENAMETOOLONG;
+             goto failed;
+          }
+        do
+          sent = sendto(fd, path, strlen(path) + 1, 0, (struct sockaddr *)&address, length);
+        while (sent < 0 && errno == EINTR);
+        error = errno;
+        free(absolute);
+        if (sent < 0)
+          {
+             errno = error;
+             goto failed;
+          }
+     }
+
+   free(cwd);
+   close(fd);
+   return 1;
+
+failed:
+   error = errno;
+   free(cwd);
+   close(fd);
+   errno = error;
+   return -1;
+}
+
+Eina_Bool
+scribble_file_open_receive(int endpoint, char *path, size_t capacity)
+{
+   union
+   {
+      struct cmsghdr alignment;
+      char bytes[CMSG_SPACE(sizeof(struct ucred))];
+   } control;
+   struct iovec buffer = { .iov_base = path, .iov_len = capacity };
+   struct msghdr message = {
+      .msg_iov = &buffer, .msg_iovlen = 1,
+      .msg_control = control.bytes, .msg_controllen = sizeof(control.bytes)
+   };
+   struct cmsghdr *header;
+   struct ucred credentials;
+   ssize_t size;
+
+   size = recvmsg(endpoint, &message, MSG_DONTWAIT);
+   if (size <= 0 || (message.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) ||
+       memchr(path, '\0', size) != path + size - 1)
+     return EINA_FALSE;
+
+   header = CMSG_FIRSTHDR(&message);
+   if (!header || header->cmsg_level != SOL_SOCKET || header->cmsg_type != SCM_CREDENTIALS ||
+       header->cmsg_len != CMSG_LEN(sizeof(credentials)))
+     return EINA_FALSE;
+   memcpy(&credentials, CMSG_DATA(header), sizeof(credentials));
+   return credentials.uid == geteuid() && (!*path || *path == '/');
+}
 
 extern char **environ;
 
diff --git a/src/bin/scribble_file_open.h b/src/bin/scribble_file_open.h
index 2dc05d9..e725daa 100644
--- a/src/bin/scribble_file_open.h
+++ b/src/bin/scribble_file_open.h
@@ -3,6 +3,9 @@
 
 #include "scribble_app.h"
 
+/* Returns 0 for the primary instance, 1 after forwarding, or -1 on error. */
+int       scribble_file_open_endpoint(int argc, char **argv, int *endpoint);
+Eina_Bool scribble_file_open_receive(int endpoint, char *path, size_t capacity);
 Eina_Bool scribble_file_open_external(const char *path);
 
 Eina_Bool scribble_open_in_panel(Scribble_App *app, Scribble_Panel *panel, const char *path);
diff --git a/src/bin/scribble_main.c b/src/bin/scribble_main.c
index 42e5c79..3d326db 100644
--- a/src/bin/scribble_main.c
+++ b/src/bin/scribble_main.c
@@ -11,6 +11,7 @@
 #include <Efreet.h>
 
 #include <errno.h>
+#include <limits.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
@@ -394,7 +395,6 @@ scribble_open_requested(Scribble_App *app, const char *path, Scribble_Document *
 {
    *document = scribble_open_path(app, path);
    if (*document) return EINA_TRUE;
-   if (path && errno == ENOEXEC && scribble_file_open_external(path)) return EINA_TRUE;
 
    int error = errno;
    if (app->dialog) scribble_dialog_clear(app);
@@ -1775,21 +1775,57 @@ scribble_elementary_config_changed(void *data, int type EINA_UNUSED,
    return ECORE_CALLBACK_RENEW;
 }
 
+static Eina_Bool
+scribble_forwarded_open(void *data, Ecore_Fd_Handler *handler)
+{
+   Scribble_App *app = data;
+   char path[PATH_MAX];
+
+   if (!app->window) return ECORE_CALLBACK_RENEW;
+   if (!scribble_file_open_receive(ecore_main_fd_handler_fd_get(handler), path, sizeof(path)))
+     return ECORE_CALLBACK_RENEW;
+   elm_win_iconified_set(app->window, EINA_FALSE);
+   evas_object_show(app->window);
+   elm_win_activate(app->window);
+   if (*path) scribble_open(app, path);
+   else if (app->active && !app->dialog) scribble_editor_activate(app->active);
+   return ECORE_CALLBACK_RENEW;
+}
+
 EAPI_MAIN int
 elm_main(int argc, char **argv)
 {
    Scribble_App app;
    Scribble_Document *document;
    Ecore_Event_Handler *config_handler;
-   int index;
+   Ecore_Fd_Handler *open_handler;
+   int endpoint, result, index;
+
+   result = scribble_file_open_endpoint(argc, argv, &endpoint);
+   if (result > 0) return 0;
+   if (result < 0)
+     {
+        perror("scribble");
+        return 1;
+     }
 
    scribble_intl_init();
    setenv("EFL_WIN_FRAME_MODE", "off", 0);
    app = (Scribble_App){0};
+   open_handler = ecore_main_fd_handler_add(endpoint, ECORE_FD_READ,
+                                            scribble_forwarded_open, &app, NULL, NULL);
+   if (!open_handler)
+     {
+        close(endpoint);
+        return 1;
+     }
+
    scribble_config_load(&app.config);
    if (!scribble_theme_apply(app.config.preferences.theme)) app.config.preferences.theme = SCRIBBLE_THEME_DEFAULT;
    if (!scribble_window_create(&app))
      {
+        ecore_main_fd_handler_del(open_handler);
+        close(endpoint);
         scribble_config_clear(&app.config);
         return 1;
      }
@@ -1802,6 +1838,8 @@ elm_main(int argc, char **argv)
                                              scribble_elementary_config_changed, &app);
    app.external_timer = ecore_timer_add(1.0, scribble_external_tick, &app);
    elm_run();
+   ecore_main_fd_handler_del(open_handler);
+   close(endpoint);
 
    if (app.external_timer) ecore_timer_del(app.external_timer);
    if (config_handler) ecore_event_handler_del(config_handler);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to