Remmina - The GTK+ Remote Desktop Client  v1.4.2
Remmina is a remote desktop client written in GTK+, aiming to be useful for system administrators and travellers, who need to work with lots of remote computers in front of either large monitors or tiny netbooks. Remmina supports multiple network protocols in an integrated and consistent user interface. Currently RDP, VNC, NX, XDMCP and SSH are supported.
remmina_ext_exec.c
Go to the documentation of this file.
1 /*
2  * Remmina - The GTK+ Remote Desktop Client
3  * Copyright (C) 2009-2010 Vic Lee
4  * Copyright (C) 2014-2020 Antenore Gatta, Giovanni Panozzo
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL. * If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so. * If you
30  * do not wish to do so, delete this exception statement from your
31  * version. * If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  *
34  */
35 
36 #include <gdk/gdkkeysyms.h>
37 #include <gtk/gtk.h>
38 #include <glib/gi18n.h>
39 #include <glib.h>
40 #include <stdlib.h>
41 #include <sys/wait.h>
42 #include <unistd.h>
43 #include "remmina_utils.h"
44 #include "remmina_file.h"
45 #include "remmina_ext_exec.h"
46 #include "remmina_public.h"
48 
49 #define SPAWN_TIMEOUT 10
50 
51 #define GET_OBJECT(object_name) gtk_builder_get_object(builder, object_name)
52 
53 static void wait_for_child(GPid pid, gint script_retval, gpointer data)
54 {
55  PCon_Spinner *pcspinner = (PCon_Spinner*)data;
56 
57  gtk_spinner_stop(GTK_SPINNER(pcspinner->spinner));
58  gtk_widget_destroy(GTK_WIDGET(pcspinner->dialog));
59  g_spawn_close_pid(pid);
60  /* TODO At the moment background processes will fail to start before the
61  * remmina connection.
62  * Adding a delay here could be a (not good) solution, or we should
63  * monitor each child opened, but it could be quit tricky and messy */
64 }
65 
66 GtkDialog* remmina_ext_exec_new(RemminaFile* remminafile, const char *remmina_ext_exec_type)
67 {
68  TRACE_CALL(__func__);
69  GtkBuilder *builder;
70  PCon_Spinner *pcspinner;
71  GError *error = NULL;
72  char **argv;
73  gchar *cmd = NULL;
74  gchar pre[11];
75  gchar post[12];
76  GPid child_pid;
77 
78  strcpy(pre, "precommand");
79  strcpy(post, "postcommand");
80 
81  if (remmina_ext_exec_type != NULL && (
82  strcmp(remmina_ext_exec_type, pre) |
83  strcmp(remmina_ext_exec_type, post) )) {
84  cmd = g_strdup(remmina_file_get_string(remminafile, remmina_ext_exec_type));
85  g_debug("[%s] %s", remmina_ext_exec_type, cmd);
86  } else
87  return FALSE;
88 
89  cmd = remmina_file_format_properties(remminafile, cmd);
90  g_debug("[%s] updated to: %s", remmina_ext_exec_type, cmd);
91  if (*cmd != 0) {
92 
93  pcspinner = g_new(PCon_Spinner, 1);
94  builder = remmina_public_gtk_builder_new_from_file("remmina_spinner.glade");
95  pcspinner->dialog = GTK_DIALOG(gtk_builder_get_object(builder, "DialogSpinner"));
96  pcspinner->label_pleasewait = GTK_LABEL(GET_OBJECT("label_pleasewait"));
97  pcspinner->spinner = GTK_WIDGET(GET_OBJECT("spinner"));
98  pcspinner->button_cancel = GTK_BUTTON(GET_OBJECT("button_cancel"));
99  /* Connect signals */
100  gtk_builder_connect_signals(builder, NULL);
101 
102  /* Exec a predefined command */
103  g_shell_parse_argv(cmd, NULL, &argv, &error);
104 
105  if (error) {
106  g_warning("%s\n", error->message);
107  g_error_free(error);
108  }
109 
110  /* Consider using G_SPAWN_SEARCH_PATH_FROM_ENVP (from glib 2.38)*/
111  g_spawn_async( NULL, // cwd
112  argv, // argv
113  NULL, // envp
114  G_SPAWN_SEARCH_PATH |
115  G_SPAWN_SEARCH_PATH_FROM_ENVP |
116  G_SPAWN_DO_NOT_REAP_CHILD, // flags
117  NULL, // child_setup
118  NULL, // child_setup user data
119  &child_pid, // pid location
120  &error); // error
121  if (!error) {
122  gtk_spinner_start(GTK_SPINNER(pcspinner->spinner));
123  g_child_watch_add(child_pid, wait_for_child, (gpointer)pcspinner);
124  gtk_dialog_run(pcspinner->dialog);
125  }else {
126  g_warning("Command %s exited with error: %s\n", cmd, error->message);
127  g_error_free(error);
128  }
129  g_strfreev(argv);
130  return (pcspinner->dialog);
131  }
132  return FALSE;
133 }
const gchar * remmina_file_get_string(RemminaFile *remminafile, const gchar *setting)
Definition: remmina_file.c:449
typedefG_BEGIN_DECLS struct _RemminaFile RemminaFile
Definition: types.h:41
GtkBuilder * remmina_public_gtk_builder_new_from_file(gchar *filename)
gchar * remmina_file_format_properties(RemminaFile *remminafile, const gchar *setting)
Definition: remmina_file.c:494
GtkDialog * remmina_ext_exec_new(RemminaFile *remminafile, const char *remmina_ext_exec_type)
General utility functions, non-GTK related.
static void wait_for_child(GPid pid, gint script_retval, gpointer data)
GtkDialog * dialog
GtkButton * button_cancel
GtkWidget * spinner
GtkLabel * label_pleasewait