Merge pull request #926 from psychon/signals-and-hostname

Signals and hostname
This commit is contained in:
Daniel Hahler 2016-06-04 00:29:02 +02:00
commit 007d3d7a14
3 changed files with 154 additions and 0 deletions

128
luaa.c
View File

@ -56,6 +56,8 @@
#include <xcb/xcb_atom.h> #include <xcb/xcb_atom.h>
#include <unistd.h> /* for gethostname() */
#ifdef WITH_DBUS #ifdef WITH_DBUS
extern const struct luaL_Reg awesome_dbus_lib[]; extern const struct luaL_Reg awesome_dbus_lib[];
#endif #endif
@ -144,6 +146,24 @@ luaA_restart(lua_State *L)
return 0; return 0;
} }
/** Send a signal to a process identified by its process id. See
* `awesome.unix_signal` for a list of signals.
* @tparam integer pid Process identifier
* @tparam integer sig Signal number
* @treturn boolean true if the signal was successfully sent, else false
* @function kill
*/
static int
luaA_kill(lua_State *L)
{
int pid = luaA_checknumber_range(L, 1, 1, INT_MAX);
int sig = luaA_checknumber_range(L, 2, 0, INT_MAX);
int result = kill(pid, sig);
lua_pushboolean(L, result == 0);
return 1;
}
/** Load an image from a given path. /** Load an image from a given path.
* *
* @param name The file name. * @param name The file name.
@ -236,6 +256,8 @@ luaA_fixups(lua_State *L)
* @field startup_errors Error message for errors that occured during * @field startup_errors Error message for errors that occured during
* startup. * startup.
* @field composite_manager_running True if a composite manager is running. * @field composite_manager_running True if a composite manager is running.
* @field unix_signal Table mapping between signal numbers and signal identifiers.
* @field hostname The hostname of the computer on which we are running.
* @table awesome * @table awesome
*/ */
static int static int
@ -284,6 +306,17 @@ luaA_awesome_index(lua_State *L)
return 1; return 1;
} }
if(A_STREQ(buf, "hostname"))
{
/* No good way to handle failures... */
char hostname[256] = "";
gethostname(&hostname[0], countof(hostname));
hostname[countof(hostname) - 1] = '\0';
lua_pushstring(L, hostname);
return 1;
}
return luaA_default_index(L); return luaA_default_index(L);
} }
@ -412,6 +445,99 @@ luaA_dofunction_on_error(lua_State *L)
return 1; return 1;
} }
static void
setup_awesome_signals(lua_State *L)
{
lua_getglobal(L, "awesome");
lua_pushstring(L, "unix_signal");
lua_newtable(L);
#define SETUP_SIGNAL(sig) \
do { \
/* Set awesome.signals["SIGSTOP"] = 42 */ \
lua_pushinteger(L, sig); \
lua_setfield(L, -2, #sig); \
/* Set awesome.signals[42] = "SIGSTOP" */ \
lua_pushinteger(L, sig); \
lua_pushstring(L, #sig); \
lua_settable(L, -3); \
} while (0)
/* Non-standard signals. These are first so that e.g. (on my system)
* signals[29] is SIGPOLL and not SIGIO (the value gets overwritten).
*/
#ifdef SIGIOT
SETUP_SIGNAL(SIGIOT);
#endif
#ifdef SIGEMT
SETUP_SIGNAL(SIGEMT);
#endif
#ifdef SIGSTKFLT
SETUP_SIGNAL(SIGSTKFLT);
#endif
#ifdef SIGIO
SETUP_SIGNAL(SIGIO);
#endif
#ifdef SIGCLD
SETUP_SIGNAL(SIGCLD);
#endif
#ifdef SIGPWR
SETUP_SIGNAL(SIGPWR);
#endif
#ifdef SIGINFO
SETUP_SIGNAL(SIGINFO);
#endif
#ifdef SIGLOST
SETUP_SIGNAL(SIGLOST);
#endif
#ifdef SIGWINCH
SETUP_SIGNAL(SIGWINCH);
#endif
#ifdef SIGUNUSED
SETUP_SIGNAL(SIGUNUSED);
#endif
/* POSIX.1-1990, according to man 7 signal */
SETUP_SIGNAL(SIGHUP);
SETUP_SIGNAL(SIGINT);
SETUP_SIGNAL(SIGQUIT);
SETUP_SIGNAL(SIGILL);
SETUP_SIGNAL(SIGABRT);
SETUP_SIGNAL(SIGFPE);
SETUP_SIGNAL(SIGKILL);
SETUP_SIGNAL(SIGSEGV);
SETUP_SIGNAL(SIGPIPE);
SETUP_SIGNAL(SIGALRM);
SETUP_SIGNAL(SIGTERM);
SETUP_SIGNAL(SIGUSR1);
SETUP_SIGNAL(SIGUSR2);
SETUP_SIGNAL(SIGCHLD);
SETUP_SIGNAL(SIGCONT);
SETUP_SIGNAL(SIGSTOP);
SETUP_SIGNAL(SIGTSTP);
SETUP_SIGNAL(SIGTTIN);
SETUP_SIGNAL(SIGTTOU);
/* POSIX.1-2001, according to man 7 signal */
SETUP_SIGNAL(SIGBUS);
SETUP_SIGNAL(SIGPOLL);
SETUP_SIGNAL(SIGPROF);
SETUP_SIGNAL(SIGSYS);
SETUP_SIGNAL(SIGTRAP);
SETUP_SIGNAL(SIGURG);
SETUP_SIGNAL(SIGVTALRM);
SETUP_SIGNAL(SIGXCPU);
SETUP_SIGNAL(SIGXFSZ);
#undef SETUP_SIGNAL
/* Set awesome.signal to the table we just created, key was already pushed */
lua_rawset(L, -3);
/* Pop "awesome" */
lua_pop(L, 1);
}
/** Initialize the Lua VM /** Initialize the Lua VM
* \param xdg An xdg handle to use to get XDG basedir. * \param xdg An xdg handle to use to get XDG basedir.
*/ */
@ -440,6 +566,7 @@ luaA_init(xdgHandle* xdg)
{ "xkb_get_layout_group", luaA_xkb_get_layout_group}, { "xkb_get_layout_group", luaA_xkb_get_layout_group},
{ "xkb_get_group_names", luaA_xkb_get_group_names}, { "xkb_get_group_names", luaA_xkb_get_group_names},
{ "xrdb_get_value", luaA_xrdb_get_value}, { "xrdb_get_value", luaA_xrdb_get_value},
{ "kill", luaA_kill},
{ NULL, NULL } { NULL, NULL }
}; };
@ -459,6 +586,7 @@ luaA_init(xdgHandle* xdg)
/* Export awesome lib */ /* Export awesome lib */
luaA_openlib(L, "awesome", awesome_lib, awesome_lib); luaA_openlib(L, "awesome", awesome_lib, awesome_lib);
setup_awesome_signals(L);
/* Export root lib */ /* Export root lib */
luaA_registerlib(L, "root", awesome_root_lib); luaA_registerlib(L, "root", awesome_root_lib);

24
tests/test-signal.lua Normal file
View File

@ -0,0 +1,24 @@
--- Tests for spawn + signals
local runner = require("_runner")
local done = false
local function exit_callback(cause, signal)
assert(cause == "signal", cause)
assert(signal == awesome.unix_signal.SIGKILL, signal)
done = true
end
local pid = awesome.spawn({"/bin/sh", "-c", "sleep 60"}, false, false,
false, false, exit_callback)
awesome.kill(pid, awesome.unix_signal.SIGKILL)
runner.run_steps({
function()
if done then
return true
end
end
})
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -8,6 +8,8 @@ local manage_called, c_snid
client.connect_signal("manage", function(c) client.connect_signal("manage", function(c)
manage_called = true manage_called = true
c_snid = c.startup_id c_snid = c.startup_id
assert(c.machine == awesome.hostname,
tostring(c.machine) .. " ~= " .. tostring(awesome.hostname))
end) end)