server: Added infrastructure for access rights mapping.
[wine] / server / serial.c
1 /*
2  * Server-side serial port communications management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2000,2001 Mike McCormack
6  *
7  *
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.
12  *
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.
17  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
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
48
49 #include "ntstatus.h"
50 #define WIN32_NO_STATUS
51 #include "windef.h"
52 #include "winternl.h"
53
54 #include "file.h"
55 #include "handle.h"
56 #include "thread.h"
57 #include "request.h"
58
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);
62
63 static int serial_get_poll_events( struct fd *fd );
64 static void serial_poll_event( struct fd *fd, int event );
65 static int serial_get_info( struct fd *fd );
66 static int serial_flush( struct fd *fd, struct event **event );
67 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb, int type, int count );
68 static void serial_cancel_async( struct fd *fd );
69
70 struct serial
71 {
72     struct object       obj;
73     struct fd          *fd;
74     unsigned int        options;
75
76     /* timeout values */
77     unsigned int        readinterval;
78     unsigned int        readconst;
79     unsigned int        readmult;
80     unsigned int        writeconst;
81     unsigned int        writemult;
82
83     unsigned int        eventmask;
84     unsigned int        commerror;
85
86     struct termios      original;
87
88     struct list         read_q;
89     struct list         write_q;
90     struct list         wait_q;
91
92     /* FIXME: add dcb, comm status, handler module, sharing */
93 };
94
95 static const struct object_ops serial_ops =
96 {
97     sizeof(struct serial),        /* size */
98     serial_dump,                  /* dump */
99     default_fd_add_queue,         /* add_queue */
100     default_fd_remove_queue,      /* remove_queue */
101     default_fd_signaled,          /* signaled */
102     no_satisfied,                 /* satisfied */
103     no_signal,                    /* signal */
104     serial_get_fd,                /* get_fd */
105     no_map_access,                /* map_access */
106     no_lookup_name,               /* lookup_name */
107     no_close_handle,              /* close_handle */
108     serial_destroy                /* destroy */
109 };
110
111 static const struct fd_ops serial_fd_ops =
112 {
113     serial_get_poll_events,       /* get_poll_events */
114     serial_poll_event,            /* poll_event */
115     serial_flush,                 /* flush */
116     serial_get_info,              /* get_file_info */
117     serial_queue_async,           /* queue_async */
118     serial_cancel_async           /* cancel_async */
119 };
120
121 /* check if the given fd is a serial port */
122 int is_serial_fd( struct fd *fd )
123 {
124     struct termios tios;
125
126     return !tcgetattr( get_unix_fd(fd), &tios );
127 }
128
129 /* create a serial object for a given fd */
130 struct object *create_serial( struct fd *fd, unsigned int options )
131 {
132     struct serial *serial;
133     int unix_fd;
134
135     if ((unix_fd = dup( get_unix_fd(fd) )) == -1) return NULL;
136
137     /* set the fd back to blocking if necessary */
138     if (options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
139         fcntl( unix_fd, F_SETFL, 0 );
140
141     if (!(serial = alloc_object( &serial_ops )))
142     {
143         close( unix_fd );
144         return NULL;
145     }
146     serial->options      = options;
147     serial->readinterval = 0;
148     serial->readmult     = 0;
149     serial->readconst    = 0;
150     serial->writemult    = 0;
151     serial->writeconst   = 0;
152     serial->eventmask    = 0;
153     serial->commerror    = 0;
154     list_init( &serial->read_q );
155     list_init( &serial->write_q );
156     list_init( &serial->wait_q );
157     if (!(serial->fd = create_anonymous_fd( &serial_fd_ops, unix_fd, &serial->obj )))
158     {
159         release_object( serial );
160         return NULL;
161     }
162     return &serial->obj;
163 }
164
165 static struct fd *serial_get_fd( struct object *obj )
166 {
167     struct serial *serial = (struct serial *)obj;
168     return (struct fd *)grab_object( serial->fd );
169 }
170
171 static void serial_destroy( struct object *obj)
172 {
173     struct serial *serial = (struct serial *)obj;
174
175     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
176     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
177     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
178     if (serial->fd) release_object( serial->fd );
179 }
180
181 static void serial_dump( struct object *obj, int verbose )
182 {
183     struct serial *serial = (struct serial *)obj;
184     assert( obj->ops == &serial_ops );
185     fprintf( stderr, "Port fd=%p mask=%x\n", serial->fd, serial->eventmask );
186 }
187
188 static struct serial *get_serial_obj( struct process *process, obj_handle_t handle, unsigned int access )
189 {
190     return (struct serial *)get_handle_obj( process, handle, access, &serial_ops );
191 }
192
193 static int serial_get_poll_events( struct fd *fd )
194 {
195     struct serial *serial = get_fd_user( fd );
196     int events = 0;
197     assert( serial->obj.ops == &serial_ops );
198
199     if (!list_empty( &serial->read_q ))  events |= POLLIN;
200     if (!list_empty( &serial->write_q )) events |= POLLOUT;
201     if (!list_empty( &serial->wait_q ))  events |= POLLIN;
202
203     /* fprintf(stderr,"poll events are %04x\n",events); */
204
205     return events;
206 }
207
208 static int serial_get_info( struct fd *fd )
209 {
210     int flags = 0;
211     struct serial *serial = get_fd_user( fd );
212     assert( serial->obj.ops == &serial_ops );
213
214     if (!(serial->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))
215         flags |= FD_FLAG_OVERLAPPED;
216     else if (!(serial->readinterval == MAXDWORD &&
217                serial->readmult == 0 && serial->readconst == 0))
218         flags |= FD_FLAG_TIMEOUT;
219     if (serial->readinterval == MAXDWORD &&
220         serial->readmult == 0 && serial->readconst == 0)
221         flags |= FD_FLAG_AVAILABLE;
222
223     return flags;
224 }
225
226 static void serial_poll_event(struct fd *fd, int event)
227 {
228     struct serial *serial = get_fd_user( fd );
229
230     /* fprintf(stderr,"Poll event %02x\n",event); */
231
232     if (!list_empty( &serial->read_q ) && (POLLIN & event) )
233         async_terminate_head( &serial->read_q, STATUS_ALERTED );
234
235     if (!list_empty( &serial->write_q ) && (POLLOUT & event) )
236         async_terminate_head( &serial->write_q, STATUS_ALERTED );
237
238     if (!list_empty( &serial->wait_q ) && (POLLIN & event) )
239         async_terminate_head( &serial->wait_q, STATUS_ALERTED );
240
241     set_fd_events( fd, serial_get_poll_events(fd) );
242 }
243
244 static void serial_queue_async( struct fd *fd, void *apc, void *user, void *iosb,
245                                 int type, int count )
246 {
247     struct serial *serial = get_fd_user( fd );
248     struct list *queue;
249     int timeout;
250     int events;
251
252     assert(serial->obj.ops == &serial_ops);
253
254     switch (type)
255     {
256     case ASYNC_TYPE_READ:
257         queue = &serial->read_q;
258         timeout = serial->readconst + serial->readmult*count;
259         break;
260     case ASYNC_TYPE_WAIT:
261         queue = &serial->wait_q;
262         timeout = 0;
263         break;
264     case ASYNC_TYPE_WRITE:
265         queue = &serial->write_q;
266         timeout = serial->writeconst + serial->writemult*count;
267         break;
268     default:
269         set_error(STATUS_INVALID_PARAMETER);
270         return;
271     }
272
273     if (!create_async( current, &timeout, queue, apc, user, iosb ))
274         return;
275
276     /* Check if the new pending request can be served immediately */
277     events = check_fd_events( fd, serial_get_poll_events( fd ) );
278     if (events)
279     {
280         /* serial_poll_event() calls set_select_events() */
281         serial_poll_event( fd, events );
282         return;
283     }
284
285     set_fd_events( fd, serial_get_poll_events( fd ) );
286 }
287
288 static void serial_cancel_async( struct fd *fd )
289 {
290     struct serial *serial = get_fd_user( fd );
291     assert(serial->obj.ops == &serial_ops);
292
293     async_terminate_queue( &serial->read_q, STATUS_CANCELLED );
294     async_terminate_queue( &serial->write_q, STATUS_CANCELLED );
295     async_terminate_queue( &serial->wait_q, STATUS_CANCELLED );
296 }
297
298 static int serial_flush( struct fd *fd, struct event **event )
299 {
300     /* MSDN says: If hFile is a handle to a communications device,
301      * the function only flushes the transmit buffer.
302      */
303     int ret = (tcflush( get_unix_fd(fd), TCOFLUSH ) != -1);
304     if (!ret) file_set_error();
305     return ret;
306 }
307
308 DECL_HANDLER(get_serial_info)
309 {
310     struct serial *serial;
311
312     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
313     {
314         /* timeouts */
315         reply->readinterval = serial->readinterval;
316         reply->readconst    = serial->readconst;
317         reply->readmult     = serial->readmult;
318         reply->writeconst   = serial->writeconst;
319         reply->writemult    = serial->writemult;
320
321         /* event mask */
322         reply->eventmask    = serial->eventmask;
323
324         /* comm port error status */
325         reply->commerror    = serial->commerror;
326
327         release_object( serial );
328     }
329 }
330
331 DECL_HANDLER(set_serial_info)
332 {
333     struct serial *serial;
334
335     if ((serial = get_serial_obj( current->process, req->handle, 0 )))
336     {
337         /* timeouts */
338         if (req->flags & SERIALINFO_SET_TIMEOUTS)
339         {
340             serial->readinterval = req->readinterval;
341             serial->readconst    = req->readconst;
342             serial->readmult     = req->readmult;
343             serial->writeconst   = req->writeconst;
344             serial->writemult    = req->writemult;
345         }
346
347         /* event mask */
348         if (req->flags & SERIALINFO_SET_MASK)
349         {
350             serial->eventmask = req->eventmask;
351             if (!serial->eventmask)
352             {
353                 async_terminate_queue( &serial->wait_q, STATUS_SUCCESS );
354             }
355         }
356
357         /* comm port error status */
358         if (req->flags & SERIALINFO_SET_ERROR)
359         {
360             serial->commerror = req->commerror;
361         }
362
363         release_object( serial );
364     }
365 }