Debian Codegraph @

assuan/assuan-pipe-server.c

raw symbols SCIP
1 /* assuan-pipe-server.c - Assuan server working over a pipe
2 * Copyright (C) 2001 Free Software Foundation, Inc.
3 *
4 * This file is part of GnuPG.
5 *
6 * GnuPG 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 * GnuPG 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, Boston, MA 02110-1301, USA
19 */
20
21 #include <config.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 #include "assuan-defs.h"
26
27 static void
28 deinit_pipe_server (ASSUAN_CONTEXT ctx)
29 {
30 /* nothing to do for this simple server */
31 }
32
33 static int
34 accept_connection (ASSUAN_CONTEXT ctx)
35 {
36 /* This is a NOP for a pipe server */
37 return 0;
38 }
39
40 static int
41 finish_connection (ASSUAN_CONTEXT ctx)
42 {
43 /* This is a NOP for a pipe server */
44 return 0;
45 }
46
47
48 /* Create a new context. Note that the handlers are set up for a pipe
49 server/client - this wau we don't need extra dummy functions */
50 int
51 _assuan_new_context (ASSUAN_CONTEXT *r_ctx)
52 {
53 ASSUAN_CONTEXT ctx;
54 int rc;
55
56 *r_ctx = NULL;
57 ctx = xtrycalloc (1, sizeof *ctx);
58 if (!ctx)
59 return ASSUAN_Out_Of_Core;
60 ctx->input_fd = -1;
61 ctx->output_fd = -1;
62
63 ctx->inbound.fd = -1;
64 ctx->outbound.fd = -1;
65
66 ctx->listen_fd = -1;
67 ctx->client_pid = (pid_t)-1;
68 /* use the pipe server handler as a default */
69 ctx->deinit_handler = deinit_pipe_server;
70 ctx->accept_handler = accept_connection;
71 ctx->finish_handler = finish_connection;
72
73 rc = _assuan_register_std_commands (ctx);
74 if (rc)
75 xfree (ctx);
76 else
77 *r_ctx = ctx;
78 return rc;
79 }
80
81
82
83 int
84 assuan_init_pipe_server (ASSUAN_CONTEXT *r_ctx, int filedes[2])
85 {
86 int rc;
87
88 rc = _assuan_new_context (r_ctx);
89 if (!rc)
90 {
91 ASSUAN_CONTEXT ctx = *r_ctx;
92
93 ctx->is_server = 1;
94 ctx->inbound.fd = filedes[0];
95 ctx->outbound.fd = filedes[1];
96 ctx->pipe_mode = 1;
97 }
98 return rc;
99 }
100
101
102 void
103 _assuan_release_context (ASSUAN_CONTEXT ctx)
104 {
105 if (ctx)
106 {
107 xfree (ctx->hello_line);
108 xfree (ctx->okay_line);
109 xfree (ctx);
110 }
111 }
112
113 void
114 assuan_deinit_server (ASSUAN_CONTEXT ctx)
115 {
116 if (ctx)
117 {
118 /* We use this function pointer to avoid linking other server
119 when not needed but still allow for a generic deinit function */
120 ctx->deinit_handler (ctx);
121 ctx->deinit_handler = NULL;
122 _assuan_release_context (ctx);
123 }
124 }