]> git.sesse.net Git - vlc/blob - src/misc/objects.c
eventfd: set close-on-exec flag
[vlc] / src / misc / objects.c
1 /*****************************************************************************
2  * objects.c: vlc_object_t handling
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * Copyright (C) 2006-2010 RĂ©mi Denis-Courmont
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /**
25  * \file
26  * This file contains the functions to handle the vlc_object_t type
27  *
28  * Unless otherwise stated, functions in this file are not cancellation point.
29  * All functions in this file are safe w.r.t. deferred cancellation.
30  */
31
32
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <vlc_common.h>
41
42 #include "../libvlc.h"
43 #include <vlc_aout.h>
44 #include "audio_output/aout_internal.h"
45
46 #include "vlc_interface.h"
47 #include "vlc_codec.h"
48
49 #include "variables.h"
50 #ifndef WIN32
51 # include <unistd.h>
52 #else
53 # include <io.h>
54 # include <winsock2.h>
55 # include <ws2tcpip.h>
56 # undef  read
57 # define read( a, b, c )  recv (a, b, c, 0)
58 # undef  write
59 # define write( a, b, c ) send (a, b, c, 0)
60 # undef  close
61 # define close( a )       closesocket (a)
62 #endif
63
64 #include <search.h>
65 #include <limits.h>
66 #include <assert.h>
67
68 #if defined (HAVE_SYS_EVENTFD_H)
69 # include <sys/eventfd.h>
70 #endif
71
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int  DumpCommand( vlc_object_t *, char const *,
77                          vlc_value_t, vlc_value_t, void * );
78
79 static vlc_object_t * FindParent    ( vlc_object_t *, int );
80 static vlc_object_t * FindChild     ( vlc_object_internals_t *, int );
81 static vlc_object_t * FindParentName( vlc_object_t *, const char * );
82 static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
83 static void PrintObject( vlc_object_internals_t *, const char * );
84 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
85
86 static vlc_list_t   * NewList       ( int );
87
88 static void vlc_object_destroy( vlc_object_t *p_this );
89 static void vlc_object_detach_unlocked (vlc_object_t *p_this);
90
91 /*****************************************************************************
92  * Local structure lock
93  *****************************************************************************/
94 static void libvlc_lock (libvlc_int_t *p_libvlc)
95 {
96     vlc_mutex_lock (&(libvlc_priv (p_libvlc)->structure_lock));
97 }
98
99 static void libvlc_unlock (libvlc_int_t *p_libvlc)
100 {
101     vlc_mutex_unlock (&(libvlc_priv (p_libvlc)->structure_lock));
102 }
103
104 #undef vlc_custom_create
105 void *vlc_custom_create( vlc_object_t *p_this, size_t i_size,
106                          int i_type, const char *psz_type )
107 {
108     vlc_object_t *p_new;
109     vlc_object_internals_t *p_priv;
110
111     /* NOTE:
112      * VLC objects are laid out as follow:
113      * - first the LibVLC-private per-object data,
114      * - then VLC_COMMON members from vlc_object_t,
115      * - finally, the type-specific data (if any).
116      *
117      * This function initializes the LibVLC and common data,
118      * and zeroes the rest.
119      */
120     p_priv = calloc( 1, sizeof( *p_priv ) + i_size );
121     if( p_priv == NULL )
122         return NULL;
123
124     assert (i_size >= sizeof (vlc_object_t));
125     p_new = (vlc_object_t *)(p_priv + 1);
126
127     p_priv->i_object_type = i_type;
128     p_new->psz_object_type = psz_type;
129     p_priv->psz_name = NULL;
130
131     p_new->b_die = false;
132     p_new->b_force = false;
133
134     p_new->psz_header = NULL;
135
136     if (p_this)
137         p_new->i_flags = p_this->i_flags
138             & (OBJECT_FLAGS_NODBG|OBJECT_FLAGS_QUIET|OBJECT_FLAGS_NOINTERACT);
139
140     p_priv->var_root = NULL;
141
142     if( p_this == NULL )
143     {
144         libvlc_int_t *self = (libvlc_int_t*)p_new;
145         p_new->p_libvlc = self;
146         vlc_mutex_init (&(libvlc_priv (self)->structure_lock));
147         p_this = p_new;
148     }
149     else
150         p_new->p_libvlc = p_this->p_libvlc;
151
152     vlc_spin_init( &p_priv->ref_spin );
153     p_priv->i_refcount = 1;
154     p_priv->pf_destructor = NULL;
155     p_priv->b_thread = false;
156     p_new->p_parent = NULL;
157     p_priv->first = NULL;
158 #ifndef NDEBUG
159     p_priv->old_parent = NULL;
160 #endif
161
162     /* Initialize mutexes and condvars */
163     vlc_mutex_init( &p_priv->var_lock );
164     vlc_cond_init( &p_priv->var_wait );
165     p_priv->pipes[0] = p_priv->pipes[1] = -1;
166
167     if (p_new == VLC_OBJECT(p_new->p_libvlc))
168     {   /* TODO: should be in src/libvlc.c */
169         int canc = vlc_savecancel ();
170         var_Create( p_new, "tree", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
171         var_AddCallback( p_new, "tree", DumpCommand, NULL );
172         var_Create( p_new, "vars", VLC_VAR_STRING | VLC_VAR_ISCOMMAND );
173         var_AddCallback( p_new, "vars", DumpCommand, NULL );
174         vlc_restorecancel (canc);
175     }
176
177     return p_new;
178 }
179
180 #undef vlc_object_create
181 /**
182  * Allocates and initializes a vlc object.
183  *
184  * @param i_size object byte size
185  *
186  * @return the new object, or NULL on error.
187  */
188 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
189 {
190     return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
191 }
192
193 #undef vlc_object_set_destructor
194 /**
195  ****************************************************************************
196  * Set the destructor of a vlc object
197  *
198  * This function sets the destructor of the vlc object. It will be called
199  * when the object is destroyed when the its refcount reaches 0.
200  * (It is called by the internal function vlc_object_destroy())
201  *****************************************************************************/
202 void vlc_object_set_destructor( vlc_object_t *p_this,
203                                 vlc_destructor_t pf_destructor )
204 {
205     vlc_object_internals_t *p_priv = vlc_internals(p_this );
206
207     vlc_spin_lock( &p_priv->ref_spin );
208     p_priv->pf_destructor = pf_destructor;
209     vlc_spin_unlock( &p_priv->ref_spin );
210 }
211
212 static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
213
214 #undef vlc_object_set_name
215 int vlc_object_set_name(vlc_object_t *obj, const char *name)
216 {
217     vlc_object_internals_t *priv = vlc_internals(obj);
218     char *newname = name ? strdup (name) : NULL;
219     char *oldname;
220
221     vlc_mutex_lock (&name_lock);
222     oldname = priv->psz_name;
223     priv->psz_name = newname;
224     vlc_mutex_unlock (&name_lock);
225
226     free (oldname);
227     return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
228 }
229
230 #undef vlc_object_get_name
231 char *vlc_object_get_name(const vlc_object_t *obj)
232 {
233     vlc_object_internals_t *priv = vlc_internals(obj);
234     char *name;
235
236     vlc_mutex_lock (&name_lock);
237     name = priv->psz_name ? strdup (priv->psz_name) : NULL;
238     vlc_mutex_unlock (&name_lock);
239
240     return name;
241 }
242
243 /**
244  ****************************************************************************
245  * Destroy a vlc object (Internal)
246  *
247  * This function destroys an object that has been previously allocated with
248  * vlc_object_create. The object's refcount must be zero and it must not be
249  * attached to other objects in any way.
250  *
251  * This function must be called with cancellation disabled (currently).
252  *****************************************************************************/
253 static void vlc_object_destroy( vlc_object_t *p_this )
254 {
255     vlc_object_internals_t *p_priv = vlc_internals( p_this );
256
257     /* Objects are always detached beforehand */
258     assert( !p_this->p_parent );
259
260     /* Send a kill to the object's thread if applicable */
261     vlc_object_kill( p_this );
262
263     /* Call the custom "subclass" destructor */
264     if( p_priv->pf_destructor )
265         p_priv->pf_destructor( p_this );
266
267     /* Any thread must have been cleaned up at this point. */
268     assert( !p_priv->b_thread );
269
270     /* Destroy the associated variables. */
271     var_DestroyAll( p_this );
272
273     vlc_cond_destroy( &p_priv->var_wait );
274     vlc_mutex_destroy( &p_priv->var_lock );
275
276     free( p_this->psz_header );
277
278     free( p_priv->psz_name );
279
280     vlc_spin_destroy( &p_priv->ref_spin );
281     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
282         close( p_priv->pipes[1] );
283     if( p_priv->pipes[0] != -1 )
284         close( p_priv->pipes[0] );
285     if( VLC_OBJECT(p_this->p_libvlc) == p_this )
286         vlc_mutex_destroy (&(libvlc_priv ((libvlc_int_t *)p_this)->structure_lock));
287
288     free( p_priv );
289 }
290
291
292 #ifdef WIN32
293 /**
294  * select()-able pipes emulated using Winsock
295  */
296 static int pipe (int fd[2])
297 {
298     SOCKADDR_IN addr;
299     int addrlen = sizeof (addr);
300
301     SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
302            c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
303     if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
304         goto error;
305
306     memset (&addr, 0, sizeof (addr));
307     addr.sin_family = AF_INET;
308     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
309     if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
310      || getsockname (l, (PSOCKADDR)&addr, &addrlen)
311      || listen (l, 1)
312      || connect (c, (PSOCKADDR)&addr, addrlen))
313         goto error;
314
315     a = accept (l, NULL, NULL);
316     if (a == INVALID_SOCKET)
317         goto error;
318
319     closesocket (l);
320     //shutdown (a, 0);
321     //shutdown (c, 1);
322     fd[0] = c;
323     fd[1] = a;
324     return 0;
325
326 error:
327     if (l != INVALID_SOCKET)
328         closesocket (l);
329     if (c != INVALID_SOCKET)
330         closesocket (c);
331     return -1;
332 }
333 #endif /* WIN32 */
334
335 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
336
337 /**
338  * Returns the readable end of a pipe that becomes readable once termination
339  * of the object is requested (vlc_object_kill()).
340  * This can be used to wake-up out of a select() or poll() event loop, such
341  * typically when doing network I/O.
342  *
343  * Note that the pipe will remain the same for the lifetime of the object.
344  * DO NOT read the pipe nor close it yourself. Ever.
345  *
346  * @param obj object that would be "killed"
347  * @return a readable pipe descriptor, or -1 on error.
348  */
349 int vlc_object_waitpipe( vlc_object_t *obj )
350 {
351     vlc_object_internals_t *internals = vlc_internals( obj );
352
353     vlc_mutex_lock (&pipe_lock);
354     if (internals->pipes[0] == -1)
355     {
356         /* This can only ever happen if someone killed us without locking: */
357         assert (internals->pipes[1] == -1);
358
359 #if defined (HAVE_SYS_EVENTFD_H)
360         internals->pipes[0] = internals->pipes[1] = eventfd (0, EFD_CLOEXEC);
361         if (internals->pipes[0] == -1)
362 #endif
363         {
364             if (pipe (internals->pipes))
365                 internals->pipes[0] = internals->pipes[1] = -1;
366         }
367
368         if (internals->pipes[0] != -1 && obj->b_die)
369         {   /* Race condition: vlc_object_kill() already invoked! */
370             msg_Dbg (obj, "waitpipe: object already dying");
371             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
372         }
373     }
374     vlc_mutex_unlock (&pipe_lock);
375     return internals->pipes[0];
376 }
377
378 #undef vlc_object_kill
379 /**
380  * Requests termination of an object, cancels the object thread, and make the
381  * object wait pipe (if it exists) readable. Not a cancellation point.
382  */
383 void vlc_object_kill( vlc_object_t *p_this )
384 {
385     vlc_object_internals_t *priv = vlc_internals( p_this );
386     int fd = -1;
387
388     vlc_thread_cancel( p_this );
389     vlc_mutex_lock( &pipe_lock );
390     if( !p_this->b_die )
391     {
392         fd = priv->pipes[1];
393         p_this->b_die = true;
394     }
395
396     /* This also serves as a memory barrier toward vlc_object_alive(): */
397     vlc_mutex_unlock( &pipe_lock );
398
399     if (fd != -1)
400     {
401         int canc = vlc_savecancel ();
402
403         /* write _after_ setting b_die, so vlc_object_alive() returns false */
404         write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
405         msg_Dbg (p_this, "waitpipe: object killed");
406         vlc_restorecancel (canc);
407     }
408 }
409
410 #undef vlc_object_find
411 /*****************************************************************************
412  * find a typed object and increment its refcount
413  *****************************************************************************
414  * This function recursively looks for a given object type. i_mode can be one
415  * of FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
416  *****************************************************************************/
417 void * vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
418 {
419     vlc_object_t *p_found;
420
421     /* If we are of the requested type ourselves, don't look further */
422     if( vlc_internals (p_this)->i_object_type == i_type )
423     {
424         vlc_object_hold( p_this );
425         return p_this;
426     }
427
428     /* Otherwise, recursively look for the object */
429     if (i_mode == FIND_ANYWHERE)
430         return vlc_object_find (VLC_OBJECT(p_this->p_libvlc), i_type, FIND_CHILD);
431
432     switch (i_type)
433     {
434         case VLC_OBJECT_VOUT:
435         case VLC_OBJECT_AOUT:
436             break;
437         case VLC_OBJECT_INPUT:
438             /* input can only be accessed like this from children,
439              * otherwise we could not promise that it is initialized */
440             if (i_mode != FIND_PARENT)
441                 return NULL;
442             break;
443         default:
444             return NULL;
445     }
446
447     libvlc_lock (p_this->p_libvlc);
448     switch (i_mode)
449     {
450         case FIND_PARENT:
451             p_found = FindParent (p_this, i_type);
452             break;
453         case FIND_CHILD:
454             p_found = FindChild (vlc_internals (p_this), i_type);
455             break;
456         default:
457             assert (0);
458     }
459     libvlc_unlock (p_this->p_libvlc);
460     return p_found;
461 }
462
463
464 static int objnamecmp(const vlc_object_t *obj, const char *name)
465 {
466     char *objname = vlc_object_get_name(obj);
467     if (objname == NULL)
468         return INT_MIN;
469
470     int ret = strcmp (objname, name);
471     free (objname);
472     return ret;
473 }
474
475 #undef vlc_object_find_name
476 /**
477  * Finds a named object and increment its reference count.
478  * Beware that objects found in this manner can be "owned" by another thread,
479  * be of _any_ type, and be attached to any module (if any). With such an
480  * object reference, you can set or get object variables, emit log messages,
481  * and read write-once object parameters (psz_object_type, etc).
482  * You CANNOT cast the object to a more specific object type, and you
483  * definitely cannot invoke object type-specific callbacks with this.
484  *
485  * @param p_this object to search from
486  * @param psz_name name of the object to search for
487  * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
488  *
489  * @return a matching object (must be released by the caller),
490  * or NULL on error.
491  */
492 vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
493                                     const char *psz_name, int i_mode )
494 {
495     vlc_object_t *p_found;
496
497     /* Reading psz_object_name from a separate inhibits thread-safety.
498      * Use a libvlc address variable instead for that sort of things! */
499     msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
500     /* If have the requested name ourselves, don't look further */
501     if( !objnamecmp(p_this, psz_name) )
502     {
503         vlc_object_hold( p_this );
504         return p_this;
505     }
506
507     /* Otherwise, recursively look for the object */
508     if (i_mode == FIND_ANYWHERE)
509         return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
510                                      FIND_CHILD);
511
512     libvlc_lock (p_this->p_libvlc);
513     vlc_mutex_lock (&name_lock);
514     switch (i_mode)
515     {
516         case FIND_PARENT:
517             p_found = FindParentName (p_this, psz_name);
518             break;
519         case FIND_CHILD:
520             p_found = FindChildName (vlc_internals (p_this), psz_name);
521             break;
522         default:
523             assert (0);
524     }
525     vlc_mutex_unlock (&name_lock);
526     libvlc_unlock (p_this->p_libvlc);
527     return p_found;
528 }
529
530 #undef vlc_object_hold
531 /**
532  * Increment an object reference counter.
533  */
534 void * vlc_object_hold( vlc_object_t *p_this )
535 {
536     vlc_object_internals_t *internals = vlc_internals( p_this );
537
538     vlc_spin_lock( &internals->ref_spin );
539     /* Avoid obvious freed object uses */
540     assert( internals->i_refcount > 0 );
541     /* Increment the counter */
542     internals->i_refcount++;
543     vlc_spin_unlock( &internals->ref_spin );
544     return p_this;
545 }
546
547 #undef vlc_object_release
548 /*****************************************************************************
549  * Decrement an object refcount
550  * And destroy the object if its refcount reach zero.
551  *****************************************************************************/
552 void vlc_object_release( vlc_object_t *p_this )
553 {
554     vlc_object_internals_t *internals = vlc_internals( p_this );
555     vlc_object_t *parent = NULL;
556     bool b_should_destroy;
557
558     vlc_spin_lock( &internals->ref_spin );
559     assert( internals->i_refcount > 0 );
560
561     if( internals->i_refcount > 1 )
562     {
563         /* Fast path */
564         /* There are still other references to the object */
565         internals->i_refcount--;
566         vlc_spin_unlock( &internals->ref_spin );
567         return;
568     }
569     vlc_spin_unlock( &internals->ref_spin );
570
571     /* Slow path */
572     /* Remember that we cannot hold the spin while waiting on the mutex */
573     libvlc_lock (p_this->p_libvlc);
574     /* Take the spin again. Note that another thread may have held the
575      * object in the (very short) mean time. */
576     vlc_spin_lock( &internals->ref_spin );
577     b_should_destroy = --internals->i_refcount == 0;
578     vlc_spin_unlock( &internals->ref_spin );
579
580     if( b_should_destroy )
581     {
582         parent = p_this->p_parent;
583         if (parent)
584             /* Detach from parent to protect against FIND_CHILDREN */
585             vlc_object_detach_unlocked (p_this);
586
587         /* We have no children */
588         assert (internals->first == NULL);
589     }
590     libvlc_unlock (p_this->p_libvlc);
591
592     if( b_should_destroy )
593     {
594         int canc;
595
596         canc = vlc_savecancel ();
597         vlc_object_destroy( p_this );
598         vlc_restorecancel (canc);
599         if (parent)
600             vlc_object_release (parent);
601     }
602 }
603
604 #undef vlc_object_attach
605 /**
606  ****************************************************************************
607  * attach object to a parent object
608  *****************************************************************************
609  * This function sets p_this as a child of p_parent, and p_parent as a parent
610  * of p_this. This link can be undone using vlc_object_detach.
611  *****************************************************************************/
612 void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent )
613 {
614     if( !p_this ) return;
615
616     vlc_object_internals_t *pap = vlc_internals (p_parent);
617     vlc_object_internals_t *priv = vlc_internals (p_this);
618     vlc_object_t *p_old_parent;
619
620     priv->prev = NULL;
621     vlc_object_hold (p_parent);
622     libvlc_lock (p_this->p_libvlc);
623 #ifndef NDEBUG
624     /* Reparenting an object carries a risk of invalid access to the parent,
625      * from another thread. This can happen when inheriting a variable, or
626      * through any direct access to vlc_object_t.p_parent. Also, reparenting
627      * brings a functional bug, whereby the reparented object uses incorrect
628      * old values for inherited variables (as the new parent may have different
629      * variable values, especially if it is an input).
630      * Note that the old parent may be already destroyed.
631      * So its pointer must not be dereferenced.
632      */
633     if (priv->old_parent)
634         msg_Info (p_this, "Reparenting an object is dangerous (%p -> %p)!",
635                   priv->old_parent, p_parent);
636 #endif
637
638     p_old_parent = p_this->p_parent;
639     if (p_old_parent)
640         vlc_object_detach_unlocked (p_this);
641
642     /* Attach the parent to its child */
643     p_this->p_parent = p_parent;
644
645     /* Attach the child to its parent */
646     priv->next = pap->first;
647     if (priv->next != NULL)
648         priv->next->prev = priv;
649     pap->first = priv;
650     libvlc_unlock (p_this->p_libvlc);
651
652     if (p_old_parent)
653         vlc_object_release (p_old_parent);
654 }
655
656
657 static void vlc_object_detach_unlocked (vlc_object_t *p_this)
658 {
659     assert (p_this->p_parent != NULL);
660
661     vlc_object_internals_t *pap = vlc_internals (p_this->p_parent);
662     vlc_object_internals_t *priv = vlc_internals (p_this);
663
664     /* Unlink */
665     if (priv->prev != NULL)
666         priv->prev->next = priv->next;
667     else
668         pap->first = priv->next;
669     if (priv->next != NULL)
670         priv->next->prev = priv->prev;
671
672     /* Remove p_this's parent */
673 #ifndef NDEBUG
674     priv->old_parent = p_this->p_parent;
675 #endif
676     p_this->p_parent = NULL;
677 }
678
679 #undef vlc_object_detach
680 /**
681  ****************************************************************************
682  * detach object from its parent
683  *****************************************************************************
684  * This function removes all links between an object and its parent.
685  *****************************************************************************/
686 void vlc_object_detach( vlc_object_t *p_this )
687 {
688     vlc_object_t *p_parent;
689     if( !p_this ) return;
690
691     libvlc_lock (p_this->p_libvlc);
692     p_parent = p_this->p_parent;
693     if (p_parent)
694         vlc_object_detach_unlocked( p_this );
695     libvlc_unlock (p_this->p_libvlc);
696
697     if (p_parent)
698         vlc_object_release (p_parent);
699 }
700
701 #undef vlc_list_children
702 /**
703  * Gets the list of children of an objects, and increment their reference
704  * count.
705  * @return a list (possibly empty) or NULL in case of error.
706  */
707 vlc_list_t *vlc_list_children( vlc_object_t *obj )
708 {
709     vlc_list_t *l;
710     vlc_object_internals_t *priv;
711     unsigned count = 0;
712
713     libvlc_lock (obj->p_libvlc);
714     for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
715          count++;
716     l = NewList (count);
717     if (likely(l != NULL))
718     {
719         unsigned i = 0;
720
721         for (priv = vlc_internals (obj)->first; priv; priv = priv->next)
722             l->p_values[i++].p_object = vlc_object_hold (vlc_externals (priv));
723     }
724     libvlc_unlock (obj->p_libvlc);
725     return l;
726 }
727
728 static void DumpVariable (const void *data, const VISIT which, const int depth)
729 {
730     if (which != postorder && which != leaf)
731         return;
732     (void) depth;
733
734     const variable_t *p_var = *(const variable_t **)data;
735     const char *psz_type = "unknown";
736
737     switch( p_var->i_type & VLC_VAR_TYPE )
738     {
739 #define MYCASE( type, nice )    \
740         case VLC_VAR_ ## type:  \
741             psz_type = nice;    \
742             break;
743         MYCASE( VOID, "void" );
744         MYCASE( BOOL, "bool" );
745         MYCASE( INTEGER, "integer" );
746         MYCASE( HOTKEY, "hotkey" );
747         MYCASE( STRING, "string" );
748         MYCASE( MODULE, "module" );
749         MYCASE( FILE, "file" );
750         MYCASE( DIRECTORY, "directory" );
751         MYCASE( VARIABLE, "variable" );
752         MYCASE( FLOAT, "float" );
753         MYCASE( TIME, "time" );
754         MYCASE( ADDRESS, "address" );
755         MYCASE( MUTEX, "mutex" );
756         MYCASE( LIST, "list" );
757 #undef MYCASE
758     }
759     printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
760     if( p_var->psz_text )
761         printf( ", %s", p_var->psz_text );
762     fputc( ')', stdout );
763     if( p_var->i_type & VLC_VAR_HASCHOICE )
764         fputs( ", has choices", stdout );
765     if( p_var->i_type & VLC_VAR_ISCOMMAND )
766         fputs( ", command", stdout );
767     if( p_var->i_entries )
768         printf( ", %d callbacks", p_var->i_entries );
769     switch( p_var->i_type & VLC_VAR_CLASS )
770     {
771         case VLC_VAR_VOID:
772         case VLC_VAR_MUTEX:
773             break;
774         case VLC_VAR_BOOL:
775             printf( ": %s", p_var->val.b_bool ? "true" : "false" );
776             break;
777         case VLC_VAR_INTEGER:
778             printf( ": %d", p_var->val.i_int );
779             break;
780         case VLC_VAR_STRING:
781             printf( ": \"%s\"", p_var->val.psz_string );
782             break;
783         case VLC_VAR_FLOAT:
784             printf( ": %f", p_var->val.f_float );
785             break;
786         case VLC_VAR_TIME:
787             printf( ": %"PRIi64, (int64_t)p_var->val.i_time );
788             break;
789         case VLC_VAR_ADDRESS:
790             printf( ": %p", p_var->val.p_address );
791             break;
792         case VLC_VAR_LIST:
793             fputs( ": TODO", stdout );
794             break;
795     }
796     fputc( '\n', stdout );
797 }
798
799 /*****************************************************************************
800  * DumpCommand: print the current vlc structure
801  *****************************************************************************
802  * This function prints either an ASCII tree showing the connections between
803  * vlc objects, and additional information such as their refcount, thread ID,
804  * etc. (command "tree"), or the same data as a simple list (command "list").
805  *****************************************************************************/
806 static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
807                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
808 {
809     (void)oldval; (void)p_data;
810     vlc_object_t *p_object = NULL;
811
812     if( *newval.psz_string )
813     {
814         /* try using the object's name to find it */
815         p_object = vlc_object_find_name( p_this, newval.psz_string,
816                                          FIND_ANYWHERE );
817         if( !p_object )
818         {
819             return VLC_ENOOBJ;
820         }
821     }
822
823     libvlc_lock (p_this->p_libvlc);
824     if( *psz_cmd == 't' )
825     {
826         char psz_foo[2 * MAX_DUMPSTRUCTURE_DEPTH + 1];
827
828         if( !p_object )
829             p_object = VLC_OBJECT(p_this->p_libvlc);
830
831         psz_foo[0] = '|';
832         DumpStructure( vlc_internals(p_object), 0, psz_foo );
833     }
834     else if( *psz_cmd == 'v' )
835     {
836         if( !p_object )
837             p_object = p_this->p_libvlc ? VLC_OBJECT(p_this->p_libvlc) : p_this;
838
839         PrintObject( vlc_internals(p_object), "" );
840         vlc_mutex_lock( &vlc_internals( p_object )->var_lock );
841         if( vlc_internals( p_object )->var_root == NULL )
842             puts( " `-o No variables" );
843         else
844             twalk( vlc_internals( p_object )->var_root, DumpVariable );
845         vlc_mutex_unlock( &vlc_internals( p_object )->var_lock );
846     }
847     libvlc_unlock (p_this->p_libvlc);
848
849     if( *newval.psz_string )
850     {
851         vlc_object_release( p_object );
852     }
853     return VLC_SUCCESS;
854 }
855
856 /*****************************************************************************
857  * vlc_list_release: free a list previously allocated by vlc_list_find
858  *****************************************************************************
859  * This function decreases the refcount of all objects in the list and
860  * frees the list.
861  *****************************************************************************/
862 void vlc_list_release( vlc_list_t *p_list )
863 {
864     int i_index;
865
866     for( i_index = 0; i_index < p_list->i_count; i_index++ )
867     {
868         vlc_object_release( p_list->p_values[i_index].p_object );
869     }
870
871     free( p_list->p_values );
872     free( p_list );
873 }
874
875 /* Following functions are local */
876
877 static vlc_object_t *FindParent (vlc_object_t *p_this, int i_type)
878 {
879     for (vlc_object_t *parent = p_this->p_parent;
880          parent != NULL;
881          parent = parent->p_parent)
882     {
883         if (vlc_internals (parent)->i_object_type == i_type)
884             return vlc_object_hold (parent);
885     }
886     return NULL;
887 }
888
889 static vlc_object_t *FindParentName (vlc_object_t *p_this, const char *name)
890 {
891     for (vlc_object_t *parent = p_this->p_parent;
892          parent != NULL;
893          parent = parent->p_parent)
894     {
895         const char *objname = vlc_internals (parent)->psz_name;
896         if (objname && !strcmp (objname, name))
897             return vlc_object_hold (parent);
898     }
899     return NULL;
900 }
901
902 static vlc_object_t *FindChild (vlc_object_internals_t *priv, int i_type)
903 {
904     for (priv = priv->first; priv != NULL; priv = priv->next)
905     {
906         if (priv->i_object_type == i_type)
907             return vlc_object_hold (vlc_externals (priv));
908
909         vlc_object_t *found = FindChild (priv, i_type);
910         if (found != NULL)
911             return found;
912     }
913     return NULL;
914 }
915
916 static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
917                                     const char *name)
918 {
919     for (priv = priv->first; priv != NULL; priv = priv->next)
920     {
921         if (priv->psz_name && !strcmp (priv->psz_name, name))
922             return vlc_object_hold (vlc_externals (priv));
923
924         vlc_object_t *found = FindChildName (priv, name);
925         if (found != NULL)
926             return found;
927     }
928     return NULL;
929 }
930
931 static void PrintObject( vlc_object_internals_t *priv,
932                          const char *psz_prefix )
933 {
934     char psz_refcount[20], psz_thread[30], psz_name[50], psz_parent[20];
935
936     int canc = vlc_savecancel ();
937     memset( &psz_name, 0, sizeof(psz_name) );
938
939     vlc_mutex_lock (&name_lock);
940     if (priv->psz_name != NULL)
941     {
942         snprintf( psz_name, 49, " \"%s\"", priv->psz_name );
943         if( psz_name[48] )
944             psz_name[48] = '\"';
945     }
946     vlc_mutex_unlock (&name_lock);
947
948     psz_refcount[0] = '\0';
949     if( priv->i_refcount > 0 )
950         snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
951
952     psz_thread[0] = '\0';
953     if( priv->b_thread )
954         snprintf( psz_thread, 29, " (thread %lu)",
955                   (unsigned long)priv->thread_id );
956
957     psz_parent[0] = '\0';
958     /* FIXME: need structure lock!!! */
959     if( vlc_externals(priv)->p_parent )
960         snprintf( psz_parent, 19, ", parent %p",
961                   vlc_externals(priv)->p_parent );
962
963     printf( " %so %p %s%s%s%s%s\n", psz_prefix,
964             vlc_externals(priv), vlc_externals(priv)->psz_object_type,
965             psz_name, psz_thread, psz_refcount, psz_parent );
966     vlc_restorecancel (canc);
967 }
968
969 static void DumpStructure (vlc_object_internals_t *priv, unsigned i_level,
970                            char *psz_foo)
971 {
972     char i_back = psz_foo[i_level];
973     psz_foo[i_level] = '\0';
974
975     PrintObject (priv, psz_foo);
976
977     psz_foo[i_level] = i_back;
978
979     if( i_level / 2 >= MAX_DUMPSTRUCTURE_DEPTH )
980     {
981         msg_Warn( vlc_externals(priv), "structure tree is too deep" );
982         return;
983     }
984
985     for (priv = priv->first; priv != NULL; priv = priv->next)
986     {
987         if( i_level )
988         {
989             psz_foo[i_level-1] = ' ';
990
991             if( psz_foo[i_level-2] == '`' )
992             {
993                 psz_foo[i_level-2] = ' ';
994             }
995         }
996
997         psz_foo[i_level] = priv->next ? '|' : '`';
998         psz_foo[i_level+1] = '-';
999         psz_foo[i_level+2] = '\0';
1000
1001         DumpStructure (priv, i_level + 2, psz_foo);
1002     }
1003 }
1004
1005 static vlc_list_t * NewList( int i_count )
1006 {
1007     vlc_list_t * p_list = malloc( sizeof( vlc_list_t ) );
1008     if( p_list == NULL )
1009         return NULL;
1010
1011     p_list->i_count = i_count;
1012
1013     if( i_count == 0 )
1014     {
1015         p_list->p_values = NULL;
1016         return p_list;
1017     }
1018
1019     p_list->p_values = malloc( i_count * sizeof( vlc_value_t ) );
1020     if( p_list->p_values == NULL )
1021     {
1022         p_list->i_count = 0;
1023         return p_list;
1024     }
1025
1026     return p_list;
1027 }