Commit 62c99042 authored by Stefan Walter's avatar Stefan Walter

Add option to seahorse-daemon to exec another process once it forks. This

    * agent/seahorse-agent-ssh.c:
    * agent/seahorse-agent.c:
    * agent/seahorse-agent.h:
    * daemon/seahorse-daemon.c: Add option to seahorse-daemon to exec
    another process once it forks. This allows chaining into startup.
parent 3595d4f0
2006-11-24 Nate Nielsen <nielsen@memberwebs.com>
* agent/seahorse-agent-ssh.c:
* agent/seahorse-agent.c:
* agent/seahorse-agent.h:
* daemon/seahorse-daemon.c: Add option to seahorse-daemon to exec
another process once it forks.
2006-11-23 Nate Nielsen <nielsen@memberwebs.com>
* agent/seahorse-agent-ssh.c: Other initialization fixes.
......
......@@ -540,6 +540,18 @@ process_display (pid_t pid)
fflush (stdout);
}
/* Setup the current process environment for using this SSH agent */
static void
process_setenv (pid_t pid)
{
gchar *var;
/* Memory doesn't need to be freed */
var = g_strdup_printf ("%lu", (long unsigned int) pid);
g_setenv ("SSH_AGENT_PID", var, TRUE);
g_setenv ("SSH_AUTH_SOCK", ssh_agent_sockname, TRUE);
}
static gboolean
create_ssh_socket (const gchar *sockname)
{
......@@ -652,6 +664,8 @@ seahorse_agent_ssh_postfork (pid_t child)
/* If any of these fail, they simply exit */
if (seahorse_agent_displayvars)
process_display (child);
else if (seahorse_agent_execvars)
process_setenv (child);
else
swap_sockets ();
}
......
......@@ -40,9 +40,11 @@
#include "seahorse-passphrase.h"
#include "seahorse-pgp-key.h"
gboolean seahorse_agent_enabled = FALSE;
gboolean seahorse_agent_displayvars = FALSE;
gboolean seahorse_agent_cshell = FALSE;
gboolean seahorse_agent_execvars = FALSE;
static gboolean seahorse_agent_enabled = FALSE;
/* PUBLISHING AGENT INFO ---------------------------------------------------- */
......@@ -72,6 +74,16 @@ process_display (const gchar *socket, pid_t pid)
fflush (stdout);
}
static void
process_setenv (const gchar *socket, pid_t pid)
{
gchar *var;
/* Memory doesn't need to be freed */
var = g_strdup_printf ("%s:%lu:1", socket, (long unsigned int) pid);
g_setenv ("GPG_AGENT_INFO", var, TRUE);
}
/* Add our agent info to gpg.conf */
static void
process_gpg_conf (const gchar *socket, pid_t pid)
......@@ -154,6 +166,8 @@ seahorse_agent_postfork (pid_t child)
/* If any of these fail, they simply exit */
if (seahorse_agent_displayvars)
process_display (socket, child);
else if(seahorse_agent_execvars)
process_setenv (socket, child);
else
process_gpg_conf (socket, child);
}
......
......@@ -58,6 +58,7 @@ void seahorse_agent_uninit ();
/* Global options to set from the command line */
extern gboolean seahorse_agent_displayvars;
extern gboolean seahorse_agent_cshell;
extern gboolean seahorse_agent_execvars;
/* -----------------------------------------------------------------------------
* seahorse-agent-io.c
......
......@@ -61,6 +61,9 @@ static const struct poptOption options[] = {
{ "variables", 'v', POPT_ARG_NONE | POPT_ARG_VAL, &seahorse_agent_displayvars, TRUE,
N_("Display variables instead of editing conf files (gpg.conf, ssh agent socket)"), NULL },
{ "execute", 'x', POPT_ARG_NONE | POPT_ARG_VAL, &seahorse_agent_execvars, TRUE,
N_("Execute other arguments on the command line"), NULL },
#endif
POPT_AUTOHELP
......@@ -69,7 +72,7 @@ static const struct poptOption options[] = {
};
static void
daemonize ()
daemonize (const gchar **exec)
{
/*
* We can't use the normal daemon call, because we have
......@@ -85,7 +88,7 @@ daemonize ()
err (1, _("couldn't fork process"));
break;
/* The child */
/* The child */
case 0:
if (setsid () == -1)
err (1, _("couldn't create new process group"));
......@@ -100,7 +103,7 @@ daemonize ()
open ("/dev/null", O_WRONLY, 0666);
chdir ("/tmp");
return;
return; /* Child process returns */
};
}
......@@ -109,15 +112,31 @@ daemonize ()
pid = getpid ();
}
/* The parent process or not daemonizing ... */
#ifdef WITH_AGENT
/* Let the agent do it's thing */
seahorse_agent_postfork (pid);
seahorse_agent_ssh_postfork (pid);
#endif
/* The parent, or not daemonized */
if (g_daemonize)
exit (0);
if (g_daemonize) {
/* If we were asked to exec another program, do that here */
if (!exec || !exec[0])
exit (0);
execvp (exec[0], (char**)exec);
g_critical ("couldn't exec %s: %s\n", exec[0], strerror (errno));
exit (1);
} else {
/* We can't overlay our process with the exec one if not daemonizing */
if (exec && exec[0])
g_warning ("cannot execute process when not daemonizing: %s", exec[0]);
}
}
static void
......@@ -205,6 +224,9 @@ int main(int argc, char* argv[])
SeahorseOperation *op;
GnomeProgram *program = NULL;
GnomeClient *client = NULL;
const char **args = NULL;
poptContext pctx;
GValue value = { 0, };
seahorse_secure_memory_init (65536);
......@@ -230,13 +252,24 @@ int main(int argc, char* argv[])
#ifdef WITH_AGENT
seahorse_agent_prefork ();
seahorse_agent_ssh_prefork ();
#endif
/* If we need to run another program, then prepare that */
if (seahorse_agent_execvars) {
g_value_init (&value, G_TYPE_POINTER);
g_object_get_property (G_OBJECT (program), GNOME_PARAM_POPT_CONTEXT, &value);
pctx = g_value_get_pointer (&value);
g_value_unset (&value);
args = poptGetArgs(pctx);
}
#endif
/*
* All functions after this point have to print messages
* nicely and not just called exit()
*/
daemonize ();
daemonize (args);
/* Handle some signals */
signal (SIGINT, on_quit);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment