2009-04-03 13:09:17 +02:00
|
|
|
/*
|
|
|
|
* spawn.c - Lua configuration management
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-27 00:24:23 +01:00
|
|
|
/** awesome core API
|
|
|
|
*
|
|
|
|
* @author Julien Danjou <julien@danjou.info>
|
|
|
|
* @copyright 2008-2009 Julien Danjou
|
|
|
|
* @module awesome
|
|
|
|
*/
|
|
|
|
|
2016-06-04 17:39:14 +02:00
|
|
|
/** For some reason the application aborted startup
|
|
|
|
* @param arg Table which only got the "id" key set
|
|
|
|
* @signal spawn::canceled
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** When one of the fields from the @{spawn::initiated} table changes
|
|
|
|
* @param arg Table which describes the spawn event
|
|
|
|
* @signal spawn::change
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** An application finished starting
|
|
|
|
* @param arg Table which only got the "id" key set
|
|
|
|
* @signal spawn::completed
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** When a new client is beginning to start
|
|
|
|
* @param arg Table which describes the spawn event
|
|
|
|
* @signal spawn::initiated
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** An application started a spawn event but didn't start in time.
|
|
|
|
* @param arg Table which only got the "id" key set
|
|
|
|
* @signal spawn::timeout
|
|
|
|
*/
|
|
|
|
|
2014-03-30 20:07:48 +02:00
|
|
|
#include "spawn.h"
|
2009-04-03 13:09:17 +02:00
|
|
|
|
2017-01-13 22:23:49 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2014-03-30 20:07:48 +02:00
|
|
|
#include <unistd.h>
|
2010-10-06 20:08:24 +02:00
|
|
|
#include <glib.h>
|
2009-04-27 20:18:39 +02:00
|
|
|
|
2009-04-03 16:30:18 +02:00
|
|
|
/** 20 seconds timeout */
|
|
|
|
#define AWESOME_SPAWN_TIMEOUT 20.0
|
|
|
|
|
|
|
|
/** Wrapper for unrefing startup sequence.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
a_sn_startup_sequence_unref(SnStartupSequence **sss)
|
|
|
|
{
|
|
|
|
return sn_startup_sequence_unref(*sss);
|
|
|
|
}
|
|
|
|
|
|
|
|
DO_ARRAY(SnStartupSequence *, SnStartupSequence, a_sn_startup_sequence_unref)
|
|
|
|
|
|
|
|
/** The array of startup sequence running */
|
2017-06-01 15:47:24 +02:00
|
|
|
static SnStartupSequence_array_t sn_waits;
|
|
|
|
|
2018-08-21 15:30:07 +02:00
|
|
|
typedef struct {
|
|
|
|
GPid pid;
|
|
|
|
int exit_callback;
|
|
|
|
} running_child_t;
|
|
|
|
|
|
|
|
static int
|
|
|
|
compare_pids(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return ((running_child_t *) a)->pid - ((running_child_t *) b)->pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
DO_BARRAY(running_child_t, running_child, DO_NOTHING, compare_pids)
|
2017-06-01 15:47:24 +02:00
|
|
|
|
2018-08-21 15:30:07 +02:00
|
|
|
static running_child_array_t running_children;
|
2009-04-03 16:30:18 +02:00
|
|
|
|
|
|
|
/** Remove a SnStartupSequence pointer from an array and forget about it.
|
2019-02-22 08:42:43 +01:00
|
|
|
* \param s The startup sequence to find, remove and unref.
|
2009-05-10 01:10:39 +02:00
|
|
|
* \return True if found and removed.
|
2009-04-03 16:30:18 +02:00
|
|
|
*/
|
2009-05-10 01:10:39 +02:00
|
|
|
static inline bool
|
2009-04-03 16:30:18 +02:00
|
|
|
spawn_sequence_remove(SnStartupSequence *s)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < sn_waits.len; i++)
|
|
|
|
if(sn_waits.tab[i] == s)
|
|
|
|
{
|
|
|
|
SnStartupSequence_array_take(&sn_waits, i);
|
2009-05-12 08:11:35 +02:00
|
|
|
sn_startup_sequence_unref(s);
|
2009-05-10 01:10:39 +02:00
|
|
|
return true;
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
2009-05-10 01:10:39 +02:00
|
|
|
return false;
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 21:01:12 +01:00
|
|
|
static gboolean
|
|
|
|
spawn_monitor_timeout(gpointer sequence)
|
2009-04-03 16:30:18 +02:00
|
|
|
{
|
2012-11-21 21:01:12 +01:00
|
|
|
if(spawn_sequence_remove(sequence))
|
2009-05-09 16:03:27 +02:00
|
|
|
{
|
2018-01-24 09:13:35 +01:00
|
|
|
signal_t *sig = signal_array_getbyname(&global_signals, "spawn::timeout");
|
2009-07-31 16:28:17 +02:00
|
|
|
if(sig)
|
|
|
|
{
|
|
|
|
/* send a timeout signal */
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_State *L = globalconf_get_lua_State();
|
|
|
|
lua_createtable(L, 0, 2);
|
|
|
|
lua_pushstring(L, sn_startup_sequence_get_id(sequence));
|
|
|
|
lua_setfield(L, -2, "id");
|
2009-07-31 16:28:17 +02:00
|
|
|
foreach(func, sig->sigfuncs)
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
luaA_object_push(L, (void *) *func);
|
|
|
|
luaA_dofunction(L, 1, 0);
|
2009-07-31 16:28:17 +02:00
|
|
|
}
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pop(L, 1);
|
2009-07-31 16:28:17 +02:00
|
|
|
}
|
2009-05-09 16:03:27 +02:00
|
|
|
}
|
2012-11-21 21:01:12 +01:00
|
|
|
sn_startup_sequence_unref(sequence);
|
|
|
|
return FALSE;
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
spawn_monitor_event(SnMonitorEvent *event, void *data)
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_State *L = globalconf_get_lua_State();
|
2009-04-03 16:30:18 +02:00
|
|
|
SnStartupSequence *sequence = sn_monitor_event_get_startup_sequence(event);
|
|
|
|
SnMonitorEventType event_type = sn_monitor_event_get_type(event);
|
|
|
|
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_createtable(L, 0, 2);
|
|
|
|
lua_pushstring(L, sn_startup_sequence_get_id(sequence));
|
|
|
|
lua_setfield(L, -2, "id");
|
2009-04-03 16:30:18 +02:00
|
|
|
|
2009-07-31 16:28:17 +02:00
|
|
|
const char *event_type_str = NULL;
|
|
|
|
|
2009-04-03 16:30:18 +02:00
|
|
|
switch(event_type)
|
|
|
|
{
|
|
|
|
case SN_MONITOR_EVENT_INITIATED:
|
2009-05-09 15:48:45 +02:00
|
|
|
/* ref the sequence for the array */
|
2009-04-03 16:30:18 +02:00
|
|
|
sn_startup_sequence_ref(sequence);
|
|
|
|
SnStartupSequence_array_append(&sn_waits, sequence);
|
2009-07-31 16:28:17 +02:00
|
|
|
event_type_str = "spawn::initiated";
|
2009-04-03 16:30:18 +02:00
|
|
|
|
|
|
|
/* Add a timeout function so we do not wait for this event to complete
|
|
|
|
* for ever */
|
2012-11-21 21:01:12 +01:00
|
|
|
g_timeout_add_seconds(AWESOME_SPAWN_TIMEOUT, spawn_monitor_timeout, sequence);
|
2009-05-09 15:48:45 +02:00
|
|
|
/* ref the sequence for the callback event */
|
|
|
|
sn_startup_sequence_ref(sequence);
|
2009-04-03 16:30:18 +02:00
|
|
|
break;
|
|
|
|
case SN_MONITOR_EVENT_CHANGED:
|
2009-07-31 16:28:17 +02:00
|
|
|
event_type_str = "spawn::change";
|
2009-04-03 16:30:18 +02:00
|
|
|
break;
|
|
|
|
case SN_MONITOR_EVENT_COMPLETED:
|
2009-07-31 16:28:17 +02:00
|
|
|
event_type_str = "spawn::completed";
|
2009-04-03 16:30:18 +02:00
|
|
|
break;
|
|
|
|
case SN_MONITOR_EVENT_CANCELED:
|
2009-07-31 16:28:17 +02:00
|
|
|
event_type_str = "spawn::canceled";
|
2009-04-03 16:30:18 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* common actions */
|
|
|
|
switch(event_type)
|
|
|
|
{
|
|
|
|
case SN_MONITOR_EVENT_INITIATED:
|
|
|
|
case SN_MONITOR_EVENT_CHANGED:
|
|
|
|
{
|
|
|
|
const char *s = sn_startup_sequence_get_name(sequence);
|
|
|
|
if(s)
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pushstring(L, s);
|
|
|
|
lua_setfield(L, -2, "name");
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if((s = sn_startup_sequence_get_description(sequence)))
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pushstring(L, s);
|
|
|
|
lua_setfield(L, -2, "description");
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
2016-02-06 13:46:46 +01:00
|
|
|
lua_pushinteger(L, sn_startup_sequence_get_workspace(sequence));
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_setfield(L, -2, "workspace");
|
2009-04-03 16:30:18 +02:00
|
|
|
|
|
|
|
if((s = sn_startup_sequence_get_binary_name(sequence)))
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pushstring(L, s);
|
|
|
|
lua_setfield(L, -2, "binary_name");
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if((s = sn_startup_sequence_get_icon_name(sequence)))
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pushstring(L, s);
|
|
|
|
lua_setfield(L, -2, "icon_name");
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if((s = sn_startup_sequence_get_wmclass(sequence)))
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pushstring(L, s);
|
|
|
|
lua_setfield(L, -2, "wmclass");
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SN_MONITOR_EVENT_COMPLETED:
|
|
|
|
case SN_MONITOR_EVENT_CANCELED:
|
|
|
|
spawn_sequence_remove(sequence);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-31 16:28:17 +02:00
|
|
|
/* send the signal */
|
2018-01-24 09:13:35 +01:00
|
|
|
signal_t *sig = signal_array_getbyname(&global_signals, event_type_str);
|
2009-07-31 16:28:17 +02:00
|
|
|
|
|
|
|
if(sig)
|
|
|
|
{
|
|
|
|
foreach(func, sig->sigfuncs)
|
|
|
|
{
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
luaA_object_push(L, (void *) *func);
|
|
|
|
luaA_dofunction(L, 1, 0);
|
2009-07-31 16:28:17 +02:00
|
|
|
}
|
2014-12-06 11:56:58 +01:00
|
|
|
lua_pop(L, 1);
|
2009-07-31 16:28:17 +02:00
|
|
|
}
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Tell the spawn module that an app has been started.
|
|
|
|
* \param c The client that just started.
|
2009-08-24 10:16:52 +02:00
|
|
|
* \param startup_id The startup id of the started application.
|
2009-04-03 16:30:18 +02:00
|
|
|
*/
|
|
|
|
void
|
2009-08-24 10:16:52 +02:00
|
|
|
spawn_start_notify(client_t *c, const char * startup_id)
|
2009-04-03 16:30:18 +02:00
|
|
|
{
|
|
|
|
foreach(_seq, sn_waits)
|
|
|
|
{
|
|
|
|
SnStartupSequence *seq = *_seq;
|
|
|
|
bool found = false;
|
|
|
|
const char *seqid = sn_startup_sequence_get_id(seq);
|
|
|
|
|
2011-11-17 16:38:39 +01:00
|
|
|
if (A_STRNEQ(seqid, startup_id))
|
2009-04-03 16:30:18 +02:00
|
|
|
found = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *seqclass = sn_startup_sequence_get_wmclass(seq);
|
2011-11-17 16:38:39 +01:00
|
|
|
if (A_STREQ(seqclass, c->class) || A_STREQ(seqclass, c->instance))
|
2009-04-03 16:30:18 +02:00
|
|
|
found = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *seqbin = sn_startup_sequence_get_binary_name(seq);
|
2011-11-17 16:38:39 +01:00
|
|
|
if (A_STREQ_CASE(seqbin, c->class) || A_STREQ_CASE(seqbin, c->instance))
|
2009-04-03 16:30:18 +02:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(found)
|
|
|
|
{
|
|
|
|
sn_startup_sequence_complete(seq);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initialize program spawner.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
spawn_init(void)
|
|
|
|
{
|
|
|
|
globalconf.sndisplay = sn_xcb_display_new(globalconf.connection, NULL, NULL);
|
|
|
|
|
2010-08-16 13:57:31 +02:00
|
|
|
globalconf.snmonitor = sn_monitor_context_new(globalconf.sndisplay,
|
|
|
|
globalconf.default_screen,
|
|
|
|
spawn_monitor_event,
|
|
|
|
NULL, NULL);
|
2017-06-02 16:10:36 +02:00
|
|
|
}
|
2017-06-01 15:47:24 +02:00
|
|
|
|
2012-11-21 21:01:12 +01:00
|
|
|
static gboolean
|
|
|
|
spawn_launchee_timeout(gpointer context)
|
2009-04-03 16:30:18 +02:00
|
|
|
{
|
2012-11-21 21:01:12 +01:00
|
|
|
sn_launcher_context_complete(context);
|
|
|
|
sn_launcher_context_unref(context);
|
|
|
|
return FALSE;
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
2009-04-03 13:09:17 +02:00
|
|
|
|
2010-12-04 13:19:01 +01:00
|
|
|
static void
|
|
|
|
spawn_callback(gpointer user_data)
|
|
|
|
{
|
2015-11-01 17:53:53 +01:00
|
|
|
SnLauncherContext *context = (SnLauncherContext *) user_data;
|
2010-12-04 13:19:01 +01:00
|
|
|
setsid();
|
2015-11-01 17:53:53 +01:00
|
|
|
|
|
|
|
if (context)
|
|
|
|
sn_launcher_context_setup_child_process(context);
|
|
|
|
else
|
|
|
|
/* Unset in case awesome was already started with this variable set */
|
|
|
|
unsetenv("DESKTOP_STARTUP_ID");
|
2010-12-04 13:19:01 +01:00
|
|
|
}
|
|
|
|
|
2017-08-13 17:48:45 +02:00
|
|
|
/** Convert a Lua table of strings to a char** array.
|
2017-08-12 16:32:27 +02:00
|
|
|
* \param L The Lua VM state.
|
2017-08-13 17:48:45 +02:00
|
|
|
* \param idx The index of the table that we should parse.
|
2017-08-12 16:32:27 +02:00
|
|
|
* \return The argv array.
|
|
|
|
*/
|
|
|
|
static gchar **
|
|
|
|
parse_table_array(lua_State *L, int idx, GError **error)
|
|
|
|
{
|
|
|
|
gchar **argv = NULL;
|
|
|
|
size_t i, len;
|
|
|
|
|
|
|
|
luaL_checktype(L, idx, LUA_TTABLE);
|
|
|
|
idx = luaA_absindex(L, idx);
|
|
|
|
len = luaA_rawlen(L, idx);
|
|
|
|
|
|
|
|
/* First verify that the table is sane: All integer keys must contain
|
|
|
|
* strings. Do this by pushing them all onto the stack.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
lua_rawgeti(L, idx, i+1);
|
|
|
|
if (lua_type(L, -1) != LUA_TSTRING)
|
|
|
|
{
|
|
|
|
g_set_error(error, G_SPAWN_ERROR, 0,
|
|
|
|
"Non-string argument at table index %zd", i+1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* From this point on nothing can go wrong and so we can safely allocate
|
|
|
|
* memory.
|
|
|
|
*/
|
|
|
|
argv = g_new0(gchar *, len + 1);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
argv[len - i - 1] = g_strdup(lua_tostring(L, -1));
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
2013-03-10 14:54:58 +01:00
|
|
|
/** Parse a command line.
|
|
|
|
* \param L The Lua VM state.
|
2017-08-13 17:48:45 +02:00
|
|
|
* \param idx The index of the argument that we should parse.
|
2013-03-10 14:54:58 +01:00
|
|
|
* \return The argv array for the new process.
|
2009-08-25 11:05:58 +02:00
|
|
|
*/
|
2013-03-10 14:54:58 +01:00
|
|
|
static gchar **
|
2015-03-31 01:17:45 +02:00
|
|
|
parse_command(lua_State *L, int idx, GError **error)
|
2009-08-25 11:05:58 +02:00
|
|
|
{
|
2013-03-10 14:54:58 +01:00
|
|
|
gchar **argv = NULL;
|
2009-08-25 11:05:58 +02:00
|
|
|
|
2013-03-10 14:54:58 +01:00
|
|
|
if (lua_isstring(L, idx))
|
|
|
|
{
|
|
|
|
const char *cmd = luaL_checkstring(L, idx);
|
2015-03-31 01:17:45 +02:00
|
|
|
if(!g_shell_parse_argv(cmd, NULL, &argv, error))
|
2013-03-10 14:54:58 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (lua_istable(L, idx))
|
|
|
|
{
|
2017-08-12 16:32:27 +02:00
|
|
|
argv = parse_table_array(L, idx, error);
|
2013-03-10 14:54:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-12 16:23:35 +02:00
|
|
|
g_set_error_literal(error, G_SPAWN_ERROR, 0,
|
2017-08-13 17:48:45 +02:00
|
|
|
"Invalid argument to spawn(), expected string or table");
|
2017-08-12 16:23:35 +02:00
|
|
|
return NULL;
|
2013-03-10 14:54:58 +01:00
|
|
|
}
|
2009-08-25 11:05:58 +02:00
|
|
|
|
2013-03-10 14:54:58 +01:00
|
|
|
return argv;
|
2009-08-25 11:05:58 +02:00
|
|
|
}
|
|
|
|
|
2015-11-29 11:53:12 +01:00
|
|
|
/** Callback for when a spawned process exits. */
|
2018-08-21 15:30:07 +02:00
|
|
|
void
|
|
|
|
spawn_child_exited(pid_t pid, int status)
|
2015-11-29 11:53:12 +01:00
|
|
|
{
|
2018-08-21 15:30:07 +02:00
|
|
|
int exit_callback;
|
|
|
|
running_child_t needle = { .pid = pid };
|
2015-11-29 11:53:12 +01:00
|
|
|
lua_State *L = globalconf_get_lua_State();
|
2018-08-21 15:30:07 +02:00
|
|
|
|
|
|
|
running_child_t *child = running_child_array_lookup(&running_children, &needle);
|
|
|
|
if (child == NULL) {
|
2018-10-04 19:09:01 +02:00
|
|
|
warn("Unknown child %d exited with %s %d",
|
|
|
|
(int)pid, WIFEXITED(status) ? "status" : "signal", status);
|
2018-08-21 15:30:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
exit_callback = child->exit_callback;
|
|
|
|
running_child_array_remove(&running_children, child);
|
2015-11-29 11:53:12 +01:00
|
|
|
|
|
|
|
/* 'Decode' the exit status */
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
lua_pushliteral(L, "exit");
|
2016-02-06 13:46:46 +01:00
|
|
|
lua_pushinteger(L, WEXITSTATUS(status));
|
2015-11-29 11:53:12 +01:00
|
|
|
} else {
|
2017-05-13 23:22:15 +02:00
|
|
|
check(WIFSIGNALED(status));
|
2015-11-29 11:53:12 +01:00
|
|
|
lua_pushliteral(L, "signal");
|
2016-02-06 13:46:46 +01:00
|
|
|
lua_pushinteger(L, WTERMSIG(status));
|
2015-11-29 11:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, exit_callback);
|
|
|
|
luaA_dofunction(L, 2, 0);
|
|
|
|
luaA_unregister(L, &exit_callback);
|
|
|
|
}
|
|
|
|
|
2009-04-03 13:09:17 +02:00
|
|
|
/** Spawn a program.
|
2015-07-24 15:20:55 +02:00
|
|
|
* The program will be started on the default screen.
|
2015-02-27 00:24:23 +01:00
|
|
|
*
|
2015-07-25 15:16:23 +02:00
|
|
|
* @tparam string|table cmd The command to launch.
|
|
|
|
* @tparam[opt=true] boolean use_sn Use startup-notification?
|
2024-11-19 14:56:09 +01:00
|
|
|
* @tparam[opt="DEV_NULL"] boolean|string stdin Pass `true` to return a fd for
|
|
|
|
* stdin. Use `"DEV_NULL"` to redirect to /dev/null, or `"INHERIT"` to inherit
|
|
|
|
* the parent's stdin. Implementation note: Pre-2.74 glib doesn't support
|
|
|
|
* *explicit* `DEV_NULL`. When `DEV_NULL` is passed on glib <2.74, Awesome will
|
|
|
|
* use glib's default behaviour.
|
|
|
|
* @tparam[opt="INHERIT"] boolean|string stdout Pass `true` to return a fd for
|
|
|
|
* stdout. Use `"DEV_NULL"` to redirect to /dev/null, or `"INHERIT"` to
|
|
|
|
* inherit the parent's stdout. Implementation note: Pre-2.74 glib doesn't
|
|
|
|
* support *explicit* `INHERIT`. When `INHERIT` is passed on glib <2.74,
|
|
|
|
* Awesome will use glib's default behaviour.
|
|
|
|
* @tparam[opt="INHERIT"] boolean|string stderr Pass `true` to return a fd for
|
|
|
|
* stderr. Use `"DEV_NULL"` to redirect to /dev/null, or `"INHERIT"` to
|
|
|
|
* inherit the parent's stderr. Implementation note: Pre-2.74 glib doesn't
|
|
|
|
* support *explicit* `INHERIT`. When `INHERIT` is passed on glib <2.74,
|
|
|
|
* Awesome will use glib's default behaviour.
|
2015-11-29 11:53:12 +01:00
|
|
|
* @tparam[opt=nil] function exit_callback Function to call on process exit. The
|
2017-08-13 17:48:45 +02:00
|
|
|
* function arguments will be type of exit ("exit" or "signal") and the exit
|
|
|
|
* code / the signal number causing process termination.
|
2017-08-12 16:39:39 +02:00
|
|
|
* @tparam[opt=nil] table cmd The environment to use for the spawned program.
|
2017-08-13 17:48:45 +02:00
|
|
|
* Without this the spawned process inherits awesome's environment.
|
2015-07-25 15:16:23 +02:00
|
|
|
* @treturn[1] integer Process ID if everything is OK.
|
|
|
|
* @treturn[1] string Startup-notification ID, if `use_sn` is true.
|
2015-08-13 09:05:52 +02:00
|
|
|
* @treturn[1] integer stdin, if `stdin` is true.
|
|
|
|
* @treturn[1] integer stdout, if `stdout` is true.
|
|
|
|
* @treturn[1] integer stderr, if `stderr` is true.
|
2017-08-12 16:10:45 +02:00
|
|
|
* @treturn[2] string An error string if an error occurred.
|
2019-06-08 01:08:05 +02:00
|
|
|
* @staticfct spawn
|
2009-04-03 13:09:17 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
luaA_spawn(lua_State *L)
|
|
|
|
{
|
2017-08-12 16:39:39 +02:00
|
|
|
gchar **argv = NULL, **envp = NULL;
|
2015-08-13 09:05:52 +02:00
|
|
|
bool use_sn = true, return_stdin = false, return_stdout = false, return_stderr = false;
|
|
|
|
int stdin_fd = -1, stdout_fd = -1, stderr_fd = -1;
|
|
|
|
int *stdin_ptr = NULL, *stdout_ptr = NULL, *stderr_ptr = NULL;
|
2015-11-29 11:53:12 +01:00
|
|
|
GSpawnFlags flags = 0;
|
2013-03-10 14:54:58 +01:00
|
|
|
gboolean retval;
|
|
|
|
GPid pid;
|
2009-04-03 13:09:17 +02:00
|
|
|
|
2009-04-03 16:30:18 +02:00
|
|
|
if(lua_gettop(L) >= 2)
|
|
|
|
use_sn = luaA_checkboolean(L, 2);
|
2024-11-19 14:56:09 +01:00
|
|
|
/* Valid values for return_std* are:
|
|
|
|
* true -> return a fd
|
|
|
|
* false -> keep glib's default behaviour
|
|
|
|
* "DEV_NULL" -> use direct output to /dev/null
|
|
|
|
* "INHERIT" -> use the same fd as the parent
|
|
|
|
*/
|
|
|
|
if(lua_gettop(L) >= 3) {
|
|
|
|
if (lua_isstring(L, 3)) {
|
|
|
|
const char *str = lua_tostring(L, 3);
|
|
|
|
if (a_strcmp(str, "DEV_NULL") == 0){
|
|
|
|
// This is the default behaviour. Compiles to a no-op before 2.74.
|
|
|
|
#if GLIB_CHECK_VERSION(2, 74, 0)
|
|
|
|
flags |= G_SPAWN_STDIN_FROM_DEV_NULL;
|
|
|
|
# endif
|
|
|
|
} else if (a_strcmp(str, "INHERIT") == 0)
|
|
|
|
flags |= G_SPAWN_CHILD_INHERITS_STDIN;
|
|
|
|
else
|
|
|
|
luaA_typerror(L, 3, "DEV_NULL or INHERIT");
|
|
|
|
} else if(lua_isboolean(L, 3)) {
|
|
|
|
return_stdin = lua_toboolean(L, 3);
|
|
|
|
} else {
|
|
|
|
luaA_typerror(L, 3, "boolean or string");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(lua_gettop(L) >= 4) {
|
|
|
|
if (lua_isstring(L, 4)) {
|
|
|
|
const char *str = lua_tostring(L, 4);
|
|
|
|
if (a_strcmp(str, "DEV_NULL") == 0)
|
|
|
|
flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
|
|
|
|
else if (a_strcmp(str, "INHERIT") == 0) {
|
|
|
|
// This is the default behaviour. Compiles to a no-op before 2.74.
|
|
|
|
#if GLIB_CHECK_VERSION(2, 74, 0)
|
|
|
|
flags |= G_SPAWN_CHILD_INHERITS_STDOUT;
|
|
|
|
# endif
|
|
|
|
} else
|
|
|
|
luaA_typerror(L, 4, "DEV_NULL or INHERIT");
|
|
|
|
} else if(lua_isboolean(L, 4)) {
|
|
|
|
return_stdout = lua_toboolean(L, 4);
|
|
|
|
} else {
|
|
|
|
luaA_typerror(L, 4, "boolean or string");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(lua_gettop(L) >= 5) {
|
|
|
|
if (lua_isstring(L, 5)) {
|
|
|
|
const char *str = lua_tostring(L, 5);
|
|
|
|
if (a_strcmp(str, "DEV_NULL") == 0)
|
|
|
|
flags |= G_SPAWN_STDERR_TO_DEV_NULL;
|
|
|
|
else if (a_strcmp(str, "INHERIT") == 0) {
|
|
|
|
// This is the default behaviour. Compiles to a no-op before 2.74.
|
|
|
|
#if GLIB_CHECK_VERSION(2, 74, 0)
|
|
|
|
flags |= G_SPAWN_CHILD_INHERITS_STDERR;
|
|
|
|
# endif
|
|
|
|
} else
|
|
|
|
luaA_typerror(L, 5, "DEV_NULL or INHERIT");
|
|
|
|
} else if(lua_isboolean(L, 5)) {
|
|
|
|
return_stderr = lua_toboolean(L, 5);
|
|
|
|
} else {
|
|
|
|
luaA_typerror(L, 5, "boolean or string");
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 11:32:53 +01:00
|
|
|
if (!lua_isnoneornil(L, 6))
|
2015-11-29 11:53:12 +01:00
|
|
|
{
|
|
|
|
luaA_checkfunction(L, 6);
|
|
|
|
flags |= G_SPAWN_DO_NOT_REAP_CHILD;
|
|
|
|
}
|
2015-08-13 09:05:52 +02:00
|
|
|
if(return_stdin)
|
|
|
|
stdin_ptr = &stdin_fd;
|
|
|
|
if(return_stdout)
|
|
|
|
stdout_ptr = &stdout_fd;
|
|
|
|
if(return_stderr)
|
|
|
|
stderr_ptr = &stderr_fd;
|
2009-04-03 16:30:18 +02:00
|
|
|
|
2015-03-31 01:17:45 +02:00
|
|
|
GError *error = NULL;
|
|
|
|
argv = parse_command(L, 1, &error);
|
2014-03-12 11:10:48 +01:00
|
|
|
if(!argv || !argv[0])
|
|
|
|
{
|
|
|
|
g_strfreev(argv);
|
2016-08-10 00:29:22 +02:00
|
|
|
if (error) {
|
2016-12-25 13:38:49 +01:00
|
|
|
lua_pushfstring(L, "spawn: parse error: %s", error->message);
|
2016-08-10 00:29:22 +02:00
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
else
|
2016-12-25 13:38:49 +01:00
|
|
|
lua_pushliteral(L, "spawn: There is nothing to execute");
|
2015-03-31 01:17:45 +02:00
|
|
|
return 1;
|
2014-03-12 11:10:48 +01:00
|
|
|
}
|
2009-04-03 13:09:17 +02:00
|
|
|
|
2017-08-12 16:39:39 +02:00
|
|
|
if (!lua_isnoneornil(L, 7)) {
|
|
|
|
envp = parse_table_array(L, 7, &error);
|
|
|
|
if (error) {
|
|
|
|
g_strfreev(argv);
|
|
|
|
g_strfreev(envp);
|
|
|
|
lua_pushfstring(L, "spawn: environment parse error: %s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-27 20:18:39 +02:00
|
|
|
SnLauncherContext *context = NULL;
|
2009-04-03 16:30:18 +02:00
|
|
|
if(use_sn)
|
|
|
|
{
|
2010-08-16 13:47:40 +02:00
|
|
|
context = sn_launcher_context_new(globalconf.sndisplay, globalconf.default_screen);
|
2009-04-03 16:30:18 +02:00
|
|
|
sn_launcher_context_set_name(context, "awesome");
|
|
|
|
sn_launcher_context_set_description(context, "awesome spawn");
|
2014-03-12 11:15:40 +01:00
|
|
|
sn_launcher_context_set_binary_name(context, argv[0]);
|
|
|
|
sn_launcher_context_initiate(context, "awesome", argv[0], globalconf.timestamp);
|
2009-04-03 16:30:18 +02:00
|
|
|
|
|
|
|
/* app will have AWESOME_SPAWN_TIMEOUT seconds to complete,
|
|
|
|
* or the timeout function will terminate the launch sequence anyway */
|
2012-11-21 21:01:12 +01:00
|
|
|
g_timeout_add_seconds(AWESOME_SPAWN_TIMEOUT, spawn_launchee_timeout, context);
|
2009-04-03 16:30:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:13:26 +02:00
|
|
|
flags |= G_SPAWN_SEARCH_PATH | G_SPAWN_CLOEXEC_PIPES;
|
2017-08-12 16:39:39 +02:00
|
|
|
retval = g_spawn_async_with_pipes(NULL, argv, envp, flags,
|
2015-11-01 17:53:53 +01:00
|
|
|
spawn_callback, context, &pid,
|
2015-08-13 09:05:52 +02:00
|
|
|
stdin_ptr, stdout_ptr, stderr_ptr, &error);
|
2013-03-10 14:54:58 +01:00
|
|
|
g_strfreev(argv);
|
2017-08-12 16:39:39 +02:00
|
|
|
g_strfreev(envp);
|
2013-03-10 14:54:58 +01:00
|
|
|
if(!retval)
|
2009-04-03 13:09:17 +02:00
|
|
|
{
|
2016-12-25 13:38:49 +01:00
|
|
|
lua_pushstring(L, error->message);
|
2009-04-27 20:18:39 +02:00
|
|
|
g_error_free(error);
|
|
|
|
if(context)
|
|
|
|
sn_launcher_context_complete(context);
|
|
|
|
return 1;
|
2009-04-03 13:09:17 +02:00
|
|
|
}
|
|
|
|
|
2015-11-29 11:53:12 +01:00
|
|
|
if(flags & G_SPAWN_DO_NOT_REAP_CHILD)
|
|
|
|
{
|
|
|
|
/* Only do this down here to avoid leaks in case of errors */
|
2018-08-21 15:30:07 +02:00
|
|
|
running_child_t child = { .pid = pid, .exit_callback = LUA_REFNIL };
|
|
|
|
luaA_registerfct(L, 6, &child.exit_callback);
|
|
|
|
running_child_array_insert(&running_children, child);
|
2015-11-29 11:53:12 +01:00
|
|
|
}
|
|
|
|
|
2010-04-24 11:49:57 +02:00
|
|
|
/* push pid on stack */
|
2016-02-06 13:46:46 +01:00
|
|
|
lua_pushinteger(L, pid);
|
2010-04-24 11:49:57 +02:00
|
|
|
|
2014-03-15 23:09:33 +01:00
|
|
|
/* push sn on stack */
|
|
|
|
if (context)
|
2015-08-13 09:05:52 +02:00
|
|
|
lua_pushstring(L, sn_launcher_context_get_startup_id(context));
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
|
|
|
|
if(return_stdin)
|
|
|
|
lua_pushinteger(L, stdin_fd);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
if(return_stdout)
|
|
|
|
lua_pushinteger(L, stdout_fd);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
|
|
|
if(return_stderr)
|
|
|
|
lua_pushinteger(L, stderr_fd);
|
|
|
|
else
|
|
|
|
lua_pushnil(L);
|
2014-03-15 23:09:33 +01:00
|
|
|
|
2015-08-13 09:05:52 +02:00
|
|
|
return 5;
|
2009-04-03 13:09:17 +02:00
|
|
|
}
|
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|