forked from wine-mirror/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
named_pipe.c
1412 lines (1231 loc) · 47.8 KB
/
named_pipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Server-side pipe management
*
* Copyright (C) 1998 Alexandre Julliard
* Copyright (C) 2001 Mike McCormack
* Copyright 2016 Jacek Caban for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "winioctl.h"
#include "file.h"
#include "handle.h"
#include "thread.h"
#include "request.h"
#include "security.h"
#include "process.h"
struct named_pipe;
struct pipe_message
{
struct list entry; /* entry in message queue */
data_size_t read_pos; /* already read bytes */
struct iosb *iosb; /* message iosb */
struct async *async; /* async of pending write */
};
struct pipe_end
{
struct object obj; /* object header */
struct fd *fd; /* pipe file descriptor */
unsigned int flags; /* pipe flags */
unsigned int state; /* pipe state */
struct named_pipe *pipe;
struct pipe_end *connection; /* the other end of the pipe */
process_id_t client_pid; /* process that created the client */
process_id_t server_pid; /* process that created the server */
data_size_t buffer_size;/* size of buffered data that doesn't block caller */
struct list message_queue;
struct async_queue read_q; /* read queue */
struct async_queue write_q; /* write queue */
};
struct pipe_server
{
struct pipe_end pipe_end; /* common header for both pipe ends */
struct list entry; /* entry in named pipe listeners list */
unsigned int options; /* pipe options */
struct async_queue listen_q; /* listen queue */
};
struct named_pipe
{
struct object obj; /* object header */
int message_mode;
unsigned int sharing;
unsigned int maxinstances;
unsigned int outsize;
unsigned int insize;
unsigned int instances;
timeout_t timeout;
struct list listeners; /* list of servers listening on this pipe */
struct async_queue waiters; /* list of clients waiting to connect */
};
struct named_pipe_device
{
struct object obj; /* object header */
struct namespace *pipes; /* named pipe namespace */
};
struct named_pipe_device_file
{
struct object obj; /* object header */
struct fd *fd; /* pseudo-fd for ioctls */
struct named_pipe_device *device; /* named pipe device */
};
static void named_pipe_dump( struct object *obj, int verbose );
static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
static void named_pipe_destroy( struct object *obj );
static const struct object_ops named_pipe_ops =
{
sizeof(struct named_pipe), /* size */
named_pipe_dump, /* dump */
no_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
NULL, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
named_pipe_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
named_pipe_link_name, /* link_name */
default_unlink_name, /* unlink_name */
named_pipe_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
named_pipe_destroy /* destroy */
};
/* common server and client pipe end functions */
static void pipe_end_destroy( struct object *obj );
static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
static struct fd *pipe_end_get_fd( struct object *obj );
static struct security_descriptor *pipe_end_get_sd( struct object *obj );
static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
unsigned int set_info );
static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
static int pipe_end_flush( struct fd *fd, struct async *async );
static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class );
static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
/* server end functions */
static void pipe_server_dump( struct object *obj, int verbose );
static void pipe_server_destroy( struct object *obj);
static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static const struct object_ops pipe_server_ops =
{
sizeof(struct pipe_server), /* size */
pipe_server_dump, /* dump */
file_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
default_fd_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
pipe_end_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
pipe_end_get_sd, /* get_sd */
pipe_end_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
fd_close_handle, /* close_handle */
pipe_server_destroy /* destroy */
};
static const struct fd_ops pipe_server_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
pipe_end_get_fd_type, /* get_fd_type */
pipe_end_read, /* read */
pipe_end_write, /* write */
pipe_end_flush, /* flush */
pipe_end_get_file_info, /* get_file_info */
pipe_end_get_volume_info, /* get_volume_info */
pipe_server_ioctl, /* ioctl */
no_fd_queue_async, /* queue_async */
pipe_end_reselect_async /* reselect_async */
};
/* client end functions */
static void pipe_client_dump( struct object *obj, int verbose );
static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static const struct object_ops pipe_client_ops =
{
sizeof(struct pipe_end), /* size */
pipe_client_dump, /* dump */
file_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
default_fd_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
pipe_end_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
pipe_end_get_sd, /* get_sd */
pipe_end_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
fd_close_handle, /* close_handle */
pipe_end_destroy /* destroy */
};
static const struct fd_ops pipe_client_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
pipe_end_get_fd_type, /* get_fd_type */
pipe_end_read, /* read */
pipe_end_write, /* write */
pipe_end_flush, /* flush */
pipe_end_get_file_info, /* get_file_info */
pipe_end_get_volume_info, /* get_volume_info */
pipe_client_ioctl, /* ioctl */
no_fd_queue_async, /* queue_async */
pipe_end_reselect_async /* reselect_async */
};
static void named_pipe_device_dump( struct object *obj, int verbose );
static struct object_type *named_pipe_device_get_type( struct object *obj );
static struct object *named_pipe_device_lookup_name( struct object *obj,
struct unicode_str *name, unsigned int attr );
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options );
static void named_pipe_device_destroy( struct object *obj );
static const struct object_ops named_pipe_device_ops =
{
sizeof(struct named_pipe_device), /* size */
named_pipe_device_dump, /* dump */
named_pipe_device_get_type, /* get_type */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
no_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
named_pipe_device_lookup_name, /* lookup_name */
directory_link_name, /* link_name */
default_unlink_name, /* unlink_name */
named_pipe_device_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
no_close_handle, /* close_handle */
named_pipe_device_destroy /* destroy */
};
static void named_pipe_device_file_dump( struct object *obj, int verbose );
static struct fd *named_pipe_device_file_get_fd( struct object *obj );
static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd );
static void named_pipe_device_file_destroy( struct object *obj );
static const struct object_ops named_pipe_device_file_ops =
{
sizeof(struct named_pipe_device_file), /* size */
named_pipe_device_file_dump, /* dump */
no_get_type, /* get_type */
add_queue, /* add_queue */
remove_queue, /* remove_queue */
default_fd_signaled, /* signaled */
no_satisfied, /* satisfied */
no_signal, /* signal */
named_pipe_device_file_get_fd, /* get_fd */
default_fd_map_access, /* map_access */
default_get_sd, /* get_sd */
default_set_sd, /* set_sd */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
fd_close_handle, /* close_handle */
named_pipe_device_file_destroy /* destroy */
};
static const struct fd_ops named_pipe_device_fd_ops =
{
default_fd_get_poll_events, /* get_poll_events */
default_poll_event, /* poll_event */
named_pipe_device_file_get_fd_type, /* get_fd_type */
no_fd_read, /* read */
no_fd_write, /* write */
no_fd_flush, /* flush */
default_fd_get_file_info, /* get_file_info */
no_fd_get_volume_info, /* get_volume_info */
named_pipe_device_ioctl, /* ioctl */
default_fd_queue_async, /* queue_async */
default_fd_reselect_async /* reselect_async */
};
static void named_pipe_dump( struct object *obj, int verbose )
{
fputs( "Named pipe\n", stderr );
}
static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
{
if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}
static void pipe_server_dump( struct object *obj, int verbose )
{
struct pipe_server *server = (struct pipe_server *) obj;
assert( obj->ops == &pipe_server_ops );
fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
server->pipe_end.state );
}
static void pipe_client_dump( struct object *obj, int verbose )
{
struct pipe_end *client = (struct pipe_end *) obj;
assert( obj->ops == &pipe_client_ops );
fprintf( stderr, "Named pipe client server=%p\n", client->connection );
}
static void named_pipe_destroy( struct object *obj)
{
struct named_pipe *pipe = (struct named_pipe *) obj;
assert( list_empty( &pipe->listeners ) );
assert( !pipe->instances );
free_async_queue( &pipe->waiters );
}
static struct fd *pipe_end_get_fd( struct object *obj )
{
struct pipe_end *pipe_end = (struct pipe_end *) obj;
return (struct fd *) grab_object( pipe_end->fd );
}
static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
{
struct pipe_message *message;
if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
message->iosb = (struct iosb *)grab_object( iosb );
message->async = NULL;
message->read_pos = 0;
list_add_tail( &pipe_end->message_queue, &message->entry );
return message;
}
static void wake_message( struct pipe_message *message )
{
struct async *async = message->async;
message->async = NULL;
if (!async) return;
message->iosb->status = STATUS_SUCCESS;
message->iosb->result = message->iosb->in_size;
async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
release_object( async );
}
static void free_message( struct pipe_message *message )
{
list_remove( &message->entry );
if (message->iosb) release_object( message->iosb );
free( message );
}
static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
{
struct pipe_end *connection = pipe_end->connection;
struct pipe_message *message, *next;
struct async *async;
pipe_end->connection = NULL;
pipe_end->state = status == STATUS_PIPE_DISCONNECTED
? FILE_PIPE_DISCONNECTED_STATE : FILE_PIPE_CLOSING_STATE;
fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
async_wake_up( &pipe_end->read_q, status );
LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
{
async = message->async;
if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
if (!async) continue;
async_terminate( async, status );
release_object( async );
}
if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
if (connection)
{
connection->connection = NULL;
pipe_end_disconnect( connection, status );
}
}
static void pipe_end_destroy( struct object *obj )
{
struct pipe_end *pipe_end = (struct pipe_end *)obj;
struct pipe_message *message;
pipe_end_disconnect( pipe_end, STATUS_PIPE_BROKEN );
while (!list_empty( &pipe_end->message_queue ))
{
message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
assert( !message->async );
free_message( message );
}
free_async_queue( &pipe_end->read_q );
free_async_queue( &pipe_end->write_q );
if (pipe_end->fd) release_object( pipe_end->fd );
if (pipe_end->pipe) release_object( pipe_end->pipe );
}
static void pipe_server_destroy( struct object *obj )
{
struct pipe_server *server = (struct pipe_server *)obj;
struct named_pipe *pipe = server->pipe_end.pipe;
assert( obj->ops == &pipe_server_ops );
assert( pipe->instances );
if (!--pipe->instances) unlink_named_object( &pipe->obj );
if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE)
list_remove( &server->entry );
free_async_queue( &server->listen_q );
pipe_end_destroy( obj );
}
static void named_pipe_device_dump( struct object *obj, int verbose )
{
fputs( "Named pipe device\n", stderr );
}
static struct object_type *named_pipe_device_get_type( struct object *obj )
{
static const WCHAR name[] = {'D','e','v','i','c','e'};
static const struct unicode_str str = { name, sizeof(name) };
return get_object_type( &str );
}
static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
unsigned int attr )
{
struct named_pipe_device *device = (struct named_pipe_device*)obj;
struct object *found;
assert( obj->ops == &named_pipe_device_ops );
assert( device->pipes );
if (!name) return NULL; /* open the device itself */
if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
name->len = 0;
return found;
}
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
unsigned int sharing, unsigned int options )
{
struct named_pipe_device_file *file;
if (!(file = alloc_object( &named_pipe_device_file_ops ))) return NULL;
file->device = (struct named_pipe_device *)grab_object( obj );
if (!(file->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, obj, options )))
{
release_object( file );
return NULL;
}
allow_fd_caching( file->fd );
return &file->obj;
}
static void named_pipe_device_destroy( struct object *obj )
{
struct named_pipe_device *device = (struct named_pipe_device*)obj;
assert( obj->ops == &named_pipe_device_ops );
free( device->pipes );
}
struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
{
struct named_pipe_device *dev;
if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
get_error() != STATUS_OBJECT_NAME_EXISTS)
{
dev->pipes = NULL;
if (!(dev->pipes = create_namespace( 7 )))
{
release_object( dev );
dev = NULL;
}
}
return &dev->obj;
}
static void named_pipe_device_file_dump( struct object *obj, int verbose )
{
struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
fprintf( stderr, "File on named pipe device %p\n", file->device );
}
static struct fd *named_pipe_device_file_get_fd( struct object *obj )
{
struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
return (struct fd *)grab_object( file->fd );
}
static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd )
{
return FD_TYPE_DEVICE;
}
static void named_pipe_device_file_destroy( struct object *obj )
{
struct named_pipe_device_file *file = (struct named_pipe_device_file*)obj;
assert( obj->ops == &named_pipe_device_file_ops );
if (file->fd) release_object( file->fd );
release_object( file->device );
}
static int pipe_end_flush( struct fd *fd, struct async *async )
{
struct pipe_end *pipe_end = get_fd_user( fd );
if (!pipe_end->pipe)
{
set_error( STATUS_PIPE_DISCONNECTED );
return 0;
}
if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
{
fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
set_error( STATUS_PENDING );
}
return 1;
}
static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
{
struct pipe_end *pipe_end = get_fd_user( fd );
struct named_pipe *pipe = pipe_end->pipe;
switch (info_class)
{
case FileNameInformation:
{
FILE_NAME_INFORMATION *name_info;
data_size_t name_len, reply_size;
const WCHAR *name;
if (get_reply_max_size() < sizeof(*name_info))
{
set_error( STATUS_INFO_LENGTH_MISMATCH );
return;
}
/* FIXME: We should be able to return on unlinked pipe */
if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
{
set_error( STATUS_PIPE_DISCONNECTED );
return;
}
reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
if (reply_size > get_reply_max_size())
{
reply_size = get_reply_max_size();
set_error( STATUS_BUFFER_OVERFLOW );
}
if (!(name_info = set_reply_data_size( reply_size ))) return;
name_info->FileNameLength = name_len + sizeof(WCHAR);
name_info->FileName[0] = '\\';
reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
break;
}
case FilePipeInformation:
{
FILE_PIPE_INFORMATION *pipe_info;
if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
{
set_error( STATUS_ACCESS_DENIED );
return;
}
if (get_reply_max_size() < sizeof(*pipe_info))
{
set_error( STATUS_INFO_LENGTH_MISMATCH );
return;
}
if (!pipe)
{
set_error( STATUS_PIPE_DISCONNECTED );
return;
}
if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
pipe_info->ReadMode = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
break;
}
case FilePipeLocalInformation:
{
FILE_PIPE_LOCAL_INFORMATION *pipe_info;
if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
{
set_error( STATUS_ACCESS_DENIED );
return;
}
if (get_reply_max_size() < sizeof(*pipe_info))
{
set_error( STATUS_INFO_LENGTH_MISMATCH );
return;
}
if (!pipe)
{
set_error( STATUS_PIPE_DISCONNECTED );
return;
}
if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
pipe_info->NamedPipeType = pipe->message_mode;
switch (pipe->sharing)
{
case FILE_SHARE_READ:
pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
break;
case FILE_SHARE_WRITE:
pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
break;
case FILE_SHARE_READ | FILE_SHARE_WRITE:
pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
break;
}
pipe_info->MaximumInstances = pipe->maxinstances;
pipe_info->CurrentInstances = pipe->instances;
pipe_info->InboundQuota = pipe->insize;
pipe_info->ReadDataAvailable = 0; /* FIXME */
pipe_info->OutboundQuota = pipe->outsize;
pipe_info->WriteQuotaAvailable = 0; /* FIXME */
pipe_info->NamedPipeState = pipe_end->state;
pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
break;
}
default:
default_fd_get_file_info( fd, handle, info_class );
}
}
static struct security_descriptor *pipe_end_get_sd( struct object *obj )
{
struct pipe_end *pipe_end = (struct pipe_end *) obj;
if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
set_error( STATUS_PIPE_DISCONNECTED );
return NULL;
}
static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
unsigned int set_info )
{
struct pipe_end *pipe_end = (struct pipe_end *) obj;
if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
set_error( STATUS_PIPE_DISCONNECTED );
return 0;
}
static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
{
switch (info_class)
{
case FileFsDeviceInformation:
{
static const FILE_FS_DEVICE_INFORMATION device_info =
{
FILE_DEVICE_NAMED_PIPE,
FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
};
if (get_reply_max_size() >= sizeof(device_info))
set_reply_data( &device_info, sizeof(device_info) );
else
set_error( STATUS_BUFFER_TOO_SMALL );
break;
}
default:
set_error( STATUS_NOT_IMPLEMENTED );
}
}
static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
{
struct pipe_message *message;
if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
{
message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
}
else
{
data_size_t avail = 0;
LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
{
avail += message->iosb->in_size - message->read_pos;
if (avail >= iosb->out_size) break;
}
iosb->out_size = min( iosb->out_size, avail );
iosb->status = STATUS_SUCCESS;
}
message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
{
iosb->out_data = message->iosb->in_data;
message->iosb->in_data = NULL;
wake_message( message );
free_message( message );
}
else
{
data_size_t write_pos = 0, writing;
char *buf = NULL;
if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
{
iosb->out_size = 0;
iosb->status = STATUS_NO_MEMORY;
return;
}
do
{
message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
write_pos += writing;
message->read_pos += writing;
if (message->read_pos == message->iosb->in_size)
{
wake_message(message);
free_message(message);
}
} while (write_pos < iosb->out_size);
}
iosb->result = iosb->out_size;
}
/* We call async_terminate in our reselect implementation, which causes recursive reselect.
* We're not interested in such reselect calls, so we ignore them. */
static int ignore_reselect;
static void reselect_write_queue( struct pipe_end *pipe_end );
static void reselect_read_queue( struct pipe_end *pipe_end )
{
struct async *async;
struct iosb *iosb;
int read_done = 0;
ignore_reselect = 1;
while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
{
iosb = async_get_iosb( async );
message_queue_read( pipe_end, iosb );
async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
release_object( async );
release_object( iosb );
read_done = 1;
}
ignore_reselect = 0;
if (pipe_end->connection)
{
if (list_empty( &pipe_end->message_queue ))
fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
else if (read_done)
reselect_write_queue( pipe_end->connection );
}
}
static void reselect_write_queue( struct pipe_end *pipe_end )
{
struct pipe_message *message, *next;
struct pipe_end *reader = pipe_end->connection;
data_size_t avail = 0;
if (!reader) return;
ignore_reselect = 1;
LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
{
if (message->async && message->iosb->status != STATUS_PENDING)
{
release_object( message->async );
message->async = NULL;
free_message( message );
}
else
{
avail += message->iosb->in_size - message->read_pos;
if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
wake_message( message );
}
}
ignore_reselect = 0;
reselect_read_queue( reader );
}
static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
{
struct pipe_end *pipe_end = get_fd_user( fd );
switch (pipe_end->state)
{
case FILE_PIPE_CONNECTED_STATE:
break;
case FILE_PIPE_DISCONNECTED_STATE:
set_error( STATUS_PIPE_DISCONNECTED );
return 0;
case FILE_PIPE_LISTENING_STATE:
set_error( STATUS_PIPE_LISTENING );
return 0;
case FILE_PIPE_CLOSING_STATE:
if (!list_empty( &pipe_end->message_queue )) break;
set_error( STATUS_PIPE_BROKEN );
return 0;
}
queue_async( &pipe_end->read_q, async );
reselect_read_queue( pipe_end );
set_error( STATUS_PENDING );
return 1;
}
static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
{
struct pipe_end *pipe_end = get_fd_user( fd );
struct pipe_message *message;
struct iosb *iosb;
switch (pipe_end->state)
{
case FILE_PIPE_CONNECTED_STATE:
break;
case FILE_PIPE_DISCONNECTED_STATE:
set_error( STATUS_PIPE_DISCONNECTED );
return 0;
case FILE_PIPE_LISTENING_STATE:
set_error( STATUS_PIPE_LISTENING );
return 0;
case FILE_PIPE_CLOSING_STATE:
set_error( STATUS_PIPE_CLOSING );
return 0;
}
if (!pipe_end->pipe->message_mode && !get_req_data_size()) return 1;
iosb = async_get_iosb( async );
message = queue_message( pipe_end->connection, iosb );
release_object( iosb );
if (!message) return 0;
message->async = (struct async *)grab_object( async );
queue_async( &pipe_end->write_q, async );
reselect_write_queue( pipe_end );
set_error( STATUS_PENDING );
return 1;
}
static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
{
struct pipe_end *pipe_end = get_fd_user( fd );
if (ignore_reselect) return;
if (&pipe_end->write_q == queue)
reselect_write_queue( pipe_end );
else if (&pipe_end->read_q == queue)
reselect_read_queue( pipe_end );
}
static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
{
return FD_TYPE_PIPE;
}
static int pipe_end_peek( struct pipe_end *pipe_end )
{
unsigned reply_size = get_reply_max_size();
FILE_PIPE_PEEK_BUFFER *buffer;
struct pipe_message *message;
data_size_t avail = 0;
data_size_t message_length = 0;
if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
{
set_error( STATUS_INFO_LENGTH_MISMATCH );
return 0;
}
reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
switch (pipe_end->state)
{
case FILE_PIPE_CONNECTED_STATE:
break;
case FILE_PIPE_CLOSING_STATE:
if (!list_empty( &pipe_end->message_queue )) break;
set_error( STATUS_PIPE_BROKEN );
return 0;
default:
set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
return 0;
}
LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
avail += message->iosb->in_size - message->read_pos;
reply_size = min( reply_size, avail );
if (avail && pipe_end->pipe->message_mode)
{
message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
message_length = message->iosb->in_size - message->read_pos;
reply_size = min( reply_size, message_length );
}
if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
buffer->NamedPipeState = pipe_end->state;
buffer->ReadDataAvailable = avail;
buffer->NumberOfMessages = 0; /* FIXME */
buffer->MessageLength = message_length;
if (reply_size)
{
data_size_t write_pos = 0, writing;
LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
{
writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
writing );
write_pos += writing;
if (write_pos == reply_size) break;
}
}
if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
return 1;
}
static int pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
{
struct pipe_message *message;
struct iosb *iosb;
if (!pipe_end->connection)
{
set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
return 0;
}
if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
{
set_error( STATUS_INVALID_READ_MODE );
return 0;