Debian Codegraph @

modules/contrib/src/os_testproc.c

raw
1 /*
2 * Copyright (c) 2009 Jilles Tjoelker, et al
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * Reads data from a child process via a pipe.
6 */
7
8 #include "atheme-compat.h"
9
10 #if (CURRENT_ABI_REVISION < 730000)
11 # include "datastream.h"
12 #endif
13
14 #ifndef _WIN32
15
16 struct testprocdata
17 {
18 char dest[COMPAT_NICKLEN + 1];
19 connection_t *pip;
20 };
21
22 static struct testprocdata procdata;
23
24 static void
25 testproc_recvqhandler(connection_t *cptr)
26 {
27 char buf[BUFSIZE];
28 int count;
29 user_t *u;
30
31 if (cptr != procdata.pip)
32 {
33 slog(LG_INFO, "testproc_recvqhandler(): called with unexpected fd %d", cptr->fd);
34 return;
35 }
36
37 count = recvq_getline(cptr, buf, sizeof buf - 1);
38 if (count <= 0)
39 return;
40 if (buf[count - 1] == '\n')
41 count--;
42 if (count > 0 && buf[count - 1] == '\r')
43 count--;
44 if (count == 0)
45 buf[count++] = ' ';
46 buf[count] = '\0';
47 u = user_find(procdata.dest);
48 if (u != NULL)
49 notice(service_find("operserv")->me->nick, u->nick, "%s", buf);
50 }
51
52 static void
53 testproc_closehandler(connection_t *cptr)
54 {
55 if (cptr != procdata.pip)
56 {
57 slog(LG_INFO, "testproc_closehandler(): called with unexpected fd %d", cptr->fd);
58 return;
59 }
60
61 slog(LG_DEBUG, "testproc_closehandler(): fd %d closed", cptr->fd);
62 procdata.pip = NULL;
63 }
64
65 static void
66 os_cmd_testproc(sourceinfo_t *si, int parc, char *parv[])
67 {
68 int pipes[2];
69
70 if (si->su == NULL)
71 {
72 command_fail(si, fault_noprivs, _("\2%s\2 can only be executed via IRC."), "TESTPROC");
73 return;
74 }
75
76 if (procdata.pip != NULL)
77 {
78 command_fail(si, fault_toomany, "Another TESTPROC is still in progress");
79 return;
80 }
81
82 if (pipe(pipes) == -1)
83 {
84 command_fail(si, fault_toomany, "Failed to create pipe");
85 return;
86 }
87 switch (fork())
88 {
89 case -1:
90 close(pipes[0]);
91 close(pipes[1]);
92 command_fail(si, fault_toomany, "Failed to fork");
93 return;
94 case 0:
95 connection_close_all_fds();
96 close(pipes[0]);
97 dup2(pipes[1], 1);
98 dup2(pipes[1], 2);
99 close(pipes[1]);
100 execl("/bin/sh", "sh", "-c", "echo hi; sleep 1; echo hi 2; sleep 0.5; echo hi 3; sleep 4; echo hi 4", (char *)NULL);
101 (void)write(2, "Failed to exec /bin/sh\n", 23);
102 _exit(255);
103 break;
104 default:
105 close(pipes[1]);
106 procdata.pip = connection_add("testproc pipe", pipes[0], 0, recvq_put, NULL);
107 procdata.pip->recvq_handler = testproc_recvqhandler;
108 procdata.pip->close_handler = testproc_closehandler;
109 mowgli_strlcpy(procdata.dest, CLIENT_NAME(si->su), sizeof procdata.dest);
110 break;
111 }
112 }
113
114 static command_t os_testproc = {
115 .name = "TESTPROC",
116 .desc = N_("Does something with child processes."),
117 .access = AC_NONE,
118 .maxparc = 0,
119 .cmd = &os_cmd_testproc,
120 .help = { .path = "contrib/testproc" },
121 };
122
123 static void
124 mod_init(module_t *const restrict m)
125 {
126 service_named_bind_command("operserv", &os_testproc);
127 }
128
129 static void
130 mod_deinit(const module_unload_intent_t intent)
131 {
132 if (procdata.pip != NULL)
133 connection_close_soon(procdata.pip);
134
135 service_named_unbind_command("operserv", &os_testproc);
136 }
137
138 SIMPLE_DECLARE_MODULE_V1("contrib/os_testproc", MODULE_UNLOAD_CAPABILITY_OK)
139
140 #endif