Fix Windows port (spawn and assuan engine).
[gpgme.git] / src / priv-io.h
1 /* priv-io.h - Interface to the private I/O functions.
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH
4
5    This file is part of GPGME.
6  
7    GPGME is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11    
12    GPGME is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16    
17    You should have received a copy of the GNU Lesser General Public
18    License along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #ifndef IO_H
23 #define IO_H
24
25 #ifdef HAVE_W32_SYSTEM
26 # ifdef HAVE_W32CE_SYSTEM
27 #  include "w32-ce.h"
28 # endif
29 # include <windows.h>
30 #else
31 # include <sys/socket.h>
32 #endif
33
34 /* For pid_t.  */
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38
39
40 /* A single file descriptor passed to spawn.  For child fds, dup_to
41    specifies the fd it should become in the child, but only 0, 1 and 2
42    are valid values (due to a limitation in the W32 code).  As return
43    value, the PEER_NAME fields specify the name of the file
44    descriptor in the spawned process, or -1 if no change.  If ARG_LOC
45    is not 0, it specifies the index in the argument vector of the
46    program which contains a numerical representation of the file
47    descriptor for translation purposes.  */
48 struct spawn_fd_item_s
49 {
50   int fd;
51   int dup_to;
52   int peer_name;
53   int arg_loc;
54 };
55
56 struct io_select_fd_s
57 {
58   int fd;
59   int for_read;
60   int for_write;
61   int signaled;
62   void *opaque;
63 };
64
65 /* These function are either defined in posix-io.c or w32-io.c.  */
66 void _gpgme_io_subsystem_init (void);
67 int _gpgme_io_socket (int namespace, int style, int protocol);
68 int _gpgme_io_connect (int fd, struct sockaddr *addr, int addrlen);
69 int _gpgme_io_read (int fd, void *buffer, size_t count);
70 int _gpgme_io_write (int fd, const void *buffer, size_t count);
71 int _gpgme_io_pipe (int filedes[2], int inherit_idx);
72 int _gpgme_io_close (int fd);
73 typedef void (*_gpgme_close_notify_handler_t) (int,void*);
74 int _gpgme_io_set_close_notify (int fd, _gpgme_close_notify_handler_t handler,
75                                 void *value);
76 int _gpgme_io_set_nonblocking (int fd);
77
78 /* A flag to tell the spawn function to allow the child process to set
79    the foreground window. */
80 #define IOSPAWN_FLAG_ALLOW_SET_FG 1
81 /* Don't close any child FDs.  */
82 #define IOSPAWN_FLAG_NOCLOSE 2
83
84 /* Spawn the executable PATH with ARGV as arguments.  After forking
85    close all fds except for those in FD_LIST in the child, then
86    optionally dup() the child fds.  Finally, all fds in the list are
87    closed in the parent.  */
88 int _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags,
89                      struct spawn_fd_item_s *fd_list,
90                      void (*atfork) (void *opaque, int reserved),
91                      void *atforkvalue, pid_t *r_pid);
92
93 int _gpgme_io_select (struct io_select_fd_s *fds, size_t nfds, int nonblock);
94
95 /* Write the printable version of FD to the buffer BUF of length
96    BUFLEN.  The printable version is the representation on the command
97    line that the child process expects.  */
98 int _gpgme_io_fd2str (char *buf, int buflen, int fd);
99
100 /* Duplicate a file descriptor.  This is more restrictive than dup():
101    it assumes that the resulting file descriptors are essentially
102    co-equal (for example, no private offset), which is true for pipes
103    and sockets (but not files) under Unix with the standard dup()
104    function.  Basically, this function is used to reference count the
105    status output file descriptor shared between GPGME and libassuan
106    (in engine-gpgsm.c).  */
107 int _gpgme_io_dup (int fd);
108
109 #ifndef HAVE_W32_SYSTEM
110 int _gpgme_io_recvmsg (int fd, struct msghdr *msg, int flags);
111 int _gpgme_io_sendmsg (int fd, const struct msghdr *msg, int flags);
112 int _gpgme_io_waitpid (int pid, int hang, int *r_status, int *r_signal);
113 #endif
114
115 #endif /* IO_H */