sapi: Add a partial implementation of SpObjectTokenEnum::Next().
[wine.git] / server / snapshot.c
blobe35588a136ca83a3906c65282704fee28c85a5c1
1 /*
2 * Server-side snapshots
4 * Copyright (C) 1999 Alexandre Julliard
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.
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.
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
20 * FIXME: heap snapshots not implemented
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
36 #include "handle.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
42 struct snapshot
44 struct object obj; /* object header */
45 struct process_snapshot *processes; /* processes snapshot */
46 int process_count; /* count of processes */
47 int process_pos; /* current position in proc snapshot */
48 struct thread_snapshot *threads; /* threads snapshot */
49 int thread_count; /* count of threads */
50 int thread_pos; /* current position in thread snapshot */
53 static void snapshot_dump( struct object *obj, int verbose );
54 static void snapshot_destroy( struct object *obj );
56 static const struct object_ops snapshot_ops =
58 sizeof(struct snapshot), /* size */
59 snapshot_dump, /* dump */
60 no_get_type, /* get_type */
61 no_add_queue, /* add_queue */
62 NULL, /* remove_queue */
63 NULL, /* signaled */
64 NULL, /* satisfied */
65 no_signal, /* signal */
66 no_get_fd, /* get_fd */
67 no_map_access, /* map_access */
68 default_get_sd, /* get_sd */
69 default_set_sd, /* set_sd */
70 no_lookup_name, /* lookup_name */
71 no_link_name, /* link_name */
72 NULL, /* unlink_name */
73 no_open_file, /* open_file */
74 no_close_handle, /* close_handle */
75 snapshot_destroy /* destroy */
79 /* create a new snapshot */
80 static struct snapshot *create_snapshot( unsigned int flags )
82 struct snapshot *snapshot;
84 if (!(snapshot = alloc_object( &snapshot_ops ))) return NULL;
86 snapshot->process_pos = 0;
87 snapshot->process_count = 0;
88 if (flags & SNAP_PROCESS)
89 snapshot->processes = process_snap( &snapshot->process_count );
91 snapshot->thread_pos = 0;
92 snapshot->thread_count = 0;
93 if (flags & SNAP_THREAD)
94 snapshot->threads = thread_snap( &snapshot->thread_count );
96 return snapshot;
99 /* get the next process in the snapshot */
100 static int snapshot_next_process( struct snapshot *snapshot, struct next_process_reply *reply )
102 struct process_snapshot *ptr;
103 struct process_dll *exe_module;
105 if (!snapshot->process_count)
107 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
108 return 0;
110 if (snapshot->process_pos >= snapshot->process_count)
112 set_error( STATUS_NO_MORE_FILES );
113 return 0;
115 ptr = &snapshot->processes[snapshot->process_pos++];
116 reply->count = ptr->count;
117 reply->pid = get_process_id( ptr->process );
118 reply->ppid = ptr->process->parent_id;
119 reply->threads = ptr->threads;
120 reply->priority = ptr->priority;
121 reply->handles = ptr->handles;
122 reply->unix_pid = ptr->process->unix_pid;
123 if ((exe_module = get_process_exe_module( ptr->process )) && exe_module->filename)
125 data_size_t len = min( exe_module->namelen, get_reply_max_size() );
126 set_reply_data( exe_module->filename, len );
128 return 1;
131 /* get the next thread in the snapshot */
132 static int snapshot_next_thread( struct snapshot *snapshot, struct next_thread_reply *reply )
134 struct thread_snapshot *ptr;
136 if (!snapshot->thread_count)
138 set_error( STATUS_INVALID_PARAMETER ); /* FIXME */
139 return 0;
141 if (snapshot->thread_pos >= snapshot->thread_count)
143 set_error( STATUS_NO_MORE_FILES );
144 return 0;
146 ptr = &snapshot->threads[snapshot->thread_pos++];
147 reply->count = ptr->count;
148 reply->pid = get_process_id( ptr->thread->process );
149 reply->tid = get_thread_id( ptr->thread );
150 reply->base_pri = ptr->priority;
151 reply->delta_pri = 0; /* FIXME */
152 return 1;
155 static void snapshot_dump( struct object *obj, int verbose )
157 struct snapshot *snapshot = (struct snapshot *)obj;
158 assert( obj->ops == &snapshot_ops );
159 fprintf( stderr, "Snapshot: %d procs %d threads\n",
160 snapshot->process_count, snapshot->thread_count );
163 static void snapshot_destroy( struct object *obj )
165 int i;
166 struct snapshot *snapshot = (struct snapshot *)obj;
167 assert( obj->ops == &snapshot_ops );
168 if (snapshot->process_count)
170 for (i = 0; i < snapshot->process_count; i++)
171 release_object( snapshot->processes[i].process );
172 free( snapshot->processes );
174 if (snapshot->thread_count)
176 for (i = 0; i < snapshot->thread_count; i++)
177 release_object( snapshot->threads[i].thread );
178 free( snapshot->threads );
182 /* create a snapshot */
183 DECL_HANDLER(create_snapshot)
185 struct snapshot *snapshot;
187 reply->handle = 0;
188 if ((snapshot = create_snapshot( req->flags )))
190 reply->handle = alloc_handle( current->process, snapshot, 0, req->attributes );
191 release_object( snapshot );
195 /* get the next process from a snapshot */
196 DECL_HANDLER(next_process)
198 struct snapshot *snapshot;
200 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
201 0, &snapshot_ops )))
203 if (req->reset) snapshot->process_pos = 0;
204 snapshot_next_process( snapshot, reply );
205 release_object( snapshot );
209 /* get the next thread from a snapshot */
210 DECL_HANDLER(next_thread)
212 struct snapshot *snapshot;
214 if ((snapshot = (struct snapshot *)get_handle_obj( current->process, req->handle,
215 0, &snapshot_ops )))
217 if (req->reset) snapshot->thread_pos = 0;
218 snapshot_next_thread( snapshot, reply );
219 release_object( snapshot );