clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name adpeval.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/home/isvv/naviserver/nsd -resource-dir /usr/local/lib/clang/15.0.0 -D _FORTIFY_SOURCE=2 -D NDEBUG -D SYSTEM_MALLOC -I ../include -I /usr/include/tcl8.6 -D HAVE_CONFIG_H -internal-isystem /usr/local/lib/clang/15.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/11/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=c99 -fdebug-compilation-dir=/home/isvv/naviserver/nsd -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-checker alpha -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-07-23-130959-11103-1 -x c adpeval.c
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | #include "nsd.h" |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | typedef struct AdpCache { |
46 | int refcnt; |
47 | Ns_Time expires; |
48 | AdpCode code; |
49 | } AdpCache; |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | typedef struct Page { |
57 | NsServer *servPtr; |
58 | Tcl_HashEntry *hPtr; |
59 | time_t mtime; |
60 | off_t size; |
61 | dev_t dev; |
62 | ino_t ino; |
63 | unsigned int flags; |
64 | int refcnt; |
65 | int evals; |
66 | int cacheGen; |
67 | AdpCache *cachePtr; |
68 | AdpCode code; |
69 | bool locked; |
70 | } Page; |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | typedef struct Objs { |
78 | int nobjs; |
79 | Tcl_Obj *objs[1]; |
80 | } Objs; |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | typedef struct InterpPage { |
89 | Page *pagePtr; |
90 | Objs *objs; |
91 | int cacheGen; |
92 | Objs *cacheObjs; |
93 | } InterpPage; |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | static Page *ParseFile(const NsInterp *itPtr, const char *file, struct stat *stPtr, unsigned int flags) |
100 | NS_GNUC_NONNULL(1) NS_GNUC_NONNULL(2) NS_GNUC_NONNULL(3); |
101 | |
102 | static int AdpEval(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *resvar) |
103 | NS_GNUC_NONNULL(1); |
104 | |
105 | static int AdpExec(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *file, |
106 | const AdpCode *codePtr, Objs *objsPtr, Tcl_DString *outputPtr, |
107 | const struct stat *stPtr) |
108 | NS_GNUC_NONNULL(1) NS_GNUC_NONNULL(5) NS_GNUC_NONNULL(7); |
109 | |
110 | static int AdpSource(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *file, |
111 | const Ns_Time *expiresPtr, Tcl_DString *outputPtr) |
112 | NS_GNUC_NONNULL(1) NS_GNUC_NONNULL(4) NS_GNUC_NONNULL(6); |
113 | |
114 | static int AdpDebug(const NsInterp *itPtr, const char *ptr, int len, int nscript) |
115 | NS_GNUC_NONNULL(1) NS_GNUC_NONNULL(2); |
116 | |
117 | static void DecrCache(AdpCache *cachePtr) |
118 | NS_GNUC_NONNULL(1); |
119 | |
120 | static Objs *AllocObjs(int nobjs); |
121 | |
122 | static void FreeObjs(Objs *objsPtr) |
123 | NS_GNUC_NONNULL(1); |
124 | |
125 | static void AdpTrace(const NsInterp *itPtr, const char *ptr, int len) |
126 | NS_GNUC_NONNULL(1) NS_GNUC_NONNULL(2); |
127 | |
128 | static Ns_Callback FreeInterpPage; |
129 | static Ns_ServerInitProc ConfigServerAdp; |
130 | |
131 |
|
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | void |
149 | NsConfigAdp(void) |
150 | { |
151 | NsRegisterServerInit(ConfigServerAdp); |
152 | } |
153 | |
154 | static Ns_ReturnCode |
155 | ConfigServerAdp(const char *server) |
156 | { |
157 | NsServer *servPtr = NsGetServer(server); |
158 | const char *path; |
159 | |
160 | path = Ns_ConfigSectionPath(NULL, server, NULL, "adp", (char *)0L); |
161 | |
162 | |
163 | |
164 | |
165 | |
166 | Tcl_InitHashTable(&servPtr->adp.pages, TCL_STRING_KEYS); |
167 | Tcl_InitHashTable(&servPtr->adp.tags, TCL_STRING_KEYS); |
168 | |
169 | Ns_CondInit(&servPtr->adp.pagecond); |
170 | |
171 | Ns_MutexInit(&servPtr->adp.pagelock); |
172 | Ns_MutexSetName2(&servPtr->adp.pagelock, "ns:adp:pages", server); |
173 | |
174 | Ns_RWLockInit(&servPtr->adp.taglock); |
175 | Ns_RWLockSetName2(&servPtr->adp.taglock, "rw:adp:tags", server); |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | servPtr->adp.errorpage = Ns_ConfigString(path, "errorpage", NULL); |
182 | servPtr->adp.startpage = Ns_ConfigString(path, "startpage", NULL); |
183 | servPtr->adp.debuginit = Ns_ConfigString(path, "debuginit", "ns_adp_debuginit"); |
184 | servPtr->adp.tracesize = Ns_ConfigInt(path, "tracesize", 40); |
185 | servPtr->adp.cachesize = (size_t)Ns_ConfigMemUnitRange(path, "cachesize", "5MB", 5000 * 1024, |
186 | 1000 * 1024, INT_MAX); |
187 | servPtr->adp.bufsize = (size_t)Ns_ConfigMemUnitRange(path, "bufsize", "1MB", 1024 * 1000, |
188 | 100 * 1024, INT_MAX); |
189 | servPtr->adp.defaultExtension = Ns_ConfigString(path, "defaultextension", NULL); |
190 | |
191 | servPtr->adp.flags = 0u; |
192 | (void) Ns_ConfigFlag(path, "cache", ADP_CACHE, 0, &servPtr->adp.flags); |
193 | (void) Ns_ConfigFlag(path, "stream", ADP_STREAM, 0, &servPtr->adp.flags); |
194 | (void) Ns_ConfigFlag(path, "enableexpire", ADP_EXPIRE, 0, &servPtr->adp.flags); |
195 | (void) Ns_ConfigFlag(path, "enabledebug", ADP_DEBUG, 0, &servPtr->adp.flags); |
196 | (void) Ns_ConfigFlag(path, "safeeval", ADP_SAFE, 0, &servPtr->adp.flags); |
197 | (void) Ns_ConfigFlag(path, "singlescript", ADP_SINGLE, 0, &servPtr->adp.flags); |
198 | (void) Ns_ConfigFlag(path, "trace", ADP_TRACE, 0, &servPtr->adp.flags); |
199 | (void) Ns_ConfigFlag(path, "detailerror", ADP_DETAIL, 1, &servPtr->adp.flags); |
200 | (void) Ns_ConfigFlag(path, "stricterror", ADP_STRICT, 0, &servPtr->adp.flags); |
201 | (void) Ns_ConfigFlag(path, "displayerror", ADP_DISPLAY, 0, &servPtr->adp.flags); |
202 | (void) Ns_ConfigFlag(path, "trimspace", ADP_TRIM, 0, &servPtr->adp.flags); |
203 | (void) Ns_ConfigFlag(path, "autoabort", ADP_AUTOABORT, 1, &servPtr->adp.flags); |
204 | |
205 | return NS_OK; |
206 | } |
207 | |
208 |
|
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | int |
228 | NsAdpEval(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *resvar) |
229 | { |
230 | NS_NONNULL_ASSERT(itPtr != NULL); |
231 | |
232 | return AdpEval(itPtr, objc, objv, resvar); |
233 | } |
234 | |
235 | int |
236 | NsAdpSource(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *resvar) |
237 | { |
238 | NS_NONNULL_ASSERT(itPtr != NULL); |
239 | |
240 | itPtr->adp.flags |= ADP_ADPFILE; |
241 | return AdpEval(itPtr, objc, objv, resvar); |
| |
242 | } |
243 | |
244 | static int |
245 | AdpEval(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *resvar) |
246 | { |
247 | Tcl_Interp *interp; |
248 | AdpCode code; |
249 | Tcl_DString output; |
250 | int result; |
251 | char *obj0; |
252 | |
253 | NS_NONNULL_ASSERT(itPtr != NULL); |
254 | |
255 | interp = itPtr->interp; |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | Tcl_DStringInit(&output); |
262 | obj0 = Tcl_GetString(objv[0]); |
263 | if ((itPtr->adp.flags & ADP_ADPFILE) != 0u) { |
| 2 | | Assuming the condition is true | |
|
| |
264 | result = AdpSource(itPtr, objc, objv, obj0, NULL, &output); |
| |
265 | } else { |
266 | NsAdpParse(&code, itPtr->servPtr, obj0, itPtr->adp.flags, NULL); |
267 | result = AdpExec(itPtr, objc, objv, NULL, &code, NULL, &output, NULL); |
268 | NsAdpFreeCode(&code); |
269 | } |
270 | |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | if (result == TCL_OK) { |
277 | Tcl_Obj *objPtr; |
278 | |
279 | if (resvar != NULL) { |
280 | objPtr = Tcl_GetObjResult(interp); |
281 | if (Tcl_SetVar2Ex(interp, resvar, NULL, objPtr, TCL_LEAVE_ERR_MSG) == NULL) { |
282 | result = TCL_ERROR; |
283 | } |
284 | } |
285 | if (result == TCL_OK) { |
286 | objPtr = Tcl_NewStringObj(output.string, output.length); |
287 | Tcl_SetObjResult(interp, objPtr); |
288 | } |
289 | } |
290 | Tcl_DStringFree(&output); |
291 | |
292 | return result; |
293 | } |
294 | |
295 |
|
296 | |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | int |
313 | NsAdpInclude(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *file, const Ns_Time *expiresPtr) |
314 | { |
315 | Ns_DString *outputPtr; |
316 | |
317 | NS_NONNULL_ASSERT(itPtr != NULL); |
318 | NS_NONNULL_ASSERT(file != NULL); |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | if (itPtr->adp.framePtr != NULL) { |
326 | outputPtr = itPtr->adp.framePtr->outputPtr; |
327 | } else { |
328 | outputPtr = &itPtr->adp.output; |
329 | } |
330 | return AdpSource(itPtr, objc, objv, file, expiresPtr, outputPtr); |
331 | } |
332 | |
333 |
|
334 | |
335 | |
336 | |
337 | |
338 | |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | |
347 | |
348 | |
349 | |
350 | void |
351 | NsAdpInit(NsInterp *itPtr) |
352 | { |
353 | NS_NONNULL_ASSERT(itPtr != NULL); |
354 | |
355 | Tcl_DStringInit(&itPtr->adp.output); |
356 | NsAdpReset(itPtr); |
357 | } |
358 | |
359 | void |
360 | NsAdpFree(NsInterp *itPtr) |
361 | { |
362 | NS_NONNULL_ASSERT(itPtr != NULL); |
363 | |
364 | if (itPtr->adp.cache != NULL) { |
365 | Ns_CacheDestroy(itPtr->adp.cache); |
366 | itPtr->adp.cache = NULL; |
367 | } |
368 | Tcl_DStringFree(&itPtr->adp.output); |
369 | } |
370 | |
371 |
|
372 | |
373 | |
374 | |
375 | |
376 | |
377 | |
378 | |
379 | |
380 | |
381 | |
382 | |
383 | |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | void |
390 | NsAdpReset(NsInterp *itPtr) |
391 | { |
392 | NS_NONNULL_ASSERT(itPtr != NULL); |
393 | |
394 | itPtr->adp.exception = ADP_OK; |
395 | itPtr->adp.debugLevel = 0; |
396 | itPtr->adp.debugInit = 0; |
397 | itPtr->adp.debugFile = NULL; |
398 | itPtr->adp.chan = NULL; |
399 | itPtr->adp.conn = NULL; |
400 | if (itPtr->servPtr != NULL) { |
401 | itPtr->adp.bufsize = itPtr->servPtr->adp.bufsize; |
402 | itPtr->adp.flags = itPtr->servPtr->adp.flags; |
403 | } else { |
404 | itPtr->adp.bufsize = 1024u * 1000u; |
405 | itPtr->adp.flags = 0u; |
406 | } |
407 | Tcl_DStringSetLength(&itPtr->adp.output, 0); |
408 | } |
409 |
|
410 | |
411 | |
412 | |
413 | |
414 | |
415 | |
416 | |
417 | |
418 | |
419 | |
420 | |
421 | |
422 | |
423 | |
424 | |
425 | |
426 | |
427 | |
428 | |
429 | static int |
430 | AdpSource(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *file, |
431 | const Ns_Time *expiresPtr, Tcl_DString *outputPtr) |
432 | { |
433 | NsServer *servPtr; |
434 | Tcl_Interp *interp; |
435 | Tcl_HashEntry *hPtr; |
436 | struct stat st; |
437 | Ns_DString tmp, path; |
438 | InterpPage *ipagePtr = NULL; |
439 | Page *pagePtr = NULL; |
440 | Ns_Time now; |
441 | int isNew; |
442 | const char *p; |
443 | int result = TCL_ERROR; |
444 | |
445 | NS_NONNULL_ASSERT(itPtr != NULL); |
446 | NS_NONNULL_ASSERT(file != NULL); |
447 | NS_NONNULL_ASSERT(outputPtr != NULL); |
448 | |
449 | servPtr = itPtr->servPtr; |
450 | interp = itPtr->interp; |
451 | |
452 | Ns_DStringInit(&tmp); |
453 | Ns_DStringInit(&path); |
454 | |
455 | |
456 | |
457 | |
458 | |
459 | if (Ns_PathIsAbsolute(file) == NS_FALSE) { |
| 5 | | Assuming the condition is false | |
|
| |
460 | if (itPtr->adp.cwd == NULL) { |
461 | file = Ns_PagePath(&tmp, servPtr->server, file, (char *)0L); |
462 | } else { |
463 | file = Ns_MakePath(&tmp, itPtr->adp.cwd, file, (char *)0L); |
464 | } |
465 | } |
466 | file = Ns_NormalizePath(&path, file); |
467 | Ns_DStringSetLength(&tmp, 0); |
468 | |
469 | |
470 | |
471 | |
472 | |
473 | if (itPtr->adp.debugLevel > 0) { |
| 7 | | Assuming field 'debugLevel' is <= 0 | |
|
474 | ++itPtr->adp.debugLevel; |
475 | } else if ((itPtr->conn != NULL) |
| 8 | | Assuming field 'conn' is equal to NULL | |
|
476 | && ((itPtr->adp.flags & ADP_DEBUG) != 0u) |
477 | && itPtr->adp.debugFile != NULL |
478 | && (p = strrchr(file, INTCHAR('/'))) != NULL |
479 | && Tcl_StringMatch(p+1, itPtr->adp.debugFile) != 0) { |
480 | const Ns_Set *hdrs; |
481 | const char *host, *port, *procs; |
482 | |
483 | hdrs = Ns_ConnGetQuery(interp, itPtr->conn, NULL, NULL); |
484 | host = Ns_SetIGet(hdrs, "dhost"); |
485 | port = Ns_SetIGet(hdrs, "dport"); |
486 | procs = Ns_SetIGet(hdrs, "dprocs"); |
487 | if (NsAdpDebug(itPtr, host, port, procs) != TCL_OK) { |
488 | (void) Ns_ConnReturnNotice(itPtr->conn, 200, "Debug Init Failed", |
489 | Tcl_GetStringResult(interp)); |
490 | itPtr->adp.exception = ADP_ABORT; |
491 | goto done; |
492 | } |
493 | } |
494 | |
495 | if (itPtr->adp.cache == NULL) { |
| 9 | | Assuming field 'cache' is not equal to NULL | |
|
| |
496 | Ns_DStringPrintf(&tmp, "nsadp:%p", (void *)itPtr); |
497 | itPtr->adp.cache = Ns_CacheCreateSz(tmp.string, TCL_STRING_KEYS, |
498 | itPtr->servPtr->adp.cachesize, FreeInterpPage); |
499 | Ns_DStringSetLength(&tmp, 0); |
500 | } |
501 | |
502 | |
503 | |
504 | |
505 | |
506 | if (stat(file, &st) != 0) { |
| 11 | | Assuming the condition is false | |
|
| |
507 | Ns_TclPrintfResult(interp, "could not stat \"%s\": %s", |
508 | file, Tcl_PosixError(interp)); |
509 | } else if (!S_ISREG(st.st_mode)) { |
| 13 | | Assuming the condition is true | |
|
| |
510 | Ns_TclPrintfResult(interp, "not an ordinary file: %s", file); |
511 | } else { |
512 | Ns_Entry *ePtr; |
513 | |
514 | |
515 | |
516 | |
517 | |
518 | ePtr = Ns_CacheFindEntry(itPtr->adp.cache, file); |
519 | if (ePtr != NULL) { |
| 15 | | Assuming 'ePtr' is equal to NULL | |
|
| |
520 | ipagePtr = Ns_CacheGetValue(ePtr); |
521 | if (ipagePtr->pagePtr->mtime != st.st_mtime |
522 | || ipagePtr->pagePtr->size != st.st_size |
523 | || ipagePtr->pagePtr->dev != st.st_dev |
524 | || ipagePtr->pagePtr->ino != st.st_ino |
525 | || ipagePtr->pagePtr->flags != itPtr->adp.flags) { |
526 | Ns_CacheFlushEntry(ePtr); |
527 | ipagePtr = NULL; |
528 | } |
529 | } |
530 | if (ipagePtr == NULL) { |
| |
531 | |
532 | |
533 | |
534 | |
535 | |
536 | Ns_MutexLock(&servPtr->adp.pagelock); |
537 | hPtr = Tcl_CreateHashEntry(&servPtr->adp.pages, file, &isNew); |
538 | while (isNew == 0 && (pagePtr = Tcl_GetHashValue(hPtr)) == NULL) { |
| 18 | | Assuming 'isNew' is not equal to 0 | |
|
539 | |
540 | Ns_CondWait(&servPtr->adp.pagecond, &servPtr->adp.pagelock); |
541 | hPtr = Tcl_CreateHashEntry(&servPtr->adp.pages, file, &isNew); |
542 | } |
543 | if (isNew == 0 && (pagePtr->mtime != st.st_mtime |
544 | || pagePtr->size != st.st_size |
545 | || pagePtr->dev != st.st_dev |
546 | || pagePtr->ino != st.st_ino |
547 | || pagePtr->flags != itPtr->adp.flags)) { |
548 | |
549 | Tcl_SetHashValue(hPtr, NULL); |
550 | pagePtr->hPtr = NULL; |
551 | isNew = 1; |
552 | } |
553 | if (isNew != 0) { |
| |
554 | Ns_MutexUnlock(&servPtr->adp.pagelock); |
555 | pagePtr = ParseFile(itPtr, file, &st, itPtr->adp.flags); |
556 | Ns_MutexLock(&servPtr->adp.pagelock); |
557 | if (pagePtr == NULL) { |
| |
558 | Tcl_DeleteHashEntry(hPtr); |
559 | } else { |
560 | pagePtr->hPtr = hPtr; |
561 | Tcl_SetHashValue(hPtr, pagePtr); |
562 | } |
563 | Ns_CondBroadcast(&servPtr->adp.pagecond); |
564 | } |
565 | if (pagePtr != NULL) { |
| |
566 | ++pagePtr->refcnt; |
567 | } |
568 | Ns_MutexUnlock(&servPtr->adp.pagelock); |
569 | if (pagePtr != NULL) { |
| |
570 | ipagePtr = ns_malloc(sizeof(InterpPage)); |
571 | ipagePtr->pagePtr = pagePtr; |
572 | ipagePtr->cacheGen = 0; |
573 | ipagePtr->objs = AllocObjs(pagePtr->code.nscripts); |
574 | ipagePtr->cacheObjs = NULL; |
575 | ePtr = Ns_CacheCreateEntry(itPtr->adp.cache, file, &isNew); |
576 | if (isNew == 0) { |
| 23 | | Assuming 'isNew' is not equal to 0 | |
|
| |
577 | Ns_CacheUnsetValue(ePtr); |
578 | } |
579 | Ns_CacheSetValueSz(ePtr, ipagePtr, |
580 | (size_t) ipagePtr->pagePtr->size); |
581 | } |
582 | } |
583 | } |
584 | |
585 | |
586 | |
587 | |
588 | |
589 | if (ipagePtr != NULL) { |
| |
590 | const AdpCode *codePtr; |
591 | Objs *objsPtr; |
592 | int cacheGen = 0; |
593 | AdpCache *cachePtr; |
594 | |
595 | pagePtr = ipagePtr->pagePtr; |
596 | if (expiresPtr == NULL || (itPtr->adp.flags & ADP_CACHE) == 0u) { |
597 | cachePtr = NULL; |
598 | } else { |
599 | |
600 | Ns_MutexLock(&servPtr->adp.pagelock); |
601 | |
602 | |
603 | |
604 | |
605 | |
606 | while ((cachePtr = pagePtr->cachePtr) == NULL && pagePtr->locked) { |
607 | Ns_CondWait(&servPtr->adp.pagecond, &servPtr->adp.pagelock); |
608 | } |
609 | |
610 | |
611 | |
612 | |
613 | |
614 | if (cachePtr != NULL && ! pagePtr->locked) { |
615 | Ns_GetTime(&now); |
616 | if (Ns_DiffTime(&cachePtr->expires, &now, NULL) < 0) { |
617 | pagePtr->locked = NS_TRUE; |
618 | cachePtr = NULL; |
619 | } |
620 | } |
621 | |
622 | |
623 | |
624 | |
625 | |
626 | if (cachePtr == NULL) { |
627 | Ns_MutexUnlock(&servPtr->adp.pagelock); |
628 | codePtr = &pagePtr->code; |
629 | ++itPtr->adp.refresh; |
630 | result = AdpExec(itPtr, objc, objv, file, codePtr, ipagePtr->objs, |
631 | &tmp, &st); |
632 | --itPtr->adp.refresh; |
633 | |
634 | |
635 | |
636 | |
637 | |
638 | |
639 | if (result == TCL_OK && (itPtr->adp.flags & ADP_CACHE) != 0u) { |
640 | cachePtr = ns_malloc(sizeof(AdpCache)); |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | |
650 | |
651 | |
652 | |
653 | |
654 | NsAdpParse(&cachePtr->code, itPtr->servPtr, tmp.string, |
655 | itPtr->adp.flags & ~ADP_TCLFILE, file); |
656 | Ns_GetTime(&cachePtr->expires); |
657 | Ns_IncrTime(&cachePtr->expires, expiresPtr->sec, expiresPtr->usec); |
658 | cachePtr->refcnt = 1; |
659 | } |
660 | Ns_DStringSetLength(&tmp, 0); |
661 | Ns_MutexLock(&servPtr->adp.pagelock); |
662 | if (cachePtr != NULL) { |
663 | if (pagePtr->cachePtr != NULL) { |
664 | DecrCache(pagePtr->cachePtr); |
665 | } |
666 | ++pagePtr->cacheGen; |
667 | pagePtr->cachePtr = cachePtr; |
668 | } |
669 | pagePtr->locked = NS_FALSE; |
670 | Ns_CondBroadcast(&servPtr->adp.pagecond); |
671 | } |
672 | cacheGen = pagePtr->cacheGen; |
673 | if (cachePtr != NULL) { |
674 | ++cachePtr->refcnt; |
675 | } |
676 | Ns_MutexUnlock(&servPtr->adp.pagelock); |
677 | } |
678 | if (cachePtr == NULL) { |
| |
679 | codePtr = &pagePtr->code; |
680 | objsPtr = ipagePtr->objs; |
681 | } else { |
682 | codePtr = &cachePtr->code; |
683 | if (ipagePtr->cacheObjs != NULL && cacheGen != ipagePtr->cacheGen) { |
684 | FreeObjs(ipagePtr->cacheObjs); |
685 | ipagePtr->cacheObjs = NULL; |
686 | } |
687 | if (ipagePtr->cacheObjs == NULL) { |
688 | ipagePtr->cacheObjs = AllocObjs(AdpCodeScripts(codePtr)); |
689 | ipagePtr->cacheGen = cacheGen; |
690 | } |
691 | objsPtr = ipagePtr->cacheObjs; |
692 | } |
693 | result = AdpExec(itPtr, objc, objv, file, codePtr, objsPtr, outputPtr, &st); |
| |
694 | Ns_MutexLock(&servPtr->adp.pagelock); |
695 | ++ipagePtr->pagePtr->evals; |
696 | if (cachePtr != NULL) { |
697 | DecrCache(cachePtr); |
698 | } |
699 | Ns_MutexUnlock(&servPtr->adp.pagelock); |
700 | } |
701 | if (itPtr->adp.debugLevel > 0) { |
702 | --itPtr->adp.debugLevel; |
703 | } |
704 | |
705 | done: |
706 | Ns_DStringFree(&path); |
707 | Ns_DStringFree(&tmp); |
708 | |
709 | return result; |
710 | } |
711 | |
712 |
|
713 | |
714 | |
715 | |
716 | |
717 | |
718 | |
719 | |
720 | |
721 | |
722 | |
723 | |
724 | |
725 | |
726 | |
727 | |
728 | |
729 | |
730 | |
731 | int |
732 | NsAdpDebug(NsInterp *itPtr, const char *host, const char *port, const char *procs) |
733 | { |
734 | Tcl_Interp *interp; |
735 | Tcl_DString ds; |
736 | int result; |
737 | |
738 | NS_NONNULL_ASSERT(itPtr != NULL); |
739 | |
740 | interp = itPtr->interp; |
741 | result = TCL_OK; |
742 | |
743 | if (itPtr->adp.debugInit == 0) { |
744 | itPtr->deleteInterp = NS_TRUE; |
745 | Tcl_DStringInit(&ds); |
746 | Tcl_DStringAppendElement(&ds, itPtr->servPtr->adp.debuginit); |
747 | Tcl_DStringAppendElement(&ds, (procs != NULL) ? procs : NS_EMPTY_STRING); |
748 | Tcl_DStringAppendElement(&ds, (host != NULL) ? host : NS_EMPTY_STRING); |
749 | Tcl_DStringAppendElement(&ds, (port != NULL) ? port : NS_EMPTY_STRING); |
750 | result = Tcl_EvalEx(interp, ds.string, ds.length, 0); |
751 | Tcl_DStringFree(&ds); |
752 | if (result != TCL_OK) { |
753 | NsAdpLogError(itPtr); |
754 | result = TCL_ERROR; |
755 | |
756 | } else { |
757 | |
758 | |
759 | |
760 | |
761 | |
762 | if (Tcl_LinkVar(interp, "ns_adp_output", |
763 | (char *) &itPtr->adp.output.string, |
764 | TCL_LINK_STRING | TCL_LINK_READ_ONLY) != TCL_OK) { |
765 | NsAdpLogError(itPtr); |
766 | } |
767 | |
768 | itPtr->adp.debugInit = 1; |
769 | itPtr->adp.debugLevel = 1; |
770 | } |
771 | } |
772 | return result; |
773 | } |
774 | |
775 |
|
776 | |
777 | |
778 | |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | |
786 | |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | int |
794 | NsTclAdpStatsObjCmd(ClientData clientData, Tcl_Interp *interp, |
795 | int UNUSED(objc), Tcl_Obj *const* UNUSED(objv)) |
796 | { |
797 | const NsInterp *itPtr = clientData; |
798 | NsServer *servPtr = itPtr->servPtr; |
799 | Ns_DString ds; |
800 | Tcl_HashSearch search; |
801 | Tcl_HashEntry *hPtr; |
802 | |
803 | Ns_DStringInit(&ds); |
804 | |
805 | Ns_MutexLock(&servPtr->adp.pagelock); |
806 | hPtr = Tcl_FirstHashEntry(&servPtr->adp.pages, &search); |
807 | while (hPtr != NULL) { |
808 | const Page *pagePtr = Tcl_GetHashValue(hPtr); |
809 | char *file = Tcl_GetHashKey(&servPtr->adp.pages, hPtr); |
810 | |
811 | Ns_DStringPrintf(&ds, "{%s} " |
812 | "{dev %" PRIu64 " ino %" PRIu64 " mtime %" PRIu64 " " |
813 | "refcnt %d evals %d size %" PROTd" blocks %d scripts %d} ", |
814 | file, |
815 | (uint64_t) pagePtr->dev, (uint64_t) pagePtr->ino, (uint64_t) pagePtr->mtime, |
816 | pagePtr->refcnt, pagePtr->evals, pagePtr->size, |
817 | pagePtr->code.nblocks, pagePtr->code.nscripts); |
818 | hPtr = Tcl_NextHashEntry(&search); |
819 | } |
820 | Ns_MutexUnlock(&servPtr->adp.pagelock); |
821 | |
822 | Tcl_DStringResult(interp, &ds); |
823 | |
824 | return TCL_OK; |
825 | } |
826 | |
827 |
|
828 | |
829 | |
830 | |
831 | |
832 | |
833 | |
834 | |
835 | |
836 | |
837 | |
838 | |
839 | |
840 | |
841 | |
842 | |
843 | |
844 | |
845 | static Page * |
846 | ParseFile(const NsInterp *itPtr, const char *file, struct stat *stPtr, unsigned int flags) |
847 | { |
848 | Tcl_Interp *interp; |
849 | Tcl_Encoding encoding; |
850 | Tcl_DString utf; |
851 | char *buf = NULL; |
852 | int fd, tries = 0; |
853 | size_t size = 0u; |
854 | ssize_t n; |
855 | Page *pagePtr = NULL; |
856 | |
857 | NS_NONNULL_ASSERT(itPtr != NULL); |
858 | NS_NONNULL_ASSERT(file != NULL); |
859 | NS_NONNULL_ASSERT(stPtr != NULL); |
860 | |
861 | interp = itPtr->interp; |
862 | |
863 | fd = ns_open(file, O_RDONLY | O_BINARY | O_CLOEXEC, 0); |
864 | if (fd < 0) { |
865 | Ns_TclPrintfResult(interp, "could not open \"%s\": %s", |
866 | file, Tcl_PosixError(interp)); |
867 | return NULL; |
868 | } |
869 | |
870 | do { |
871 | |
872 | |
873 | |
874 | |
875 | |
876 | if (fstat(fd, stPtr) != 0) { |
877 | Ns_TclPrintfResult(interp, "could not fstat \"%s\": %s", |
878 | file, Tcl_PosixError(interp)); |
879 | goto done; |
880 | } |
881 | size = (size_t)stPtr->st_size; |
882 | buf = ns_realloc(buf, size + 1u); |
883 | |
884 | |
885 | |
886 | |
887 | |
888 | n = ns_read(fd, buf, size + 1u); |
889 | if (n < 0) { |
890 | Ns_TclPrintfResult(interp, "could not read \"%s\": %s", |
891 | file, Tcl_PosixError(interp)); |
892 | goto done; |
893 | } |
894 | if ((size_t)n != size) { |
895 | |
896 | |
897 | |
898 | |
899 | if (ns_lseek(fd, (off_t) 0, SEEK_SET) != 0) { |
900 | Ns_TclPrintfResult(interp, "could not lseek \"%s\": %s", |
901 | file, Tcl_PosixError(interp)); |
902 | goto done; |
903 | } |
904 | Ns_ThreadYield(); |
905 | } |
906 | } while ((size_t)n != size && ++tries < 10); |
907 | |
908 | if ((size_t)n != size) { |
909 | Ns_TclPrintfResult(interp, "inconsistent file: %s", file); |
910 | } else { |
911 | char *page; |
912 | |
913 | buf[n] = '\0'; |
914 | Tcl_DStringInit(&utf); |
915 | encoding = Ns_GetFileEncoding(file); |
916 | if (encoding == NULL) { |
917 | page = buf; |
918 | } else { |
919 | page = Tcl_ExternalToUtfDString(encoding, buf, (int)n, &utf); |
920 | } |
921 | pagePtr = ns_malloc(sizeof(Page)); |
922 | pagePtr->servPtr = itPtr->servPtr; |
923 | pagePtr->flags = flags; |
924 | pagePtr->refcnt = 0; |
925 | pagePtr->evals = 0; |
926 | pagePtr->locked = NS_FALSE; |
927 | pagePtr->cacheGen = 0; |
928 | pagePtr->cachePtr = NULL; |
929 | pagePtr->mtime = stPtr->st_mtime; |
930 | pagePtr->size = stPtr->st_size; |
931 | pagePtr->dev = stPtr->st_dev; |
932 | pagePtr->ino = stPtr->st_ino; |
933 | NsAdpParse(&pagePtr->code, itPtr->servPtr, page, flags, file); |
934 | Tcl_DStringFree(&utf); |
935 | } |
936 | |
937 | done: |
938 | ns_free(buf); |
939 | (void) ns_close(fd); |
940 | |
941 | return pagePtr; |
942 | } |
943 | |
944 |
|
945 | |
946 | |
947 | |
948 | |
949 | |
950 | |
951 | |
952 | |
953 | |
954 | |
955 | |
956 | |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | void |
963 | NsAdpLogError(NsInterp *itPtr) |
964 | { |
965 | Tcl_Interp *interp; |
966 | const Ns_Conn *conn; |
967 | Ns_DString ds; |
968 | const AdpFrame *framePtr; |
969 | int len; |
970 | const char *err, *adp, *inc, *dot; |
971 | |
972 | NS_NONNULL_ASSERT(itPtr != NULL); |
973 | |
974 | interp = itPtr->interp; |
975 | conn = itPtr->conn; |
976 | |
977 | framePtr = itPtr->adp.framePtr; |
978 | Ns_DStringInit(&ds); |
979 | |
980 | if (framePtr != NULL) { |
981 | Ns_DStringPrintf(&ds, "\n at line %d of ", |
982 | (int)framePtr->line + Tcl_GetErrorLine(interp)); |
983 | } |
984 | inc = NS_EMPTY_STRING; |
985 | while (framePtr != NULL) { |
986 | if (framePtr->file != NULL) { |
987 | Ns_DStringPrintf(&ds, "%sadp file \"%s\"", inc, framePtr->file); |
988 | if (framePtr->ident != NULL) { |
989 | Ns_DStringPrintf(&ds, " {%s}", Tcl_GetString(framePtr->ident)); |
990 | } |
991 | } else { |
992 | adp = Tcl_GetStringFromObj(framePtr->objv[0], &len); |
993 | dot = NS_EMPTY_STRING; |
994 | if (len > 150) { |
995 | len = 150; |
996 | dot = "..."; |
997 | } |
998 | while (((unsigned char)adp[len] & 0xC0u) == 0x80u) { |
999 | |
1000 | |
1001 | |
1002 | len--; |
1003 | dot = "..."; |
1004 | } |
1005 | Ns_DStringPrintf(&ds, "%sadp script:\n\"%.*s%s\"", |
1006 | inc, len, adp, dot); |
1007 | } |
1008 | framePtr = framePtr->prevPtr; |
1009 | inc = "\n included from "; |
1010 | } |
1011 | if (conn != NULL && (itPtr->adp.flags & ADP_DETAIL) != 0u) { |
1012 | size_t i; |
1013 | |
1014 | Ns_DStringPrintf(&ds, "\n while processing connection %s:\n%8s%s", |
1015 | NsConnIdStr(conn), NS_EMPTY_STRING, |
1016 | conn->request.line); |
1017 | for (i = 0u; i < Ns_SetSize(conn->headers); ++i) { |
1018 | Ns_DStringPrintf(&ds, "\n %s: %s", |
1019 | Ns_SetKey(conn->headers, i), |
1020 | Ns_SetValue(conn->headers, i)); |
1021 | } |
1022 | } |
1023 | err = Ns_TclLogErrorInfo(interp, ds.string); |
1024 | if ((itPtr->adp.flags & ADP_DISPLAY) != 0u) { |
1025 | Ns_DStringSetLength(&ds, 0); |
1026 | Ns_DStringAppend(&ds, "<br><pre>\n"); |
1027 | Ns_QuoteHtml(&ds, err); |
1028 | Ns_DStringAppend(&ds, "\n<br></pre>\n"); |
1029 | (void)NsAdpAppend(itPtr, ds.string, ds.length); |
1030 | } |
1031 | Ns_DStringFree(&ds); |
1032 | adp = itPtr->servPtr->adp.errorpage; |
1033 | if (adp != NULL && itPtr->adp.errorLevel == 0) { |
1034 | Tcl_Obj *objv[2]; |
1035 | |
1036 | ++itPtr->adp.errorLevel; |
1037 | objv[0] = Tcl_NewStringObj(adp, -1); |
1038 | Tcl_IncrRefCount(objv[0]); |
1039 | objv[1] = Tcl_GetVar2Ex(interp, "errorInfo", NULL, TCL_GLOBAL_ONLY); |
1040 | if (objv[1] == NULL) { |
1041 | objv[1] = Tcl_GetObjResult(interp); |
1042 | } |
1043 | (void) NsAdpInclude(itPtr, 2, objv, adp, NULL); |
1044 | Tcl_DecrRefCount(objv[0]); |
1045 | --itPtr->adp.errorLevel; |
1046 | } |
1047 | } |
1048 | |
1049 |
|
1050 | |
1051 | |
1052 | |
1053 | |
1054 | |
1055 | |
1056 | |
1057 | |
1058 | |
1059 | |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | |
1066 | |
1067 | static int |
1068 | AdpExec(NsInterp *itPtr, int objc, Tcl_Obj *const* objv, const char *file, |
1069 | const AdpCode *codePtr, Objs *objsPtr, Tcl_DString *outputPtr, |
1070 | const struct stat *stPtr) |
1071 | { |
1072 | Tcl_Interp *interp; |
1073 | AdpFrame frame; |
1074 | Ns_DString cwd; |
1075 | Tcl_Obj *objPtr; |
1076 | int nscript, nblocks, result, i; |
1077 | const char *ptr, *savecwd; |
1078 | |
1079 | NS_NONNULL_ASSERT(itPtr != NULL); |
1080 | NS_NONNULL_ASSERT(codePtr != NULL); |
1081 | NS_NONNULL_ASSERT(outputPtr != NULL); |
1082 | |
1083 | interp = itPtr->interp; |
1084 | |
1085 | |
1086 | |
1087 | |
1088 | |
1089 | Ns_DStringInit(&cwd); |
1090 | frame.file = file; |
1091 | frame.objc = (unsigned short)objc; |
1092 | frame.objv = (Tcl_Obj **)objv; |
1093 | if (stPtr != NULL) { |
| |
1094 | frame.size = stPtr->st_size; |
1095 | frame.mtime = stPtr->st_mtime; |
1096 | } else { |
1097 | frame.size = 0; |
1098 | frame.mtime = 0; |
1099 | } |
1100 | frame.outputPtr = outputPtr; |
1101 | frame.ident = NULL; |
1102 | savecwd = itPtr->adp.cwd; |
1103 | if (file != NULL) { |
| |
1104 | const char *slash = strrchr(file, INTCHAR('/')); |
1105 | if (slash != NULL) { |
| 30 | | Assuming 'slash' is equal to NULL | |
|
| |
1106 | Ns_DStringNAppend(&cwd, file, (int)(slash - file)); |
1107 | itPtr->adp.cwd = cwd.string; |
1108 | } |
1109 | } |
1110 | frame.prevPtr = itPtr->adp.framePtr; |
1111 | itPtr->adp.framePtr = &frame; |
1112 | itPtr->adp.depth++; |
1113 | |
1114 | |
1115 | |
1116 | |
1117 | |
1118 | |
1119 | ptr = AdpCodeText(codePtr); |
1120 | nblocks = AdpCodeBlocks(codePtr); |
1121 | nscript = 0; |
1122 | result = TCL_OK; |
1123 | for (i = 0; itPtr->adp.exception == ADP_OK && i < nblocks; ++i) { |
| 32 | | Assuming field 'exception' is equal to ADP_OK | |
|
| 33 | | Assuming 'i' is < 'nblocks' | |
|
| 34 | | Loop condition is true. Entering loop body | |
|
| 46 | | Assuming 'i' is < 'nblocks' | |
|
| 47 | | Loop condition is true. Entering loop body | |
|
1124 | int len; |
1125 | |
1126 | frame.line = (unsigned short)AdpCodeLine(codePtr, i); |
1127 | len = AdpCodeLen(codePtr, i); |
1128 | if ((itPtr->adp.flags & ADP_TRACE) != 0u) { |
| 35 | | Assuming the condition is false | |
|
| |
| |
1129 | AdpTrace(itPtr, ptr, len); |
1130 | } |
1131 | if (len > 0) { |
| |
| |
| |
| |
1132 | result = NsAdpAppend(itPtr, ptr, len); |
1133 | } else { |
1134 | len = -len; |
1135 | if (itPtr->adp.debugLevel > 0) { |
| |
| |
1136 | result = AdpDebug(itPtr, ptr, len, nscript); |
1137 | } else if (objsPtr == NULL) { |
| 40 | | Assuming 'objsPtr' is not equal to NULL | |
|
| |
| |
1138 | result = Tcl_EvalEx(interp, ptr, len, 0); |
1139 | } else { |
1140 | assert(nscript < objsPtr->nobjs); |
1141 | objPtr = objsPtr->objs[nscript]; |
| 53 | | Access out-of-bound array element (buffer overflow) |
|
1142 | if (objPtr == NULL) { |
| 42 | | Assuming 'objPtr' is not equal to NULL | |
|
| |
1143 | objPtr = Tcl_NewStringObj(ptr, len); |
1144 | Tcl_IncrRefCount(objPtr); |
1145 | objsPtr->objs[nscript] = objPtr; |
1146 | } |
1147 | result = Tcl_EvalObjEx(interp, objPtr, 0); |
1148 | } |
1149 | ++nscript; |
1150 | |
1151 | |
1152 | |
1153 | |
1154 | |
1155 | if (result == TCL_ERROR) { |
| 44 | | Assuming 'result' is not equal to TCL_ERROR | |
|
1156 | if (NsTclTimeoutException(interp) == NS_TRUE) { |
1157 | itPtr->adp.exception = ADP_TIMEOUT; |
1158 | } |
1159 | } |
1160 | } |
1161 | |
1162 | |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | |
1168 | if (result != TCL_OK && itPtr->adp.exception == ADP_OK) { |
| 45 | | Assuming 'result' is equal to TCL_OK | |
|
1169 | if ((itPtr->adp.flags & ADP_ERRLOGGED) == 0u) { |
1170 | NsAdpLogError(itPtr); |
1171 | } |
1172 | if ((itPtr->adp.flags & ADP_STRICT) != 0u) { |
1173 | itPtr->adp.flags |= ADP_ERRLOGGED; |
1174 | break; |
1175 | } |
1176 | } |
1177 | ptr += len; |
1178 | } |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | |
1184 | switch (itPtr->adp.exception) { |
1185 | case ADP_OK: |
1186 | break; |
1187 | case ADP_RETURN: |
1188 | itPtr->adp.exception = ADP_OK; |
1189 | NS_FALL_THROUGH; |
1190 | case ADP_ABORT: |
1191 | NS_FALL_THROUGH; |
1192 | case ADP_BREAK: |
1193 | NS_FALL_THROUGH; |
1194 | case ADP_TIMEOUT: |
1195 | result = TCL_OK; |
1196 | break; |
1197 | } |
1198 | |
1199 | |
1200 | |
1201 | |
1202 | |
1203 | itPtr->adp.framePtr = frame.prevPtr; |
1204 | itPtr->adp.cwd = savecwd; |
1205 | if (frame.ident != NULL) { |
1206 | Tcl_DecrRefCount(frame.ident); |
1207 | } |
1208 | Ns_DStringFree(&cwd); |
1209 | |
1210 | return result; |
1211 | } |
1212 | |
1213 |
|
1214 | |
1215 | |
1216 | |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | static int |
1233 | AdpDebug(const NsInterp *itPtr, const char *ptr, int len, int nscript) |
1234 | { |
1235 | Tcl_Interp *interp; |
1236 | int level; |
1237 | const char *file; |
1238 | char debugfile[255]; |
1239 | Ns_DString ds; |
1240 | int result, fd; |
1241 | |
1242 | NS_NONNULL_ASSERT(itPtr != NULL); |
1243 | NS_NONNULL_ASSERT(ptr != NULL); |
1244 | |
1245 | interp = itPtr->interp; |
1246 | level = itPtr->adp.debugLevel; |
1247 | file = Tcl_GetString(itPtr->adp.framePtr->objv[0]); |
1248 | |
1249 | Ns_DStringInit(&ds); |
1250 | Ns_DStringPrintf(&ds, "#\n" |
1251 | "# level: %d\n" |
1252 | "# chunk: %d\n" |
1253 | "# file: %s\n" |
1254 | "#\n\n", level, nscript, file); |
1255 | Ns_DStringNAppend(&ds, ptr, len); |
1256 | |
1257 | snprintf(debugfile, sizeof(debugfile), |
1258 | P_tmpdir "/adp%d.%d.XXXXXX", |
1259 | level, nscript); |
1260 | fd = ns_mkstemp(debugfile); |
1261 | if (fd < 0) { |
1262 | Ns_TclPrintfResult(interp, "could not create ADP debug file"); |
1263 | result = TCL_ERROR; |
1264 | } else { |
1265 | if (ns_write(fd, ds.string, (size_t)ds.length) < 0) { |
1266 | Ns_TclPrintfResult(interp, "write to \"%s\" failed: %s", |
1267 | debugfile, Tcl_PosixError(interp)); |
1268 | result = TCL_ERROR; |
1269 | } else { |
1270 | Ns_DStringSetLength(&ds, 0); |
1271 | Ns_DStringVarAppend(&ds, "source ", debugfile, (char *)0L); |
1272 | result = Tcl_EvalEx(interp, ds.string, ds.length, 0); |
1273 | } |
1274 | (void) ns_close(fd); |
1275 | unlink(debugfile); |
1276 | } |
1277 | Ns_DStringFree(&ds); |
1278 | |
1279 | return result; |
1280 | } |
1281 | |
1282 |
|
1283 | |
1284 | |
1285 | |
1286 | |
1287 | |
1288 | |
1289 | |
1290 | |
1291 | |
1292 | |
1293 | |
1294 | |
1295 | |
1296 | |
1297 | |
1298 | |
1299 | static void |
1300 | FreeInterpPage(void *arg) |
1301 | { |
1302 | InterpPage *ipagePtr = arg; |
1303 | Page *pagePtr = ipagePtr->pagePtr; |
1304 | NsServer *servPtr = pagePtr->servPtr; |
1305 | |
1306 | FreeObjs(ipagePtr->objs); |
1307 | Ns_MutexLock(&servPtr->adp.pagelock); |
1308 | if (--pagePtr->refcnt == 0) { |
1309 | if (pagePtr->hPtr != NULL) { |
1310 | Tcl_DeleteHashEntry(pagePtr->hPtr); |
1311 | } |
1312 | if (pagePtr->cachePtr != NULL) { |
1313 | FreeObjs(ipagePtr->cacheObjs); |
1314 | DecrCache(pagePtr->cachePtr); |
1315 | } |
1316 | NsAdpFreeCode(&pagePtr->code); |
1317 | ns_free(pagePtr); |
1318 | } |
1319 | Ns_MutexUnlock(&servPtr->adp.pagelock); |
1320 | ns_free(ipagePtr); |
1321 | } |
1322 | |
1323 |
|
1324 | |
1325 | |
1326 | |
1327 | |
1328 | |
1329 | |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | |
1338 | |
1339 | |
1340 | static Objs * |
1341 | AllocObjs(int nobjs) |
1342 | { |
1343 | Objs *objsPtr; |
1344 | |
1345 | objsPtr = ns_calloc(1u, sizeof(Objs) + ((size_t)nobjs * sizeof(Tcl_Obj *))); |
1346 | objsPtr->nobjs = nobjs; |
1347 | |
1348 | return objsPtr; |
1349 | } |
1350 | |
1351 |
|
1352 | |
1353 | |
1354 | |
1355 | |
1356 | |
1357 | |
1358 | |
1359 | |
1360 | |
1361 | |
1362 | |
1363 | |
1364 | |
1365 | |
1366 | |
1367 | |
1368 | static void |
1369 | FreeObjs(Objs *objsPtr) |
1370 | { |
1371 | int i; |
1372 | |
1373 | NS_NONNULL_ASSERT(objsPtr != NULL); |
1374 | |
1375 | for (i = 0; i < objsPtr->nobjs; ++i) { |
1376 | if (objsPtr->objs[i] != NULL) { |
1377 | Tcl_DecrRefCount(objsPtr->objs[i]); |
1378 | } |
1379 | } |
1380 | ns_free(objsPtr); |
1381 | } |
1382 | |
1383 |
|
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | |
1396 | |
1397 | |
1398 | |
1399 | |
1400 | |
1401 | static void |
1402 | DecrCache(AdpCache *cachePtr) |
1403 | { |
1404 | NS_NONNULL_ASSERT(cachePtr != NULL); |
1405 | |
1406 | if (--cachePtr->refcnt == 0) { |
1407 | NsAdpFreeCode(&cachePtr->code); |
1408 | ns_free(cachePtr); |
1409 | } |
1410 | } |
1411 | |
1412 |
|
1413 | |
1414 | |
1415 | |
1416 | |
1417 | |
1418 | |
1419 | |
1420 | |
1421 | |
1422 | |
1423 | |
1424 | |
1425 | |
1426 | |
1427 | |
1428 | |
1429 | static void |
1430 | AdpTrace(const NsInterp *itPtr, const char *ptr, int len) |
1431 | { |
1432 | char type; |
1433 | |
1434 | NS_NONNULL_ASSERT(itPtr != NULL); |
1435 | NS_NONNULL_ASSERT(ptr != NULL); |
1436 | |
1437 | if (len >= 0) { |
1438 | type = 'T'; |
1439 | } else { |
1440 | type = 'S'; |
1441 | len = -len; |
1442 | } |
1443 | if (len > itPtr->servPtr->adp.tracesize) { |
1444 | len = itPtr->servPtr->adp.tracesize; |
1445 | } |
1446 | Ns_Log(Notice, "adp[%d%c]: %.*s", itPtr->adp.depth, type, len, ptr); |
1447 | } |
1448 | |
1449 | |
1450 | |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |