schedsvc: Fix xml buffer leak (Valgrind).
[wine.git] / server / serial.c
blobfea3c8c9506da786ef45e38195b9f0b50cf6e279
1 /*
2 * Server-side serial port communications management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2000,2001 Mike McCormack
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <time.h>
35 #include <unistd.h>
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39 #ifdef HAVE_TERMIOS_H
40 #include <termios.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_POLL_H
46 #include <poll.h>
47 #endif
49 #include "ntstatus.h"
50 #define WIN32_NO_STATUS
51 #include "windef.h"
52 #include "winternl.h"
54 #include "file.h"
55 #include "handle.h"
56 #include "thread.h"
57 #include "request.h"
59 static void serial_dump( struct object *obj, int verbose );
60 static struct fd *serial_get_fd( struct object *obj );
61 static void serial_destroy(struct object *obj);
63 static enum server_fd_type serial_get_fd_type( struct fd *fd );
64 static void serial_queue_async( struct fd *fd, struct async *async, int type, int count );
65 static void serial_reselect_async( struct fd *fd, struct async_queue *queue );
67 struct serial
69 struct object obj;
70 struct fd *fd;
72 struct timeout_user *read_timer;
74 /* timeout values */
75 unsigned int readinterval;
76 unsigned int readconst;
77 unsigned int readmult;
78 unsigned int writeconst;
79 unsigned int writemult;
81 unsigned int eventmask;
82 unsigned int generation; /* event mask change counter */
83 unsigned int pending_write : 1;
84 unsigned int pending_wait : 1;
86 struct termios original;
88 /* FIXME: add dcb, comm status, handler module, sharing */
91 static const struct object_ops serial_ops =
93 sizeof(struct serial), /* size */
94 serial_dump, /* dump */
95 no_get_type, /* get_type */
96 add_queue, /* add_queue */
97 remove_queue, /* remove_queue */
98 default_fd_signaled, /* signaled */
99 no_satisfied, /* satisfied */
100 no_signal, /* signal */
101 serial_get_fd, /* get_fd */
102 default_fd_map_access, /* map_access */
103 default_get_sd, /* get_sd */
104 default_set_sd, /* set_sd */
105 no_lookup_name, /* lookup_name */
106 no_link_name, /* link_name */
107 NULL, /* unlink_name */
108 no_open_file, /* open_file */
109 fd_close_handle, /* close_handle */
110 serial_destroy /* destroy */
113 static const struct fd_ops serial_fd_ops =
115 default_fd_get_poll_events, /* get_poll_events */
116 default_poll_event, /* poll_event */
117 serial_get_fd_type, /* get_fd_type */
118 no_fd_read, /* read */
119 no_fd_write, /* write */
120 no_fd_flush, /* flush */
121 default_fd_ioctl, /* ioctl */
122 serial_queue_async, /* queue_async */
123 serial_reselect_async /* reselect_async */
126 /* check if the given fd is a serial port */
127 int is_serial_fd( struct fd *fd )
129 struct termios tios;
131 return !tcgetattr( get_unix_fd(fd), &tios );
134 /* create a serial object for a given fd */
135 struct object *create_serial( struct fd *fd )
137 struct serial *serial;
139 if (!(serial = alloc_object( &serial_ops ))) return NULL;
141 serial->read_timer = NULL;
142 serial->readinterval = 0;
143 serial->readmult = 0;
144 serial->readconst = 0;
145 serial->writemult = 0;
146 serial->writeconst = 0;
147 serial->eventmask = 0;
148 serial->generation = 0;
149 serial->pending_write = 0;
150 serial->pending_wait = 0;
151 serial->fd = (struct fd *)grab_object( fd );
152 set_fd_user( fd, &serial_fd_ops, &serial->obj );
153 return &serial->obj;
156 static struct fd *serial_get_fd( struct object *obj )
158 struct serial *serial = (struct serial *)obj;
159 return (struct fd *)grab_object( serial->fd );
162 static void serial_destroy( struct object *obj)
164 struct serial *serial = (struct serial *)obj;
165 if (serial->read_timer) remove_timeout_user( serial->read_timer );
166 release_object( serial->fd );
169 static void serial_dump( struct object *obj, int verbose )
171 struct serial *serial = (struct serial *)obj;
172 assert( obj->ops == &serial_ops );
173 fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
176 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
178 return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
181 static enum server_fd_type serial_get_fd_type( struct fd *fd )
183 return FD_TYPE_SERIAL;
186 static void serial_queue_async( struct fd *fd, struct async *async, int type, int count )
188 struct serial *serial = get_fd_user( fd );
189 timeout_t timeout = 0;
191 assert(serial->obj.ops == &serial_ops);
193 switch (type)
195 case ASYNC_TYPE_READ:
196 timeout = serial->readconst + (timeout_t)serial->readmult*count;
197 break;
198 case ASYNC_TYPE_WRITE:
199 timeout = serial->writeconst + (timeout_t)serial->writemult*count;
200 break;
203 if (fd_queue_async( fd, async, type ))
205 if (timeout) async_set_timeout( async, timeout * -10000, STATUS_TIMEOUT );
206 set_error( STATUS_PENDING );
210 static void serial_read_timeout( void *arg )
212 struct serial *serial = arg;
214 serial->read_timer = NULL;
215 fd_async_wake_up( serial->fd, ASYNC_TYPE_READ, STATUS_TIMEOUT );
218 static void serial_reselect_async( struct fd *fd, struct async_queue *queue )
220 struct serial *serial = get_fd_user( fd );
222 if (serial->read_timer)
224 if (!(default_fd_get_poll_events( fd ) & POLLIN))
226 remove_timeout_user( serial->read_timer );
227 serial->read_timer = NULL;
230 else if (serial->readinterval && (default_fd_get_poll_events( fd ) & POLLIN))
232 serial->read_timer = add_timeout_user( (timeout_t)serial->readinterval * -10000,
233 serial_read_timeout, serial );
235 default_fd_reselect_async( fd, queue );
238 DECL_HANDLER(get_serial_info)
240 struct serial *serial;
242 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
244 if (req->flags & SERIALINFO_PENDING_WAIT)
246 if (serial->pending_wait)
248 release_object( serial );
249 set_error( STATUS_INVALID_PARAMETER );
250 return;
252 serial->pending_wait = 1;
255 /* timeouts */
256 reply->readinterval = serial->readinterval;
257 reply->readconst = serial->readconst;
258 reply->readmult = serial->readmult;
259 reply->writeconst = serial->writeconst;
260 reply->writemult = serial->writemult;
262 /* event mask */
263 reply->eventmask = serial->eventmask;
264 reply->cookie = serial->generation;
266 /* pending write */
267 reply->pending_write = serial->pending_write;
268 if (req->flags & SERIALINFO_PENDING_WRITE)
269 serial->pending_write = 0;
271 release_object( serial );
275 DECL_HANDLER(set_serial_info)
277 struct serial *serial;
279 if ((serial = get_serial_obj( current->process, req->handle, 0 )))
281 if (req->flags & SERIALINFO_PENDING_WAIT)
283 if (!serial->pending_wait)
285 release_object( serial );
286 set_error( STATUS_INVALID_PARAMETER );
287 return;
289 serial->pending_wait = 0;
292 /* timeouts */
293 if (req->flags & SERIALINFO_SET_TIMEOUTS)
295 serial->readinterval = req->readinterval;
296 serial->readconst = req->readconst;
297 serial->readmult = req->readmult;
298 serial->writeconst = req->writeconst;
299 serial->writemult = req->writemult;
302 /* pending write */
303 if (req->flags & SERIALINFO_PENDING_WRITE)
304 serial->pending_write = 1;
306 /* event mask */
307 if (req->flags & SERIALINFO_SET_MASK)
309 serial->eventmask = req->eventmask;
310 serial->generation++;
311 fd_async_wake_up( serial->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
314 release_object( serial );