Added assuan_socket_connect_fd() and assuan_set_finish_handler().
[libpwmd.git] / assuan / assuan-connect.c
blob026888c975533bd3ae4c9886aa71aea9c94b88da
1 /* assuan-connect.c - Establish a connection (client)
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/>.
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <signal.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #ifndef HAVE_W32_SYSTEM
32 #include <sys/wait.h>
33 #endif
35 #include "assuan-defs.h"
37 /* Disconnect and release the context CTX. */
38 void
39 assuan_disconnect (assuan_context_t ctx)
41 if (ctx)
43 assuan_write_line (ctx, "BYE");
44 if (ctx->user_finish_handler)
45 ctx->user_finish_handler(ctx);
46 ctx->finish_handler (ctx);
47 ctx->deinit_handler (ctx);
48 ctx->deinit_handler = NULL;
49 _assuan_release_context (ctx);
53 /* Return the PID of the peer or -1 if not known. This function works
54 in some situations where assuan_get_ucred fails. */
55 pid_t
56 assuan_get_pid (assuan_context_t ctx)
58 return (ctx && ctx->pid)? ctx->pid : -1;
62 #ifndef HAVE_W32_SYSTEM
63 /* Return user credentials. PID, UID and GID amy be gived as NULL if
64 you are not interested in this value. For getting the pid of the
65 peer the assuan_get_pid is usually better suited. */
66 assuan_error_t
67 assuan_get_peercred (assuan_context_t ctx, pid_t *pid, uid_t *uid, gid_t *gid)
69 if (!ctx)
70 return _assuan_error (ASSUAN_Invalid_Value);
71 if (!ctx->peercred.valid)
72 return _assuan_error (ASSUAN_General_Error);
74 #ifdef HAVE_SO_PEERCRED
75 if (pid)
76 *pid = ctx->peercred.pid;
77 if (uid)
78 *uid = ctx->peercred.uid;
79 if (gid)
80 *gid = ctx->peercred.gid;
81 #endif
83 return 0;
85 #endif /* HAVE_W32_SYSTEM */