Only include 'sys/user.h' for Linux. Fixes a compilation error on
[wine] / server / snapshot.c
1 /*
2  * Server-side snapshots
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * FIXME: heap snapshots not implemented
7  */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "windef.h"
14 #include "tlhelp32.h"
15
16 #include "handle.h"
17 #include "process.h"
18 #include "thread.h"
19 #include "request.h"
20
21
22 struct snapshot
23 {
24     struct object             obj;           /* object header */
25     struct process           *process;       /* process of this snapshot (for modules and heaps) */
26     struct process_snapshot  *processes;     /* processes snapshot */
27     int                       process_count; /* count of processes */
28     int                       process_pos;   /* current position in proc snapshot */
29     struct thread_snapshot   *threads;       /* threads snapshot */
30     int                       thread_count;  /* count of threads */
31     int                       thread_pos;    /* current position in thread snapshot */
32     struct module_snapshot   *modules;       /* modules snapshot */
33     int                       module_count;  /* count of modules */
34     int                       module_pos;    /* current position in module snapshot */
35 };
36
37 static void snapshot_dump( struct object *obj, int verbose );
38 static void snapshot_destroy( struct object *obj );
39
40 static const struct object_ops snapshot_ops =
41 {
42     sizeof(struct snapshot),      /* size */
43     snapshot_dump,                /* dump */
44     no_add_queue,                 /* add_queue */
45     NULL,                         /* remove_queue */
46     NULL,                         /* signaled */
47     NULL,                         /* satisfied */
48     NULL,                         /* get_poll_events */
49     NULL,                         /* poll_event */
50     no_get_fd,                    /* get_fd */
51     no_flush,                     /* flush */
52     no_get_file_info,             /* get_file_info */
53     snapshot_destroy              /* destroy */
54 };
55
56
57 /* create a new snapshot */
58 static struct snapshot *create_snapshot( void *pid, int flags )
59 {
60     struct process *process = NULL;
61     struct snapshot *snapshot;
62
63     /* need a process for modules and heaps */
64     if (flags & (TH32CS_SNAPMODULE|TH32CS_SNAPHEAPLIST))
65     {
66         if (!pid) process = (struct process *)grab_object( current->process );
67         else if (!(process = get_process_from_id( pid ))) return NULL;
68     }
69
70     if (!(snapshot = alloc_object( &snapshot_ops, -1 )))
71     {
72         if (process) release_object( process );
73         return NULL;
74     }
75
76     snapshot->process = process;
77
78     snapshot->process_pos = 0;
79     snapshot->process_count = 0;
80     if (flags & TH32CS_SNAPPROCESS)
81         snapshot->processes = process_snap( &snapshot->process_count );
82
83     snapshot->thread_pos = 0;
84     snapshot->thread_count = 0;
85     if (flags & TH32CS_SNAPTHREAD)
86         snapshot->threads = thread_snap( &snapshot->thread_count );
87
88     snapshot->module_pos = 0;
89     snapshot->module_count = 0;
90     if (flags & TH32CS_SNAPMODULE)
91         snapshot->modules = module_snap( process, &snapshot->module_count );
92
93     return snapshot;
94 }
95
96 /* get the next process in the snapshot */
97 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_request *req )
98 {
99     struct process_snapshot *ptr;
100
101     if (!snapshot->process_count)
102     {
103         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
104         return 0;
105     }
106     if (req->reset) snapshot->process_pos = 0;
107     else if (snapshot->process_pos >= snapshot->process_count)
108     {
109         set_error( STATUS_NO_MORE_FILES );
110         return 0;
111     }
112     ptr = &snapshot->processes[snapshot->process_pos++];
113     req->count    = ptr->count;
114     req->pid      = get_process_id( ptr->process );
115     req->threads  = ptr->threads;
116     req->priority = ptr->priority;
117     return 1;
118 }
119
120 /* get the next thread in the snapshot */
121 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_request *req )
122 {
123     struct thread_snapshot *ptr;
124
125     if (!snapshot->thread_count)
126     {
127         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
128         return 0;
129     }
130     if (req->reset) snapshot->thread_pos = 0;
131     else if (snapshot->thread_pos >= snapshot->thread_count)
132     {
133         set_error( STATUS_NO_MORE_FILES );
134         return 0;
135     }
136     ptr = &snapshot->threads[snapshot->thread_pos++];
137     req->count     = ptr->count;
138     req->pid       = get_process_id( ptr->thread->process );
139     req->tid       = get_thread_id( ptr->thread );
140     req->base_pri  = ptr->priority;
141     req->delta_pri = 0;  /* FIXME */
142     return 1;
143 }
144
145 /* get the next module in the snapshot */
146 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_request *req )
147 {
148     struct module_snapshot *ptr;
149
150     if (!snapshot->module_count)
151     {
152         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
153         return 0;
154     }
155     if (req->reset) snapshot->module_pos = 0;
156     else if (snapshot->module_pos >= snapshot->module_count)
157     {
158         set_error( STATUS_NO_MORE_FILES );
159         return 0;
160     }
161     ptr = &snapshot->modules[snapshot->module_pos++];
162     req->pid  = get_process_id( snapshot->process );
163     req->base = ptr->base;
164     return 1;
165 }
166
167 static void snapshot_dump( struct object *obj, int verbose )
168 {
169     struct snapshot *snapshot = (struct snapshot *)obj;
170     assert( obj->ops == &snapshot_ops );
171     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
172              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
173 }
174
175 static void snapshot_destroy( struct object *obj )
176 {
177     int i;
178     struct snapshot *snapshot = (struct snapshot *)obj;
179     assert( obj->ops == &snapshot_ops );
180     if (snapshot->process_count)
181     {
182         for (i = 0; i < snapshot->process_count; i++)
183             release_object( snapshot->processes[i].process );
184         free( snapshot->processes );
185     }
186     if (snapshot->thread_count)
187     {
188         for (i = 0; i < snapshot->thread_count; i++)
189             release_object( snapshot->threads[i].thread );
190         free( snapshot->threads );
191     }
192     if (snapshot->module_count) free( snapshot->modules );
193     if (snapshot->process) release_object( snapshot->process );
194 }
195
196 /* create a snapshot */
197 DECL_HANDLER(create_snapshot)
198 {
199     struct snapshot *snapshot;
200
201     req->handle = 0;
202     if ((snapshot = create_snapshot( req->pid, req->flags )))
203     {
204         req->handle = alloc_handle( current->process, snapshot, 0, req->inherit );
205         release_object( snapshot );
206     }
207 }
208
209 /* get the next process from a snapshot */
210 DECL_HANDLER(next_process)
211 {
212     struct snapshot *snapshot;
213
214     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
215                                                        0, &snapshot_ops )))
216     {
217         snapshot_next_process( snapshot, req );
218         release_object( snapshot );
219     }
220 }
221
222 /* get the next thread from a snapshot */
223 DECL_HANDLER(next_thread)
224 {
225     struct snapshot *snapshot;
226
227     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
228                                                        0, &snapshot_ops )))
229     {
230         snapshot_next_thread( snapshot, req );
231         release_object( snapshot );
232     }
233 }
234
235 /* get the next module from a snapshot */
236 DECL_HANDLER(next_module)
237 {
238     struct snapshot *snapshot;
239
240     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
241                                                        0, &snapshot_ops )))
242     {
243         snapshot_next_module( snapshot, req );
244         release_object( snapshot );
245     }
246 }