1
/*###################################################################################
3
# Embperl - Copyright (c) 1997-2004 Gerald Richter / ECOS
5
# You may distribute under the terms of either the GNU General Public
6
# License or the Artistic License, as specified in the Perl README file.
7
# For use with Apache httpd and mod_perl, see also Apache copyright.
9
# THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13
# $Id: epio.c,v 1.25 2004/01/23 06:50:55 richter Exp $
15
###################################################################################*/
20
#include "crypto/epcrypto.h"
26
#define FILEIOTYPE "StdIO"
27
/* define same helper macros to let it run with plain perl 5.003 */
28
/* where no PerlIO is present */
30
#define PerlIO_stdinF stdin
31
#define PerlIO_stdoutF stdout
32
#define PerlIO_stderrF stderr
33
#define PerlIO_close fclose
34
#define PerlIO_open fopen
35
#define PerlIO_flush fflush
36
#define PerlIO_vprintf vfprintf
37
#define PerlIO_fileno fileno
38
#define PerlIO_tell ftell
39
#define PerlIO_seek fseek
41
#define PerlIO_read(f,buf,cnt) fread(buf,1,cnt,f)
42
#define PerlIO_write(f,buf,cnt) fwrite(buf,1,cnt,f)
44
#define PerlIO_putc(f,c) fputc(c,f)
48
#define FILEIOTYPE "PerlIO"
50
#define PerlIO_stdinF PerlIO_stdin ()
51
#define PerlIO_stdoutF PerlIO_stdout ()
52
#define PerlIO_stderrF PerlIO_stderr ()
58
/* Some helper macros for tied handles, taken from mod_perl 2.0 :-) */
60
* bleedperl change #11639 switch tied handle magic
61
* from living in the gv to the GvIOp(gv), so we have to deal
62
* with both to support 5.6.x
64
#if ((PERL_REVISION == 5) && (PERL_VERSION >= 7))
65
# define TIEHANDLE_SV(handle) (SV*)GvIOp((SV*)handle)
67
# define TIEHANDLE_SV(handle) (SV*)handle
70
#define HANDLE_GV(name) gv_fetchpv(name, TRUE, SVt_PVIO)
75
#define DefaultLog "/tmp/embperl.log"
78
static request_rec * pAllocReq = NULL ;
83
/* -------------------------------------------------------------------------------------
85
* begin output transaction
87
-------------------------------------------------------------------------------------- */
89
struct tBuf * oBegin (/*i/o*/ register req * r)
92
EPENTRY1N (oBegin, r -> Component.pOutput -> nMarker) ;
94
r -> Component.pOutput -> nMarker++ ;
96
return r -> Component.pOutput -> pLastBuf ;
99
/* -------------------------------------------------------------------------------------
101
* rollback output transaction (throw away all the output since corresponding begin)
103
-------------------------------------------------------------------------------------- */
105
void oRollbackOutput (/*i/o*/ register req * r,
109
EPENTRY1N (oRollback, r -> Component.pOutput -> nMarker) ;
113
if (r -> Component.pOutput -> pLastFreeBuf)
114
r -> Component.pOutput -> pLastFreeBuf -> pNext = r -> Component.pOutput -> pFirstBuf ;
116
r -> Component.pOutput -> pFreeBuf = r -> Component.pOutput -> pFirstBuf ;
118
r -> Component.pOutput -> pLastFreeBuf = r -> Component.pOutput -> pLastBuf ;
120
r -> Component.pOutput -> pFirstBuf = NULL ;
121
r -> Component.pOutput -> nMarker = 0 ;
125
if (r -> Component.pOutput -> pLastBuf == pBuf || pBuf -> pNext == NULL)
126
r -> Component.pOutput -> nMarker-- ;
129
r -> Component.pOutput -> nMarker = pBuf -> pNext -> nMarker - 1 ;
130
if (r -> Component.pOutput -> pLastFreeBuf)
131
r -> Component.pOutput -> pLastFreeBuf -> pNext = pBuf -> pNext ;
133
r -> Component.pOutput -> pFreeBuf = pBuf -> pNext ;
134
r -> Component.pOutput -> pLastFreeBuf = r -> Component.pOutput -> pLastBuf ;
136
pBuf -> pNext = NULL ;
139
r -> Component.pOutput -> pLastBuf = pBuf ;
143
/* -------------------------------------------------------------------------------------
145
* rollback output transaction and errors(throw away all the output since corresponding
148
-------------------------------------------------------------------------------------- */
150
void oRollback (/*i/o*/ register req * r,
154
oRollbackOutput (r, pBuf) ;
156
/* RollbackError (r) ; */
159
/* ---------------------------------------------------------------------------- */
161
/* commit output transaction (all the output since corresponding begin is vaild)*/
163
/* ---------------------------------------------------------------------------- */
165
void oCommitToMem (/*i/o*/ register req * r,
170
EPENTRY1N (oCommit, r -> Component.pOutput -> nMarker) ;
174
r -> Component.pOutput -> nMarker = 0 ;
176
if (r -> Component.pOutput -> pLastBuf == pBuf)
177
r -> Component.pOutput -> nMarker-- ;
179
r -> Component.pOutput -> nMarker = pBuf -> pNext -> nMarker - 1 ;
181
if (r -> Component.pOutput -> nMarker == 0)
184
pBuf = r -> Component.pOutput -> pFirstBuf ;
186
pBuf = pBuf -> pNext ;
192
memmove (pOut, pBuf + 1, pBuf -> nSize) ;
193
pOut += pBuf -> nSize ;
194
pBuf = pBuf -> pNext ;
202
owrite (r, pBuf + 1, pBuf -> nSize) ;
203
pBuf = pBuf -> pNext ;
208
/* CommitError (r) ; */
211
/* ---------------------------------------------------------------------------- */
213
/* commit output transaction (all the output since corresponding begin is vaild)*/
215
/* ---------------------------------------------------------------------------- */
217
void oCommit (/*i/o*/ register req * r,
221
EPENTRY1N (oCommit, r -> Component.pOutput -> nMarker) ;
223
oCommitToMem (r, pBuf, NULL) ;
226
/* ---------------------------------------------------------------------------- */
228
/* write to a buffer */
230
/* we will alloc a new buffer for every write */
231
/* this is fast with ep_palloc */
233
/* ---------------------------------------------------------------------------- */
236
static int bufwrite (/*i/o*/ register req * r,
237
/*in*/ const void * ptr, size_t size)
243
EPENTRY1N (bufwrite, r -> Component.pOutput -> nMarker) ;
245
pBuf = (struct tBuf *)ep_palloc (r -> Component.pOutput -> pPool, size + sizeof (struct tBuf)) ;
250
memcpy (pBuf + 1, ptr, size) ;
251
pBuf -> pNext = NULL ;
252
pBuf -> nSize = size ;
253
pBuf -> nMarker = r -> Component.pOutput -> nMarker ;
255
if (r -> Component.pOutput -> pLastBuf)
257
r -> Component.pOutput -> pLastBuf -> pNext = pBuf ;
258
pBuf -> nCount = r -> Component.pOutput -> pLastBuf -> nCount + size ;
261
pBuf -> nCount = size ;
263
if (r -> Component.pOutput -> pFirstBuf == NULL)
264
r -> Component.pOutput -> pFirstBuf = pBuf ;
265
r -> Component.pOutput -> pLastBuf = pBuf ;
272
/* ---------------------------------------------------------------------------- */
276
/* free all buffers */
277
/* note: this is not nessecary for apache palloc, because all buffers are freed */
278
/* at the end of the request */
280
/* ---------------------------------------------------------------------------- */
283
static void buffree (/*i/o*/ register req * r)
286
struct tBuf * pNext = NULL ;
290
if ((r -> Component.Config.bDebug & dbgMem) == 0 && pAllocReq != NULL)
292
r -> Component.pOutput -> pFirstBuf = NULL ;
293
r -> Component.pOutput -> pLastBuf = NULL ;
294
r -> Component.pOutput -> pFreeBuf = NULL ;
295
r -> Component.pOutput -> pLastFreeBuf = NULL ;
296
return ; /* no need for apache to free memory */
300
/* first walk thru the used buffers */
302
pBuf = r -> Component.pOutput -> pFirstBuf ;
305
pNext = pBuf -> pNext ;
310
r -> Component.pOutput -> pFirstBuf = NULL ;
311
r -> Component.pOutput -> pLastBuf = NULL ;
314
/* now walk thru the unused buffers */
316
pBuf = r -> Component.pOutput -> pFreeBuf ;
319
pNext = pBuf -> pNext ;
324
r -> Component.pOutput -> pFreeBuf = NULL ;
325
r -> Component.pOutput -> pLastFreeBuf = NULL ;
329
/* ---------------------------------------------------------------------------- */
331
/* get the length outputed to buffers so far */
333
/* ---------------------------------------------------------------------------- */
335
int GetContentLength (/*i/o*/ register req * r)
337
if (r -> Component.pOutput -> pLastBuf)
338
return r -> Component.pOutput -> pLastBuf -> nCount ;
344
/* ---------------------------------------------------------------------------- */
346
/* set the name of the input file and open it */
348
/* ---------------------------------------------------------------------------- */
351
int OpenInput (/*i/o*/ register req * r,
352
/*in*/ const char * sFilename)
364
handle = HANDLE_GV("STDIN") ;
367
SV *iohandle = TIEHANDLE_SV(handle) ;
369
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj)
371
r -> Component.ifdobj = mg->mg_obj ;
372
if (r -> Component.Config.bDebug)
374
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj)));
375
lprintf (r -> pApp, "[%d]Open TIED STDIN %s...\n", r -> pThread -> nPid, package) ;
381
if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF)
382
PerlIO_close (r -> Component.ifd) ;
384
r -> Component.ifd = NULL ;
386
if (sFilename == NULL || *sFilename == '\0')
389
GV * io = gv_fetchpv("STDIN", TRUE, SVt_PVIO) ;
390
if (io == NULL || (r -> Component.ifd = IoIFP(io)) == NULL)
392
if (r -> Component.Config.bDebug)
393
lprintf (r -> pApp, "[%d]Cannot get Perl STDIN, open os stdin\n", r -> pThread -> nPid) ;
394
r -> Component.ifd = PerlIO_stdinF ;
398
r -> Component.ifd = PerlIO_stdinF ;
403
if ((r -> Component.ifd = PerlIO_open (sFilename, "r")) == NULL)
405
strncpy (r -> errdat1, sFilename, sizeof (r -> errdat1) - 1) ;
406
strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
407
return rcFileOpenErr ;
414
/* ---------------------------------------------------------------------------- */
416
/* close input file */
418
/* ---------------------------------------------------------------------------- */
421
int CloseInput (/*i/o*/ register req * r)
427
if (0) /* r -> Component.ifdobj) */
433
XPUSHs(r -> Component.ifdobj);
435
perl_call_method ("CLOSE", G_VOID | G_EVAL) ;
439
r -> Component.ifdobj = NULL ;
448
if (r -> Component.ifd && r -> Component.ifd != PerlIO_stdinF)
449
PerlIO_close (r -> Component.ifd) ;
451
r -> Component.ifd = NULL ;
458
/* ---------------------------------------------------------------------------- */
460
/* read block of data from input (web client) */
462
/* ---------------------------------------------------------------------------- */
465
int iread (/*i/o*/ register req * r,
466
/*in*/ void * ptr, size_t size)
469
char * p = (char *)ptr ; /* workaround for aix c complier */
475
if (r -> Component.ifdobj)
485
XPUSHs(r -> Component.ifdobj);
486
XPUSHs(sv_2mortal(pBufSV = NEWSV(0, 0)));
488
num = perl_call_method ("READ", G_SCALAR) ;
498
p = SvPV (pBufSV, l) ;
515
ap_setup_client_block(r -> pApacheReq, REQUEST_CHUNKED_ERROR);
516
if(ap_should_client_block(r -> pApacheReq))
522
c = ap_get_client_block(r -> pApacheReq, p, size);
535
return PerlIO_read (r -> Component.ifd, p, size) ;
539
/* ---------------------------------------------------------------------------- */
541
/* read line of data from input (web client) */
543
/* ---------------------------------------------------------------------------- */
546
char * igets (/*i/o*/ register req * r,
547
/*in*/ char * s, int size)
558
FILE * f = PerlIO_exportFILE (r -> Component.ifd, 0) ;
559
char * p = fgets (s, size, f) ;
560
PerlIO_releaseFILE (r -> Component.ifd, f) ;
566
return fgets (s, size, r -> Component.ifd) ;
570
/* ---------------------------------------------------------------------------- */
572
/* read HTML File into pBuf */
574
/* ---------------------------------------------------------------------------- */
576
int ReadHTML (/*i/o*/ register req * r,
577
/*in*/ char * sInputfile,
578
/*in*/ size_t * nFileSize,
579
/*out*/ SV * * ppBuf)
591
if (r -> Component.Config.bDebug)
592
lprintf (r -> pApp, "[%d]Reading %s as input using %s (%d Bytes)...\n", r -> pThread -> nPid, sInputfile, FILEIOTYPE, *nFileSize) ;
595
if ((ifd = PerlIO_open (sInputfile, "rb")) == NULL)
597
if ((ifd = PerlIO_open (sInputfile, "r")) == NULL)
600
strncpy (r -> errdat1, sInputfile, sizeof (r -> errdat1) - 1) ;
601
strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
602
return rcFileOpenErr ;
605
if ((long)*nFileSize < 0)
606
return rcFileOpenErr ;
609
pBufSV = sv_2mortal (newSV(*nFileSize + 1)) ;
610
pData = SvPVX(pBufSV) ;
620
syntax = (r -> Component.pTokenTable && strcmp ((char *)r -> Component.pTokenTable, "Text") == 0)?"Text":"Embperl" ;
622
syntax = r -> Component.Config.sSyntax ;
625
if ((rc = do_crypt_file (ifd, NULL, pData, *nFileSize, 0, syntax, EPC_HEADER)) <= 0)
627
if (rc < -1 || !EPC_UNENCYRPTED)
629
sprintf (r -> errdat1, "%d", rc) ;
630
return rcCryptoWrongHeader + -rc - 1;
633
PerlIO_seek (ifd, 0, SEEK_SET) ;
634
*nFileSize = PerlIO_read (ifd, pData, *nFileSize) ;
642
*nFileSize = PerlIO_read (ifd, pData, *nFileSize) ;
648
pData [*nFileSize] = '\0' ;
649
SvCUR_set (pBufSV, *nFileSize) ;
650
SvTEMP_off (pBufSV) ;
659
/* ---------------------------------------------------------------------------- */
661
/* set the name of the output file and open it */
663
/* ---------------------------------------------------------------------------- */
667
int OpenOutput (/*i/o*/ register req * r,
668
/*in*/ const char * sFilename)
675
r -> Component.pOutput -> pFirstBuf = NULL ;
676
r -> Component.pOutput -> pLastBuf = NULL ;
677
r -> Component.pOutput -> nMarker = 0 ;
678
r -> Component.pOutput -> pMemBuf = NULL ;
679
r -> Component.pOutput -> nMemBufSize = 0 ;
680
r -> Component.pOutput -> pFreeBuf = NULL ;
681
r -> Component.pOutput -> pLastFreeBuf = NULL ;
685
if (r -> Component.pOutput -> ofd && r -> Component.pOutput -> ofd != PerlIO_stdoutF)
686
PerlIO_close (r -> Component.pOutput -> ofd) ;
688
r -> Component.pOutput -> ofd = NULL ;
690
if (sFilename == NULL || *sFilename == '\0')
695
if (r -> Component.Config.bDebug)
696
lprintf (r -> pApp, "[%d]Using APACHE for output...\n", r -> pThread -> nPid) ;
701
handle = HANDLE_GV("STDOUT") ;
704
SV *iohandle = TIEHANDLE_SV(handle) ;
706
if (iohandle && SvMAGICAL(iohandle) && (mg = mg_find((SV*)iohandle, 'q')) && mg->mg_obj)
708
r -> Component.pOutput -> ofdobj = mg->mg_obj ;
709
if (r -> Component.Config.bDebug)
711
char *package = HvNAME(SvSTASH((SV*)SvRV(mg->mg_obj)));
712
lprintf (r -> pApp, "[%d]Open TIED STDOUT %s for output...\n", r -> pThread -> nPid, package) ;
718
r -> Component.pOutput -> ofd = PerlIO_stdoutF ;
720
if (r -> Component.Config.bDebug)
724
lprintf (r -> pApp, "[%d]Open STDOUT to Apache for output...\n", r -> pThread -> nPid) ;
727
lprintf (r -> pApp, "[%d]Open STDOUT for output...\n", r -> pThread -> nPid) ;
732
if (r -> Component.Config.bDebug)
733
lprintf (r -> pApp, "[%d]Open %s for output...\n", r -> pThread -> nPid, sFilename) ;
736
if ((r -> Component.pOutput -> ofd = PerlIO_open (sFilename, "wb")) == NULL)
738
if ((r -> Component.pOutput -> ofd = PerlIO_open (sFilename, "w")) == NULL)
741
strncpy (r -> errdat1, sFilename, sizeof (r -> errdat1) - 1) ;
742
strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
743
return rcFileOpenErr ;
750
/* ---------------------------------------------------------------------------- */
752
/* close the output file */
754
/* ---------------------------------------------------------------------------- */
757
int CloseOutput (/*in*/ tReq * r,
758
tComponentOutput * pOutput)
767
if (0) /* r -> Component.pOutput -> ofdobj) */
773
XPUSHs(pOutput -> ofdobj);
775
perl_call_method ("CLOSE", G_VOID | G_EVAL) ;
779
pOutput -> ofdobj = NULL ;
783
if (pOutput -> ofd && pOutput -> ofd != PerlIO_stdoutF)
784
PerlIO_close (pOutput -> ofd) ;
786
pOutput -> ofd = NULL ;
792
/* ---------------------------------------------------------------------------- */
794
/* set output to memory buffer */
796
/* ---------------------------------------------------------------------------- */
800
void OutputToMemBuf (/*i/o*/ register req * r,
802
/*in*/ size_t nBufSize)
806
pBuf = ep_palloc (r -> Component.pOutput -> pPool, nBufSize) ;
809
r -> Component.pOutput -> pMemBuf = pBuf ;
810
r -> Component.pOutput -> pMemBufPtr = pBuf ;
811
r -> Component.pOutput -> nMemBufSize = nBufSize ;
812
r -> Component.pOutput -> nMemBufSizeFree = nBufSize ;
816
/* ---------------------------------------------------------------------------- */
818
/* set output to standard */
820
/* ---------------------------------------------------------------------------- */
823
char * OutputToStd (/*i/o*/ register req * r)
826
char * p = r -> Component.pOutput -> pMemBuf ;
827
r -> Component.pOutput -> pMemBuf = NULL ;
828
r -> Component.pOutput -> nMemBufSize = 0 ;
829
r -> Component.pOutput -> nMemBufSizeFree = 0 ;
835
/* ---------------------------------------------------------------------------- */
837
/* puts to output (web client) */
839
/* ---------------------------------------------------------------------------- */
841
int oputs (/*i/o*/ register req * r,
842
/*in*/ const char * str)
845
return owrite (r, str, strlen (str)) ;
849
/* ---------------------------------------------------------------------------- */
851
/* write block of data to output (web client) */
853
/* ---------------------------------------------------------------------------- */
855
int owrite (/*i/o*/ register req * r,
856
/*in*/ const void * ptr, size_t size)
862
if (n == 0 || r -> Component.pOutput -> bDisableOutput)
865
if (r -> Component.pOutput -> pMemBuf)
868
size_t s = r -> Component.pOutput -> nMemBufSize ;
869
if (n >= r -> Component.pOutput -> nMemBufSizeFree)
873
s = n + r -> Component.pOutput -> nMemBufSize ;
875
r -> Component.pOutput -> nMemBufSize += s ;
876
r -> Component.pOutput -> nMemBufSizeFree += s ;
877
/*lprintf (r -> pApp, "[%d]MEM: Realloc pMemBuf, nMemSize = %d\n", nPid, nMemBufSize) ; */
879
p = ep_palloc (r -> Component.pOutput -> pPool, r -> Component.pOutput -> nMemBufSize) ;
882
r -> Component.pOutput -> nMemBufSize -= s ;
883
r -> Component.pOutput -> nMemBufSizeFree -= s ;
886
memcpy (p, r -> Component.pOutput -> pMemBuf, oldsize) ;
887
r -> Component.pOutput -> pMemBufPtr = p + (r -> Component.pOutput -> pMemBufPtr - r -> Component.pOutput -> pMemBuf) ;
888
r -> Component.pOutput -> pMemBuf = p ;
891
memcpy (r -> Component.pOutput -> pMemBufPtr, ptr, n) ;
892
r -> Component.pOutput -> pMemBufPtr += n ;
893
*(r -> Component.pOutput -> pMemBufPtr) = '\0' ;
894
r -> Component.pOutput -> nMemBufSizeFree -= n ;
899
if (r -> Component.pOutput -> nMarker)
900
return bufwrite (r, ptr, n) ;
902
if (r -> Component.pOutput -> ofdobj)
908
XPUSHs(r -> Component.pOutput -> ofdobj);
909
XPUSHs(sv_2mortal(newSVpv((char *)ptr,size)));
911
perl_call_method ("PRINT", G_SCALAR) ;
920
if (r -> pApacheReq && r -> Component.pOutput -> ofd == NULL)
924
n = ap_rwrite (ptr, n, r -> pApacheReq) ;
925
if (r -> Component.Config.bDebug & dbgFlushOutput)
926
ap_rflush (r -> pApacheReq) ;
933
if (n > 0 && r -> Component.pOutput -> ofd)
935
n = PerlIO_write (r -> Component.pOutput -> ofd, (void *)ptr, size) ;
937
if (r -> Component.Config.bDebug & dbgFlushOutput)
938
PerlIO_flush (r -> Component.pOutput -> ofd) ;
946
/* ---------------------------------------------------------------------------- */
948
/* write one char to output (web client) */
950
/* ---------------------------------------------------------------------------- */
953
void oputc (/*i/o*/ register req * r,
959
if (r -> Component.pOutput -> nMarker || r -> Component.pOutput -> pMemBuf || r -> Component.pOutput -> ofdobj)
966
if (r -> pApacheReq && r -> Component.pOutput -> ofd == NULL)
968
ap_rputc (c, r -> pApacheReq) ;
969
if (r -> Component.Config.bDebug & dbgFlushOutput)
970
ap_rflush (r -> pApacheReq) ;
974
PerlIO_putc (r -> Component.pOutput -> ofd, c) ;
976
if (r -> Component.Config.bDebug & dbgFlushOutput)
977
PerlIO_flush (r -> Component.pOutput -> ofd) ;
982
/* ---------------------------------------------------------------------------- */
986
/* ---------------------------------------------------------------------------- */
989
int OpenLog (/*i/o*/ tApp * a)
995
return ok ; /*already open */
997
if (a -> lfd && a -> lfd != PerlIO_stdoutF)
998
PerlIO_close (a -> lfd) ; /* close old logfile */
1003
if (a -> Config.bDebug == 0)
1004
return ok ; /* never write to logfile if debugging is disabled */
1006
if (!a -> Config.sLog && a -> Config.sLog[0] == '\0')
1008
a -> lfd = PerlIO_stdoutF ;
1012
if ((a -> lfd = PerlIO_open (a -> Config.sLog, "a")) == NULL)
1014
tReq * r = a -> pThread -> pCurrReq ;
1017
strncpy (r -> errdat1, a -> Config.sLog, sizeof (r -> errdat1) - 1) ;
1018
strncpy (r -> errdat2, Strerror(errno), sizeof (r -> errdat2) - 1) ;
1020
return rcLogFileOpenErr ;
1026
/* ---------------------------------------------------------------------------- */
1028
/* return the handle of the log file */
1030
/* ---------------------------------------------------------------------------- */
1033
int GetLogHandle (/*i/o*/ tApp * a)
1039
return PerlIO_fileno (a -> lfd) ;
1045
/* ---------------------------------------------------------------------------- */
1047
/* return the current posittion of the log file */
1049
/* ---------------------------------------------------------------------------- */
1052
long GetLogFilePos (/*i/o*/ tApp * a)
1058
return PerlIO_tell (a -> lfd) ;
1064
/* ---------------------------------------------------------------------------- */
1066
/* close the log file */
1068
/* ---------------------------------------------------------------------------- */
1071
int CloseLog (/*i/o*/ tApp * a)
1076
if (a -> lfd && a -> lfd != PerlIO_stdoutF)
1077
PerlIO_close (a -> lfd) ;
1086
/* ---------------------------------------------------------------------------- */
1088
/* flush the log file */
1090
/* ---------------------------------------------------------------------------- */
1093
int FlushLog (/*i/o*/ tApp * a)
1098
if (a -> lfd != NULL)
1099
PerlIO_flush (a -> lfd) ;
1106
/* ---------------------------------------------------------------------------- */
1108
/* printf to log file */
1110
/* ---------------------------------------------------------------------------- */
1112
int lprintf (/*i/o*/ tApp * a,
1113
/*in*/ const char * sFormat,
1121
if (a -> lfd == NULL)
1124
va_start (ap, sFormat) ;
1127
n = PerlIO_vprintf (a -> lfd, sFormat, ap) ;
1128
if (a -> Config.bDebug & dbgFlushLog)
1129
PerlIO_flush (a -> lfd) ;
1139
/* ---------------------------------------------------------------------------- */
1141
/* write block of data to log file */
1143
/* ---------------------------------------------------------------------------- */
1146
int lwrite (/*i/o*/ tApp * a,
1147
/*in*/ const void * ptr,
1154
if (a -> lfd == NULL)
1157
n = PerlIO_write (a -> lfd, (void *)ptr, size) ;
1159
if (a -> Config.bDebug & dbgFlushLog)
1160
PerlIO_flush (a -> lfd) ;
1168
/* ---------------------------------------------------------------------------- */
1170
/* Memory Allocation */
1172
/* ---------------------------------------------------------------------------- */
1175
void _free (/*i/o*/ register req * r,
1180
if (pAllocReq && !(r -> Component.Config.bDebug & dbgMem))
1186
if (r -> Component.Config.bDebug & dbgMem)
1191
/* we do it a bit complicted so it compiles also on aix */
1196
r -> nAllocSize -= size ;
1197
lprintf (r -> pApp, "[%d]MEM: Free %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1202
if (r -> pApacheReq == NULL)
1210
void * _malloc (/*i/o*/ register req * r,
1217
pAllocReq = r -> pApacheReq ;
1219
if (r -> pApacheReq)
1221
p = apr_palloc (r -> pApacheReq -> pool, size + sizeof (size)) ;
1228
p = malloc (size + sizeof (size)) ;
1232
if (r -> Component.Config.bDebug & dbgMem)
1235
/* we do it a bit complicted so it compiles also on aix */
1240
r -> nAllocSize += size ;
1241
lprintf (r -> pApp, "[%d]MEM: Alloc %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1248
void * _realloc (/*i/o*/ register req * r, void * ptr, size_t oldsize, size_t size)
1254
if (r -> pApacheReq)
1256
p = apr_palloc (r -> pApacheReq -> pool, size + sizeof (size)) ;
1261
if (r -> Component.Config.bDebug & dbgMem)
1265
/* we do it a bit complicted so it compiles also on aix */
1270
ps = (size_t *)ptr ;
1273
r -> nAllocSize += size - sizeold ;
1275
lprintf (r -> pApp, "[%d]MEM: ReAlloc %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1279
memcpy (p, ptr, oldsize) ;
1284
if (r -> Component.Config.bDebug & dbgMem)
1287
ps = (size_t *)ptr ;
1289
r -> nAllocSize -= *ps ;
1291
p = realloc (ps, size + sizeof (size)) ;
1295
/* we do it a bit complicted so it compiles also on aix */
1299
r -> nAllocSize += size ;
1300
lprintf (r -> pApp, "[%d]MEM: ReAlloc %d Bytes at %08x Allocated so far %d Bytes\n" ,r -> pThread -> nPid, size, p, r -> nAllocSize) ;
1306
p = realloc (ptr, size + sizeof (size)) ;
1313
char * _memstrcat (/*i/o*/ register req * r,
1324
EPENTRY(_memstrcat) ;
1333
lprintf (r -> pApp, "sum = %d p = %s\n", sum, p) ;
1334
p = va_arg (ap, char *) ;
1340
sp = str = _malloc (r, sum+1) ;
1348
lprintf (r -> pApp, "l = %d p = %s\n", l, p) ;
1349
memcpy (str, p, l) ;
1351
p = va_arg (ap, char *) ;
1363
char * _ep_strdup (/*i/o*/ register req * r,
1364
/*in*/ const char * str)
1368
int len = strlen (str) ;
1370
p = (char *)_malloc (r, len + 1) ;
1380
char * _ep_strndup (/*i/o*/ register req * r,
1381
/*in*/ const char * str,
1387
p = (char *)_malloc (r, len + 1) ;
1391
strncpy (p, str, len) ;
1399
char * _ep_memdup (/*i/o*/ register req * r,
1400
/*in*/ const char * str,
1406
p = (char *)_malloc (r, len + 1) ;
1410
memcpy (p, str, len) ;