root/eaccelerator/tags/0.9.5-beta2/ea_info.c

Revision 188, 13.0 kB (checked in by bart, 3 years ago)

Fixed bug #24

  • Property svn:keywords set to svn:eol-style
Line 
1 /*
2    +----------------------------------------------------------------------+
3    | eAccelerator project                                                 |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 2004 - 2006 eAccelerator                               |
6    | http://eaccelerator.net                                                      |
7    +----------------------------------------------------------------------+
8    | This program is free software; you can redistribute it and/or        |
9    | modify it under the terms of the GNU General Public License          |
10    | as published by the Free Software Foundation; either version 2       |
11    | of the License, or (at your option) any later version.               |
12    |                                                                      |
13    | This program is distributed in the hope that it will be useful,      |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
16    | GNU General Public License for more details.                         |
17    |                                                                      |
18    | You should have received a copy of the GNU General Public License    |
19    | along with this program; if not, write to the Free Software          |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston,               |
21    | MA  02111-1307, USA.                                                 |
22    |                                                                      |
23    | A copy is availble at http://www.gnu.org/copyleft/gpl.txt            |
24    +----------------------------------------------------------------------+
25    $Id: $
26 */
27
28 #include "eaccelerator.h"
29 #include "eaccelerator_version.h"
30 #include "ea_info.h"
31 #include "mm.h"
32 #include "cache.h"
33 #include "zend.h"
34 #include "fopen_wrappers.h"
35 #include <fcntl.h>
36
37 #ifndef O_BINARY
38 #  define O_BINARY 0
39 #endif
40
41 #ifdef WITH_EACCELERATOR_INFO
42
43 #define NOT_ADMIN_WARNING "This script isn't in the allowed_admin_path setting!"
44
45 extern eaccelerator_mm *eaccelerator_mm_instance;
46
47 /* {{{ isAdminAllowed(): check if the admin functions are allowed for the calling script */
48 static int isAdminAllowed(TSRMLS_D) {
49     const char *filename = zend_get_executed_filename(TSRMLS_C);
50     if (EAG(allowed_admin_path) && *EAG(allowed_admin_path)) {
51         char *path;
52         char *p;
53         char *next;
54
55         path = estrdup(EAG(allowed_admin_path));
56         p = path;
57
58         while (p && *p) {
59             next = strchr(p, DEFAULT_DIR_SEPARATOR);
60             if (next != NULL) {
61                 *next = '\0';
62                 ++next;
63             }
64            
65             if (!php_check_specific_open_basedir(p, filename TSRMLS_CC)) {
66                 efree(path);
67                 return 1;
68             }
69
70             p = next;
71         }
72         efree(path);
73         return 0;
74     }
75     return 0;
76 }
77 /* }}} */
78
79 /* {{{ PHP_FUNCTION(eaccelerator_caching): enable or disable caching */
80 PHP_FUNCTION(eaccelerator_caching)
81 {
82     zend_bool enable;
83    
84         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &enable) == FAILURE)
85                 return;
86
87     if (isAdminAllowed(TSRMLS_C)) {
88         EACCELERATOR_UNPROTECT();
89         if (enable) {
90             eaccelerator_mm_instance->enabled = 1;
91         } else {
92             eaccelerator_mm_instance->enabled = 0;
93         }
94         EACCELERATOR_PROTECT();
95     } else {
96         zend_error(E_WARNING, NOT_ADMIN_WARNING);
97     }
98    
99     RETURN_NULL();
100 }
101 /* }}} */
102
103 /* {{{ PHP_FUNCTION(eaccelerator_optimizer): enable or disable optimizer */
104 #ifdef WITH_EACCELERATOR_OPTIMIZER
105 PHP_FUNCTION(eaccelerator_optimizer)
106 {
107     zend_bool enable;
108    
109         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &enable) == FAILURE)
110                 return;
111
112     if (isAdminAllowed(TSRMLS_C)) {
113         EACCELERATOR_UNPROTECT();
114         if (enable) {
115             eaccelerator_mm_instance->optimizer_enabled = 1;
116         } else {
117             eaccelerator_mm_instance->optimizer_enabled = 0;
118         }
119         EACCELERATOR_PROTECT();
120     } else {
121         zend_error(E_WARNING, NOT_ADMIN_WARNING);
122     }
123    
124     RETURN_NULL();
125 }
126 #endif
127 /* }}} */
128
129 /* {{{ PHP_FUNCTION(eaccelerator_clean): remove all expired scripts and data from shared memory and disk cache */
130 PHP_FUNCTION(eaccelerator_clean)
131 {
132         time_t t;
133
134     if (!isAdminAllowed(TSRMLS_C)) {
135         zend_error(E_WARNING, NOT_ADMIN_WARNING);
136         RETURN_NULL();
137     }
138
139         t = time (0);
140
141         /* Remove expired scripts from shared memory */
142         eaccelerator_prune (t);
143
144         /* Remove expired keys (session data, content) from disk cache */
145 #ifndef ZEND_WIN32
146         /* clear file cache */
147         {
148                 DIR *dp;
149                 struct dirent *entry;
150                 char s[MAXPATHLEN];
151
152                 if ((dp = opendir (EAG (cache_dir))) != NULL) {
153                         while ((entry = readdir (dp)) != NULL) {
154                                 if (strstr(entry->d_name, "eaccelerator-user") == entry->d_name) {
155                                         int f;
156                                         strncpy (s, EAG (cache_dir), MAXPATHLEN - 1);
157                                         strlcat (s, "/", MAXPATHLEN);
158                                         strlcat (s, entry->d_name, MAXPATHLEN);
159                                         if ((f = open (s, O_RDONLY | O_BINARY)) > 0) {
160                                                 mm_file_header hdr;
161                                                 EACCELERATOR_FLOCK (f, LOCK_SH);
162                                                 if (read (f, &hdr, sizeof (hdr)) != sizeof (hdr)
163                                 || strncmp (hdr.magic, "EACCELERATOR",  8) != 0 || (hdr.mtime != 0 && hdr.mtime < t)) {
164                                                         EACCELERATOR_FLOCK (f, LOCK_UN);
165                                                         close (f);
166                                                         unlink (s);
167                                                 } else {
168                                                         EACCELERATOR_FLOCK (f, LOCK_UN);
169                                                         close (f);
170                                                 }
171                                         }
172                                 }
173                         }
174                         closedir (dp);
175                 }
176         }
177 #else
178         {
179                 HANDLE hList;
180                 TCHAR szDir[MAXPATHLEN];
181                 WIN32_FIND_DATA FileData;
182                 char s[MAXPATHLEN];
183
184                 snprintf (szDir, MAXPATHLEN, "%s\\eaccelerator-user*", EAG (cache_dir));
185
186                 if ((hList = FindFirstFile (szDir, &FileData)) != INVALID_HANDLE_VALUE) {
187                         do {
188                                 int f;
189                                 strncpy (s, EAG (cache_dir), MAXPATHLEN - 1);
190                                 strlcat (s, "\\", MAXPATHLEN);
191                                 strlcat (s, FileData.cFileName, MAXPATHLEN);
192                                 if ((f = open (s, O_RDONLY | O_BINARY)) > 0) {
193                                         mm_file_header hdr;
194                                         EACCELERATOR_FLOCK (f, LOCK_SH);
195                                         if (read (f, &hdr, sizeof (hdr)) != sizeof (hdr)
196                             || strncmp (hdr.magic, "EACCELERATOR", 8) != 0 || (hdr.mtime != 0 && hdr.mtime < t)) {
197                                                 EACCELERATOR_FLOCK (f, LOCK_UN);
198                                                 close (f);
199                                                 unlink (s);
200                                         } else {
201                                                 EACCELERATOR_FLOCK (f, LOCK_UN);
202                                                 close (f);
203                                         }
204                                 }
205                         }
206                         while (FindNextFile (hList, &FileData));
207                 }
208                 FindClose (hList);
209         }
210 #endif
211         /* Remove expired keys (session data, content) from shared memory */
212         eaccelerator_gc (TSRMLS_C);
213 }
214 /* }}} */
215
216 /* {{{ PHP_FUNCTION(eaccelerator_clear): remove all unused scripts and data from shared memory and disk cache */
217 PHP_FUNCTION(eaccelerator_clear)
218 {
219         unsigned int i;
220         mm_cache_entry *p;
221
222     if (!isAdminAllowed(TSRMLS_C)) {
223         zend_error(E_WARNING, NOT_ADMIN_WARNING);
224         RETURN_NULL();
225     }
226
227         EACCELERATOR_UNPROTECT ();
228         EACCELERATOR_LOCK_RW ();
229         for (i = 0; i < MM_HASH_SIZE; i++) {
230                 p = eaccelerator_mm_instance->hash[i];
231                 while (p != NULL) {
232                         mm_cache_entry *r = p;
233                         p = p->next;
234                         eaccelerator_mm_instance->hash_cnt--;
235                         if (r->use_cnt <= 0) {
236                                 eaccelerator_free_nolock (r);
237                         } else {
238                                 r->removed = 1;
239                                 r->next = eaccelerator_mm_instance->removed;
240                                 eaccelerator_mm_instance->removed = r;
241                                 eaccelerator_mm_instance->rem_cnt++;
242                         }
243                 }
244                 eaccelerator_mm_instance->hash[i] = NULL;
245         }
246         for (i = 0; i < MM_USER_HASH_SIZE; i++) {
247                 mm_user_cache_entry *p = eaccelerator_mm_instance->user_hash[i];
248                 while (p != NULL) {
249                         mm_user_cache_entry *r = p;
250                         p = p->next;
251                         eaccelerator_mm_instance->user_hash_cnt--;
252                         eaccelerator_free_nolock (r);
253                 }
254                 eaccelerator_mm_instance->user_hash[i] = NULL;
255         }
256         EACCELERATOR_UNLOCK_RW ();
257         EACCELERATOR_PROTECT ();
258 #ifndef ZEND_WIN32
259         /* clear file cache */
260         {
261                 DIR *dp;
262                 struct dirent *entry;
263                 char s[MAXPATHLEN];
264
265                 if ((dp = opendir (EAG (cache_dir))) != NULL) {
266                         while ((entry = readdir (dp)) != NULL) {
267                                 if (strstr (entry->d_name, "eaccelerator") == entry->d_name) {
268                                         strncpy (s, EAG (cache_dir), MAXPATHLEN - 1);
269                                         strlcat (s, "/", MAXPATHLEN);
270                                         strlcat (s, entry->d_name, MAXPATHLEN);
271                                         unlink (s);
272                                 }
273                         }
274                         closedir (dp);
275                 }
276         }
277 #else
278         {
279                 HANDLE hList;
280                 TCHAR szDir[MAXPATHLEN];
281                 WIN32_FIND_DATA FileData;
282                 char s[MAXPATHLEN];
283
284                 snprintf (szDir, MAXPATHLEN, "%s\\eaccelerator*", EAG (cache_dir));
285
286                 if ((hList = FindFirstFile (szDir, &FileData)) != INVALID_HANDLE_VALUE) {
287                         do {
288                                 strncpy (s, EAG (cache_dir), MAXPATHLEN - 1);
289                                 strlcat (s, "\\", MAXPATHLEN);
290                                 strlcat (s, FileData.cFileName, MAXPATHLEN);
291                                 unlink (s);
292                         }
293                         while (FindNextFile (hList, &FileData));
294                 }
295
296                 FindClose (hList);
297         }
298 #endif
299     RETURN_NULL();
300 }
301 /* }}} */
302
303 /* {{{ PHP_FUNCTION(eaccelerator_purge): remove all 'removed' scripts from shared memory */
304 PHP_FUNCTION(eaccelerator_purge)
305 {
306
307     if (!isAdminAllowed(TSRMLS_C)) {
308         zend_error(E_WARNING, NOT_ADMIN_WARNING);
309         RETURN_NULL();
310     }
311
312         if (eaccelerator_mm_instance != NULL) {
313                 mm_cache_entry *p, *q;
314                 EACCELERATOR_UNPROTECT();
315                 EACCELERATOR_LOCK_RW();
316                 p = eaccelerator_mm_instance->removed;
317                 eaccelerator_mm_instance->rem_cnt = 0;
318                 eaccelerator_mm_instance->removed = NULL;
319                 while (p != NULL) {
320                         q = p->next;
321                         eaccelerator_free_nolock(p);
322                         p = q;
323                 }
324                 EACCELERATOR_UNLOCK_RW();
325                 EACCELERATOR_PROTECT();
326         }
327     RETURN_NULL();
328 }
329 /* }}} */
330
331 /* {{{ PHP_FUNCTION(eaccelerator_info): get info about eaccelerator */
332 // returns info about eaccelerator as an array
333 // returhs the same as eaccelerator section in phpinfo
334 PHP_FUNCTION (eaccelerator_info)
335 {
336         unsigned int available;
337     char *shm, *sem;
338
339     shm = (char *)mm_shm_type();
340     sem = (char *)mm_sem_type();
341         available = mm_available (eaccelerator_mm_instance->mm);
342
343         // init return table
344         array_init(return_value);
345        
346         // put eaccelerator information
347         add_assoc_string(return_value, "version", EACCELERATOR_VERSION, 1);
348         add_assoc_string(return_value, "shm_type", shm, 1);
349     add_assoc_string(return_value, "sem_type", sem, 1);
350     add_assoc_string(return_value, "logo", EACCELERATOR_LOGO_GUID, 1);
351         add_assoc_bool(return_value, "cache", (EAG (enabled)
352                 && (eaccelerator_mm_instance != NULL)
353                 && eaccelerator_mm_instance->enabled) ? 1 : 0);
354         add_assoc_bool(return_value, "optimizer", (EAG (optimizer_enabled)
355                 && (eaccelerator_mm_instance != NULL)
356                 && eaccelerator_mm_instance->optimizer_enabled) ? 1 : 0);
357         add_assoc_long(return_value, "memorySize", eaccelerator_mm_instance->total);
358         add_assoc_long(return_value, "memoryAvailable", available);
359         add_assoc_long(return_value, "memoryAllocated", eaccelerator_mm_instance->total - available);
360         add_assoc_long(return_value, "cachedScripts", eaccelerator_mm_instance->hash_cnt);
361         add_assoc_long(return_value, "removedScripts", eaccelerator_mm_instance->rem_cnt);
362     add_assoc_long(return_value, "cachedKeys", eaccelerator_mm_instance->user_hash_cnt);
363
364         return;
365 }
366 /* }}} */
367
368 /* {{{ PHP_FUNCTION(eaccelerator_cached_scripts): Get an array with information about all cached scripts */
369 PHP_FUNCTION(eaccelerator_cached_scripts)
370 {
371     mm_cache_entry *p;
372     int i;
373
374     if (!isAdminAllowed(TSRMLS_C)) {
375         zend_error(E_WARNING, NOT_ADMIN_WARNING);
376         RETURN_NULL();
377     }
378
379     array_init(return_value);
380    
381     for (i = 0; i < MM_HASH_SIZE; i++) {
382         p = eaccelerator_mm_instance->hash[i];
383         while (p != NULL) {
384             zval *script;
385             MAKE_STD_ZVAL(script);
386             array_init(script);
387             add_assoc_string(script, "file", p->realfilename, 1);
388             add_assoc_long(script, "mtime", p->mtime);
389             add_assoc_long(script, "size", p->size);
390             add_assoc_long(script, "reloads", p->nreloads);
391             add_assoc_long(script, "usecount", p->use_cnt);
392             add_assoc_long(script, "hits", p->nhits);
393             add_next_index_zval(return_value, script);
394             p = p->next;
395         }
396     }
397     return;
398 }
399 /* }}} */
400
401 /* {{{ PHP_FUNCTION(eaccelerator_removed_scripts): Get a list of removed scripts */
402 PHP_FUNCTION(eaccelerator_removed_scripts)
403 {
404     mm_cache_entry *p;
405     zval *script;
406
407     if (!isAdminAllowed(TSRMLS_C)) {
408         zend_error(E_WARNING, NOT_ADMIN_WARNING);
409         RETURN_NULL();
410     }
411
412     MAKE_STD_ZVAL(script);
413     array_init(return_value);
414
415     p = eaccelerator_mm_instance->removed;
416     while (p != NULL) {
417         array_init(script);
418         add_assoc_string(script, "file", p->realfilename, 1);
419         add_assoc_long(script, "mtime", p->mtime);
420         add_assoc_long(script, "size", p->size);
421         add_assoc_long(script, "reloads", p->nreloads);
422         add_assoc_long(script, "usecount", p->use_cnt);
423         add_assoc_long(script, "hits", p->nhits);
424         add_next_index_zval(return_value, script);
425         p = p->next;
426     }
427     return;
428 }
429 /* }}} */
430
431 /* {{{ PHP_FUNCTION(eaccelerator_list_keys): returns list of keys in shared memory that matches actual hostname or namespace */
432 PHP_FUNCTION(eaccelerator_list_keys)
433 {
434         if (eaccelerator_list_keys(return_value TSRMLS_CC)) {
435                 return;
436         } else {
437         RETURN_NULL ();
438         }
439 }
440 /* }}} */
441
442 #endif
443
444 /*
445  * Local variables:
446  * tab-width: 4
447  * c-basic-offset: 4
448  * End:
449  * vim600: noet sw=4 ts=4 fdm=marker
450  * vim<600: noet sw=4 ts=4
451  */
452
Note: See TracBrowser for help on using the browser.