server: Add an open_file() function to the object operations.
[wine] / server / snapshot.c
1 /*
2  * Server-side snapshots
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * FIXME: heap snapshots not implemented
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
40
41
42 struct snapshot
43 {
44     struct object             obj;           /* object header */
45     struct process           *process;       /* process of this snapshot (for modules and heaps) */
46     struct process_snapshot  *processes;     /* processes snapshot */
47     int                       process_count; /* count of processes */
48     int                       process_pos;   /* current position in proc snapshot */
49     struct thread_snapshot   *threads;       /* threads snapshot */
50     int                       thread_count;  /* count of threads */
51     int                       thread_pos;    /* current position in thread snapshot */
52     struct module_snapshot   *modules;       /* modules snapshot */
53     int                       module_count;  /* count of modules */
54     int                       module_pos;    /* current position in module snapshot */
55 };
56
57 static void snapshot_dump( struct object *obj, int verbose );
58 static void snapshot_destroy( struct object *obj );
59
60 static const struct object_ops snapshot_ops =
61 {
62     sizeof(struct snapshot),      /* size */
63     snapshot_dump,                /* dump */
64     no_add_queue,                 /* add_queue */
65     NULL,                         /* remove_queue */
66     NULL,                         /* signaled */
67     NULL,                         /* satisfied */
68     no_signal,                    /* signal */
69     no_get_fd,                    /* get_fd */
70     no_map_access,                /* map_access */
71     no_lookup_name,               /* lookup_name */
72     no_open_file,                 /* open_file */
73     no_close_handle,              /* close_handle */
74     snapshot_destroy              /* destroy */
75 };
76
77
78 /* create a new snapshot */
79 static struct snapshot *create_snapshot( process_id_t pid, int flags )
80 {
81     struct process *process = NULL;
82     struct snapshot *snapshot;
83
84     /* need a process for modules and heaps */
85     if (flags & (SNAP_MODULE|SNAP_HEAPLIST))
86     {
87         if (!pid) process = (struct process *)grab_object( current->process );
88         else if (!(process = get_process_from_id( pid ))) return NULL;
89     }
90
91     if (!(snapshot = alloc_object( &snapshot_ops )))
92     {
93         if (process) release_object( process );
94         return NULL;
95     }
96
97     snapshot->process = process;
98
99     snapshot->process_pos = 0;
100     snapshot->process_count = 0;
101     if (flags & SNAP_PROCESS)
102         snapshot->processes = process_snap( &snapshot->process_count );
103
104     snapshot->thread_pos = 0;
105     snapshot->thread_count = 0;
106     if (flags & SNAP_THREAD)
107         snapshot->threads = thread_snap( &snapshot->thread_count );
108
109     snapshot->module_pos = 0;
110     snapshot->module_count = 0;
111     if (flags & SNAP_MODULE)
112         snapshot->modules = module_snap( process, &snapshot->module_count );
113
114     return snapshot;
115 }
116
117 /* get the next process in the snapshot */
118 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
119 {
120     struct process_snapshot *ptr;
121     struct process_dll *exe_module;
122
123     if (!snapshot->process_count)
124     {
125         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
126         return 0;
127     }
128     if (snapshot->process_pos >= snapshot->process_count)
129     {
130         set_error( STATUS_NO_MORE_FILES );
131         return 0;
132     }
133     ptr = &snapshot->processes[snapshot->process_pos++];
134     reply->count    = ptr->count;
135     reply->pid      = get_process_id( ptr->process );
136     reply->ppid     = ptr->process->parent ? get_process_id( ptr->process->parent ) : 0;
137     reply->heap     = NULL;  /* FIXME */
138     reply->module   = NULL;  /* FIXME */
139     reply->threads  = ptr->threads;
140     reply->priority = ptr->priority;
141     reply->handles  = ptr->handles;
142     if ((exe_module = get_process_exe_module( ptr->process )) && exe_module->filename)
143     {
144         data_size_t len = min( exe_module->namelen, get_reply_max_size() );
145         set_reply_data( exe_module->filename, len );
146     }
147     return 1;
148 }
149
150 /* get the next thread in the snapshot */
151 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
152 {
153     struct thread_snapshot *ptr;
154
155     if (!snapshot->thread_count)
156     {
157         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
158         return 0;
159     }
160     if (snapshot->thread_pos >= snapshot->thread_count)
161     {
162         set_error( STATUS_NO_MORE_FILES );
163         return 0;
164     }
165     ptr = &snapshot->threads[snapshot->thread_pos++];
166     reply->count     = ptr->count;
167     reply->pid       = get_process_id( ptr->thread->process );
168     reply->tid       = get_thread_id( ptr->thread );
169     reply->base_pri  = ptr->priority;
170     reply->delta_pri = 0;  /* FIXME */
171     return 1;
172 }
173
174 /* get the next module in the snapshot */
175 static int snapshot_next_module( struct snapshot *snapshot, struct next_module_reply *reply )
176 {
177     struct module_snapshot *ptr;
178
179     if (!snapshot->module_count)
180     {
181         set_error( STATUS_INVALID_PARAMETER );  /* FIXME */
182         return 0;
183     }
184     if (snapshot->module_pos >= snapshot->module_count)
185     {
186         set_error( STATUS_NO_MORE_FILES );
187         return 0;
188     }
189     ptr = &snapshot->modules[snapshot->module_pos++];
190     reply->pid  = get_process_id( snapshot->process );
191     reply->base = ptr->base;
192     reply->size = ptr->size;
193     if (ptr->filename)
194     {
195         data_size_t len = min( ptr->namelen, get_reply_max_size() );
196         set_reply_data( ptr->filename, len );
197     }
198     return 1;
199 }
200
201 static void snapshot_dump( struct object *obj, int verbose )
202 {
203     struct snapshot *snapshot = (struct snapshot *)obj;
204     assert( obj->ops == &snapshot_ops );
205     fprintf( stderr, "Snapshot: %d procs %d threads %d modules\n",
206              snapshot->process_count, snapshot->thread_count, snapshot->module_count );
207 }
208
209 static void snapshot_destroy( struct object *obj )
210 {
211     int i;
212     struct snapshot *snapshot = (struct snapshot *)obj;
213     assert( obj->ops == &snapshot_ops );
214     if (snapshot->process_count)
215     {
216         for (i = 0; i < snapshot->process_count; i++)
217             release_object( snapshot->processes[i].process );
218         free( snapshot->processes );
219     }
220     if (snapshot->thread_count)
221     {
222         for (i = 0; i < snapshot->thread_count; i++)
223             release_object( snapshot->threads[i].thread );
224         free( snapshot->threads );
225     }
226     if (snapshot->module_count)
227     {
228         for (i = 0; i < snapshot->module_count; i++)
229             free( snapshot->modules[i].filename );
230         free( snapshot->modules );
231     }
232     if (snapshot->process) release_object( snapshot->process );
233 }
234
235 /* create a snapshot */
236 DECL_HANDLER(create_snapshot)
237 {
238     struct snapshot *snapshot;
239
240     reply->handle = 0;
241     if ((snapshot = create_snapshot( req->pid, req->flags )))
242     {
243         reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
244         release_object( snapshot );
245     }
246 }
247
248 /* get the next process from a snapshot */
249 DECL_HANDLER(next_process)
250 {
251     struct snapshot *snapshot;
252
253     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
254                                                        0, &snapshot_ops )))
255     {
256         if (req->reset) snapshot->process_pos = 0;
257         snapshot_next_process( snapshot, reply );
258         release_object( snapshot );
259     }
260 }
261
262 /* get the next thread from a snapshot */
263 DECL_HANDLER(next_thread)
264 {
265     struct snapshot *snapshot;
266
267     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
268                                                        0, &snapshot_ops )))
269     {
270         if (req->reset) snapshot->thread_pos = 0;
271         snapshot_next_thread( snapshot, reply );
272         release_object( snapshot );
273     }
274 }
275
276 /* get the next module from a snapshot */
277 DECL_HANDLER(next_module)
278 {
279     struct snapshot *snapshot;
280
281     if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
282                                                        0, &snapshot_ops )))
283     {
284         if (req->reset) snapshot->module_pos = 0;
285         snapshot_next_module( snapshot, reply );
286         release_object( snapshot );
287     }
288 }