Added TEB in init_thread request.
[wine/multimedia.git] / server / snapshot.c
blobc9e678a819c3cb1ee17188f0dffa59437dfd9de1
1 /*
2 * Server-side snapshots
4 * Copyright (C) 1999 Alexandre Julliard
6 * FIXME: only process snapshots implemented for now
7 */
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
13 #include "winerror.h"
14 #include "winnt.h"
15 #include "tlhelp32.h"
17 #include "handle.h"
18 #include "process.h"
19 #include "thread.h"
22 struct snapshot
24 struct object obj; /* object header */
25 struct process_snapshot *process; /* processes snapshot */
26 int process_count; /* count of processes */
27 int process_pos; /* current position in proc snapshot */
30 static void snapshot_dump( struct object *obj, int verbose );
31 static void snapshot_destroy( struct object *obj );
33 static const struct object_ops snapshot_ops =
35 snapshot_dump,
36 no_add_queue,
37 NULL, /* should never get called */
38 NULL, /* should never get called */
39 NULL, /* should never get called */
40 no_read_fd,
41 no_write_fd,
42 no_flush,
43 no_get_file_info,
44 snapshot_destroy
48 /* create a new snapshot */
49 static struct object *create_snapshot( int flags )
51 struct snapshot *snapshot;
52 if (!(snapshot = mem_alloc( sizeof(*snapshot) ))) return NULL;
53 init_object( &snapshot->obj, &snapshot_ops, NULL );
54 if (flags & TH32CS_SNAPPROCESS)
55 snapshot->process = process_snap( &snapshot->process_count );
56 else
57 snapshot->process_count = 0;
59 snapshot->process_pos = 0;
60 return &snapshot->obj;
63 /* get the next process in the snapshot */
64 static int snapshot_next_process( int handle, int reset, struct next_process_reply *reply )
66 struct snapshot *snapshot;
67 struct process_snapshot *ptr;
68 if (!(snapshot = (struct snapshot *)get_handle_obj( current->process, handle,
69 0, &snapshot_ops )))
70 return 0;
71 if (!snapshot->process_count)
73 SET_ERROR( ERROR_INVALID_PARAMETER ); /* FIXME */
74 release_object( snapshot );
75 return 0;
77 if (reset) snapshot->process_pos = 0;
78 else if (snapshot->process_pos >= snapshot->process_count)
80 SET_ERROR( ERROR_NO_MORE_FILES );
81 release_object( snapshot );
82 return 0;
84 ptr = &snapshot->process[snapshot->process_pos++];
85 reply->pid = ptr->process;
86 reply->threads = ptr->threads;
87 reply->priority = ptr->priority;
88 release_object( snapshot );
89 return 1;
92 static void snapshot_dump( struct object *obj, int verbose )
94 struct snapshot *snapshot = (struct snapshot *)obj;
95 assert( obj->ops == &snapshot_ops );
96 fprintf( stderr, "Snapshot: %d processes\n",
97 snapshot->process_count );
100 static void snapshot_destroy( struct object *obj )
102 int i;
103 struct snapshot *snapshot = (struct snapshot *)obj;
104 assert( obj->ops == &snapshot_ops );
105 if (snapshot->process_count)
107 for (i = 0; i < snapshot->process_count; i++)
108 release_object( snapshot->process[i].process );
109 free( snapshot->process );
111 free( snapshot );
114 /* create a snapshot */
115 DECL_HANDLER(create_snapshot)
117 struct object *obj;
118 struct create_snapshot_reply reply = { -1 };
120 if ((obj = create_snapshot( req->flags )))
122 reply.handle = alloc_handle( current->process, obj, 0, req->inherit );
123 release_object( obj );
125 send_reply( current, -1, 1, &reply, sizeof(reply) );
128 /* get the next process from a snapshot */
129 DECL_HANDLER(next_process)
131 struct next_process_reply reply;
132 snapshot_next_process( req->handle, req->reset, &reply );
133 send_reply( current, -1, 1, &reply, sizeof(reply) );