1 /* assuan-pipe-server.c - Assuan server working over a pipe
2 * Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 * This file is part of Assuan.
6 * Assuan is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Assuan is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
26 #ifdef HAVE_W32_SYSTEM
31 #include "assuan-defs.h"
35 deinit_pipe_server (assuan_context_t ctx
)
37 /* nothing to do for this simple server */
41 accept_connection (assuan_context_t ctx
)
43 /* This is a NOP for a pipe server */
48 finish_connection (assuan_context_t ctx
)
50 /* This is a NOP for a pipe server */
54 /* Create a new context. Note that the handlers are set up for a pipe
55 server/client - this way we don't need extra dummy functions */
57 _assuan_new_context (assuan_context_t
*r_ctx
)
59 static struct assuan_io io
= { _assuan_simple_read
,
67 ctx
= xtrycalloc (1, sizeof *ctx
);
69 return _assuan_error (ASSUAN_Out_Of_Core
);
70 ctx
->input_fd
= ASSUAN_INVALID_FD
;
71 ctx
->output_fd
= ASSUAN_INVALID_FD
;
73 ctx
->inbound
.fd
= ASSUAN_INVALID_FD
;
74 ctx
->outbound
.fd
= ASSUAN_INVALID_FD
;
77 ctx
->listen_fd
= ASSUAN_INVALID_FD
;
78 /* Use the pipe server handler as a default. */
79 ctx
->deinit_handler
= deinit_pipe_server
;
80 ctx
->accept_handler
= accept_connection
;
81 ctx
->finish_handler
= finish_connection
;
83 rc
= _assuan_register_std_commands (ctx
);
92 /* Returns true if atoi(S) denotes a valid socket. */
93 #ifndef HAVE_W32_SYSTEM
95 is_valid_socket (const char *s
)
99 if ( fstat (atoi (s
), &buf
) )
101 return S_ISSOCK (buf
.st_mode
);
103 #endif /*!HAVE_W32_SYSTEM*/
107 assuan_init_pipe_server (assuan_context_t
*r_ctx
, int filedes
[2])
111 rc
= _assuan_new_context (r_ctx
);
114 assuan_context_t ctx
= *r_ctx
;
119 #ifdef HAVE_W32_SYSTEM
120 /* MS Windows has so many different types of handle that one
121 needs to tranlsate them at many place forth and back. Also
122 make sure that the file descriptors are in binary mode. */
123 setmode (filedes
[0], O_BINARY
);
124 setmode (filedes
[1], O_BINARY
);
125 ctx
->inbound
.fd
= (void*)_get_osfhandle (filedes
[0]);
126 ctx
->outbound
.fd
= (void*)_get_osfhandle (filedes
[1]);
128 s
= getenv ("_assuan_connection_fd");
129 if (s
&& *s
&& is_valid_socket (s
) )
131 /* Well, we are called with an bi-directional file
132 descriptor. Prepare for using sendmsg/recvmsg. In this
133 case we ignore the passed file descriptors. */
134 ctx
->inbound
.fd
= ctx
->outbound
.fd
= atoi (s
);
135 _assuan_init_uds_io (ctx
);
136 ctx
->deinit_handler
= _assuan_uds_deinit
;
138 else if (filedes
&& filedes
[0] != ASSUAN_INVALID_FD
139 && filedes
[1] != ASSUAN_INVALID_FD
)
141 /* Standard pipe server. */
142 ctx
->inbound
.fd
= filedes
[0];
143 ctx
->outbound
.fd
= filedes
[1];
147 _assuan_release_context (*r_ctx
);
149 return ASSUAN_Problem_Starting_Server
;
154 s
= getenv ("_assuan_pipe_connect_pid");
155 if (s
&& (ul
=strtoul (s
, NULL
, 10)) && ul
)
156 ctx
->pid
= (pid_t
)ul
;
158 ctx
->pid
= (pid_t
)-1;
166 _assuan_release_context (assuan_context_t ctx
)
170 _assuan_inquire_release (ctx
);
171 xfree (ctx
->hello_line
);
172 xfree (ctx
->okay_line
);
179 assuan_deinit_server (assuan_context_t ctx
)
183 /* We use this function pointer to avoid linking other server
184 when not needed but still allow for a generic deinit function. */
185 ctx
->deinit_handler (ctx
);
186 ctx
->deinit_handler
= NULL
;
187 _assuan_release_context (ctx
);