spawn: use glib spawn module to catch error

This is a lot better than our previous code. We can now report execution
error to Lua directly.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-04-27 20:18:39 +02:00
parent 57aeb2b85e
commit 0705fbf29d
3 changed files with 16 additions and 15 deletions

View File

@ -135,6 +135,7 @@ pkg_check_modules(AWESOME_COMMON_REQUIRED REQUIRED
xcb>=1.1) xcb>=1.1)
pkg_check_modules(AWESOME_REQUIRED REQUIRED pkg_check_modules(AWESOME_REQUIRED REQUIRED
glib-2.0
cairo cairo
x11 x11
pango>=1.19.3 pango>=1.19.3

View File

@ -242,7 +242,8 @@ globalkeys = awful.util.table.join(
function () function ()
awful.prompt.run({ prompt = "Run: " }, awful.prompt.run({ prompt = "Run: " },
mypromptbox[mouse.screen], mypromptbox[mouse.screen],
awful.util.spawn, awful.completion.shell, function (...) mypromptbox[mouse.screen].text = awful.util.spawn(unpack(arg)) end,
awful.completion.shell,
awful.util.getdir("cache") .. "/history") awful.util.getdir("cache") .. "/history")
end), end),

27
spawn.c
View File

@ -24,6 +24,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <glib/gspawn.h>
#include "spawn.h" #include "spawn.h"
#include "screen.h" #include "screen.h"
#include "luaa.h" #include "luaa.h"
@ -228,6 +230,7 @@ spawn_launchee_timeout(struct ev_loop *loop, ev_timer *w, int revents)
* \lparam The command to launch. * \lparam The command to launch.
* \lparam Use startup-notification, true or false, default to true. * \lparam Use startup-notification, true or false, default to true.
* \lparam The optional screen number to spawn the command on. * \lparam The optional screen number to spawn the command on.
* \lreturn Nothing if launch was successful, an error string otherwise.
*/ */
int int
luaA_spawn(lua_State *L) luaA_spawn(lua_State *L)
@ -256,6 +259,7 @@ luaA_spawn(lua_State *L)
p_delete(&host); p_delete(&host);
} }
SnLauncherContext *context = NULL;
if(use_sn) if(use_sn)
{ {
char *cmdname, *space; char *cmdname, *space;
@ -264,7 +268,7 @@ luaA_spawn(lua_State *L)
else else
cmdname = a_strdup(cmd); cmdname = a_strdup(cmd);
SnLauncherContext *context = sn_launcher_context_new(globalconf.sndisplay, screen_virttophys(screen)); context = sn_launcher_context_new(globalconf.sndisplay, screen_virttophys(screen));
sn_launcher_context_set_name(context, "awesome"); sn_launcher_context_set_name(context, "awesome");
sn_launcher_context_set_description(context, "awesome spawn"); sn_launcher_context_set_description(context, "awesome spawn");
sn_launcher_context_set_binary_name(context, cmdname); sn_launcher_context_set_binary_name(context, cmdname);
@ -280,21 +284,16 @@ luaA_spawn(lua_State *L)
sn_launcher_context_setup_child_process(context); sn_launcher_context_setup_child_process(context);
} }
/* The double-fork construct avoids zombie processes and keeps the code GError *error = NULL;
* clean from stupid signal handlers. */ if(!g_spawn_command_line_async(cmd, &error))
if(fork() == 0)
{ {
if(fork() == 0) /* push error on stack */
{ lua_pushstring(L, error->message);
if(globalconf.connection) g_error_free(error);
xcb_disconnect(globalconf.connection); if(context)
setsid(); sn_launcher_context_complete(context);
a_exec(cmd); return 1;
warn("execl '%s' failed: %s\n", cmd, strerror(errno));
} }
exit(EXIT_SUCCESS);
}
wait(0);
return 0; return 0;
} }