2

It is possible to fetch the zend resources (zend_fetch_resource) without knowing the type of the fetching resource? If so, how?

Note: I am writing PHP extension.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
innocenat
  • 574
  • 7
  • 21

1 Answers1

2

Yes, you can.

zend_fetch_resource won't work because it receives the the types of resources that are acceptable and fails if the found one is not one of those.

Just use

void *zend_list_find(int id, int *type);

From the resource zval you can extract the id with Z_RESVAL(zval). The argument type will be filled with the type of the resource found.

However, I don't see much usage for this, except maybe to create a var_dump clone. The problem is that once you retrieve an arbitrary resource, what are you going to do with it?... In general, you know nothing about the returned data structure.

You can get the name of the resource directly with:

char *zend_rsrc_list_get_rsrc_type(int resource TSRMLS_DC);
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • Oh thank you. The use I need is that I am finishing the PECL Thread extensions (for CLI) because I want to create a server. But with server, I need to be able to pass resource between two thread, and I can't just copy zval from one thread to other, right? So I think I can fetch the resource into true global for storing, and when requested just register new zval resource with the same resource pointer (void*). – innocenat Aug 02 '10 at 08:54
  • @Nat That won't work. The resource list is a thread local variable. See http://en.wikipedia.org/wiki/Inter-process_communication – Artefacto Aug 02 '10 at 09:02
  • What I am going to do, is to fetch the resource in the source thread, store the resource (in void* form) in true global (not zend's TSRM), and put back in zval in destination thread. This should work, eh? IPC is not possible because the resource is not serializable, is it? – innocenat Aug 02 '10 at 09:25
  • @Nat You'd still have to synchronize access to the data pointed to by the resource pointer. – Artefacto Aug 02 '10 at 09:32
  • Yes, I use the lock mutex for both global resource storage and in-php resource using. Thank you very much for you help. – innocenat Aug 02 '10 at 09:39
  • @Artefacto on: "I don't see much usage for this". The case: The resource is created by a PHP extension, let call it **ext1**. Let **ext2** be another extension, which has a function to process the object in the resource, but does **not** know the ext1's le_ext1 obtained in ext1's initialization as le_ext1=zend_register_list_destructors_ex(..); So the **ext2** can't properly call zend_fetch_resource() and get the passed in "resource". Does this make sense ? – A. Genchev Jun 06 '20 at 22:50