1 /* assuan-connect.c - Establish a connection (client)
2  *	Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3  *
4  * This file is part of Assuan.
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /* Please note that this is a stripped down and modified version of
21    the orginal Assuan code from libassuan. */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #ifndef HAVE_W32_SYSTEM
35 #include <sys/wait.h>
36 #endif
37 
38 #include "assuan-defs.h"
39 
40 /* Create a new context.  */
41 int
_assuan_new_context(assuan_context_t * r_ctx)42 _assuan_new_context (assuan_context_t *r_ctx)
43 {
44   assuan_context_t ctx;
45 
46   *r_ctx = NULL;
47   ctx = xcalloc (1, sizeof *ctx);
48 
49   ctx->input_fd = -1;
50   ctx->output_fd = -1;
51 
52   ctx->inbound.fd = -1;
53   ctx->outbound.fd = -1;
54   ctx->io = NULL;
55 
56   ctx->listen_fd = -1;
57   *r_ctx = ctx;
58   return 0;
59 }
60 
61 
62 void
_assuan_release_context(assuan_context_t ctx)63 _assuan_release_context (assuan_context_t ctx)
64 {
65   if (ctx)
66     {
67       xfree (ctx->hello_line);
68       xfree (ctx->okay_line);
69       xfree (ctx);
70     }
71 }
72 
73 
74 /* Disconnect and release the context CTX. */
75 void
assuan_disconnect(assuan_context_t ctx)76 assuan_disconnect (assuan_context_t ctx)
77 {
78   if (ctx)
79     {
80       assuan_write_line (ctx, "BYE");
81       ctx->finish_handler (ctx);
82       ctx->deinit_handler (ctx);
83       ctx->deinit_handler = NULL;
84       _assuan_release_context (ctx);
85     }
86 }
87 
88 /* Return the PID of the peer or -1 if not known. */
89 pid_t
assuan_get_pid(assuan_context_t ctx)90 assuan_get_pid (assuan_context_t ctx)
91 {
92   return (ctx && ctx->pid)? ctx->pid : -1;
93 }
94 
95