Fixed bug with unallowed characters
[ArenaLive.git] / action.c
blob70c9fee1ad792b2252a1cae344b3fec9cc9cb29a
1 /*
2 * (C) Copyright 2009 ZeXx86 (zexx86@gmail.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Based on GLugin http://exa.czweb.org/repos/glugin.tar.bz2
19 * Big thanks for "exa"
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/un.h>
31 #include "glugin.h"
34 int plugin_action_0 (pluginData *pd)
36 Log ("action_0");
38 return creat ("/tmp/.arenalivep", 0777);
41 int plugin_action_1 (pluginData *pd)
43 int s, s2, t, l;
44 struct sockaddr_un local, remote;
46 if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1) {
47 Log ("socket == -1");
48 return -1;
51 local.sun_family = AF_UNIX;
52 strcpy (local.sun_path, "/tmp/.arenalive");
53 unlink (local.sun_path);
55 l = strlen (local.sun_path) + sizeof (local.sun_family);
57 if (bind (s, (struct sockaddr *) &local, l) == -1) {
58 Log ("bind == -1");
59 return -1;
62 if (listen (s, 5) == -1)
63 return -1;
65 t = sizeof (remote);
67 pid_t child = fork ();
69 if (child == -1)
70 return -1;
72 if (!child) {
73 char *user = getenv ("USER");
75 if (!user)
76 return -1;
78 if (strlen (user) > 32) {
79 Log ("ERROR -> env USER - too long");
80 _exit (0);
83 unsigned l = strlen (pd->conparam);
84 if (l > 256) {
85 Log ("ERROR -> pd->conparam - too long");
86 _exit (0);
89 conparam_verify (pd->conparam, l);
91 /* prevent before multiple instances */
92 system ("killall arenalive-linux -s9");
94 char cmd[512];
96 // ~80 + 3*32 + 128 + 15 < 512 chars
97 snprintf (cmd, 511, "LD_LIBRARY_PATH=/home/%s/.arenalive/ /home/%s/.arenalive/%s %s &> /home/%s/.arenalive/log",
98 user, user, "arenalive-linux", pd->conparam, user);
100 Log ("-- %s", cmd);
102 system (cmd);
104 _exit (0);
107 if ((s2 = accept (s, (struct sockaddr *) &remote, (unsigned *) &t)) == -1) {
108 Log ("accept == -1");
109 return -1;
112 char str[sizeof (Window)+1];
114 memcpy (str, &pd->win, sizeof (Window));
116 if (send (s2, str, sizeof (Window), 0) < 0)
117 return -1;
119 close (s2);
121 /* plugin loop */
122 glugin_proc (pd);
124 return 0;
127 int plugin_action (pluginData *pd)
129 switch (pd->action) {
130 case 0:
131 return plugin_action_0 (pd);
132 case 1:
133 return plugin_action_1 (pd);
136 return -1;