~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to assuan/assuan-socket-connect.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* assuan-socket-connect.c - Assuan socket based client
2
 
 *      Copyright (C) 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
17
 
 * License along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 
19
 
 */
20
 
 
21
 
#include <config.h>
22
 
#include <stdlib.h>
23
 
#include <stddef.h>
24
 
#include <stdio.h>
25
 
#include <errno.h>
26
 
#include <sys/socket.h>
27
 
#include <sys/un.h>
28
 
#include <unistd.h>
29
 
 
30
 
#include "assuan-defs.h"
31
 
 
32
 
#ifdef HAVE_JNLIB_LOGGING
33
 
#include "../jnlib/logging.h"
34
 
#define LOGERROR(a)      log_error ((a))
35
 
#define LOGERROR1(a,b)   log_error ((a), (b))
36
 
#define LOGERROR2(a,b,c) log_error ((a), (b), (c))
37
 
#define LOGERRORX(a)     log_printf ((a))
38
 
#else
39
 
#define LOGERROR(a)      fprintf (stderr, (a))
40
 
#define LOGERROR1(a,b)   fprintf (stderr, (a), (b))
41
 
#define LOGERROR2(a,b,c) fprintf (stderr, (a), (b), (c))
42
 
#define LOGERRORX(a)     fputs ((a), stderr)
43
 
#endif
44
 
 
45
 
 
46
 
 
47
 
static int
48
 
do_finish (ASSUAN_CONTEXT ctx)
49
 
{
50
 
  if (ctx->inbound.fd != -1)
51
 
    {
52
 
      close (ctx->inbound.fd);
53
 
    }
54
 
  ctx->inbound.fd = -1;
55
 
  ctx->outbound.fd = -1;
56
 
  return 0;
57
 
}
58
 
 
59
 
static void
60
 
do_deinit (ASSUAN_CONTEXT ctx)
61
 
{
62
 
  do_finish (ctx);
63
 
}
64
 
 
65
 
 
66
 
 
67
 
/* Make a connection to the Unix domain socket NAME and return a new
68
 
   Assuan context in CTX.  SERVER_PID is currently not used but may
69
 
   becode handy in future. */
70
 
AssuanError
71
 
assuan_socket_connect (ASSUAN_CONTEXT *r_ctx,
72
 
                       const char *name, pid_t server_pid)
73
 
{
74
 
  AssuanError err;
75
 
  ASSUAN_CONTEXT ctx;
76
 
  int fd;
77
 
  struct sockaddr_un srvr_addr;
78
 
  size_t len;
79
 
 
80
 
  if (!r_ctx || !name)
81
 
    return ASSUAN_Invalid_Value;
82
 
  *r_ctx = NULL;
83
 
 
84
 
  /* we require that the name starts with a slash, so that we can
85
 
     alter reuse this function for other socket types */
86
 
  if (*name != '/')
87
 
    return ASSUAN_Invalid_Value;
88
 
  if (strlen (name)+1 >= sizeof srvr_addr.sun_path)
89
 
    return ASSUAN_Invalid_Value;
90
 
 
91
 
  err = _assuan_new_context (&ctx); 
92
 
  if (err)
93
 
      return err;
94
 
  ctx->pid = server_pid; /* save it in case we need it later */
95
 
  ctx->deinit_handler = do_deinit;
96
 
  ctx->finish_handler = do_finish;
97
 
 
98
 
  fd = socket (AF_UNIX, SOCK_STREAM, 0);
99
 
  if (fd == -1)
100
 
    {
101
 
      LOGERROR1 ("can't create socket: %s\n", strerror (errno));
102
 
      _assuan_release_context (ctx);
103
 
      return ASSUAN_General_Error;
104
 
    }
105
 
    
106
 
  memset (&srvr_addr, 0, sizeof srvr_addr );
107
 
  srvr_addr.sun_family = AF_UNIX;
108
 
  strcpy (srvr_addr.sun_path, name);
109
 
  len = (offsetof (struct sockaddr_un, sun_path)
110
 
         + strlen (srvr_addr.sun_path) + 1);
111
 
    
112
 
  if (connect (fd, (struct sockaddr*)&srvr_addr, len) == -1)
113
 
    {
114
 
      LOGERROR2 ("can't connect to `%s': %s\n", name, strerror (errno));
115
 
      _assuan_release_context (ctx);
116
 
      close (fd );
117
 
      return ASSUAN_Connect_Failed;
118
 
    }
119
 
 
120
 
  ctx->inbound.fd = fd;
121
 
  ctx->outbound.fd = fd;
122
 
 
123
 
  /* initial handshake */
124
 
  {
125
 
    int okay, off;
126
 
 
127
 
    err = _assuan_read_from_server (ctx, &okay, &off);
128
 
    if (err)
129
 
      {
130
 
        LOGERROR1 ("can't connect server: %s\n", assuan_strerror (err));
131
 
      }
132
 
    else if (okay != 1)
133
 
      {
134
 
        LOGERROR ("can't connect server: `");
135
 
        _assuan_log_sanitized_string (ctx->inbound.line);
136
 
        LOGERRORX ("'\n");
137
 
        err = ASSUAN_Connect_Failed;
138
 
      }
139
 
  }
140
 
 
141
 
  if (err)
142
 
    {
143
 
      assuan_disconnect (ctx); 
144
 
    }
145
 
  else
146
 
    *r_ctx = ctx;
147
 
  return 0;
148
 
}
149
 
 
150