Moved most of the function into separate files: the heap debugging
functions are in "heap.c"; the daemon functions are in "daemon.c"; and, the string/text functions are in "text.c".
This commit is contained in:
parent
52c15029d1
commit
b77fc5c860
175
src/utils.c
175
src/utils.c
@ -1,4 +1,4 @@
|
|||||||
/* $Id: utils.c,v 1.29 2002-04-28 20:03:53 rjkaes Exp $
|
/* $Id: utils.c,v 1.30 2002-05-23 18:28:12 rjkaes Exp $
|
||||||
*
|
*
|
||||||
* Misc. routines which are used by the various functions to handle strings
|
* Misc. routines which are used by the various functions to handle strings
|
||||||
* and memory allocation and pretty much anything else we can think of. Also,
|
* and memory allocation and pretty much anything else we can think of. Also,
|
||||||
@ -24,85 +24,12 @@
|
|||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "conns.h"
|
#include "conns.h"
|
||||||
#include "filter.h"
|
#include "filter.h"
|
||||||
|
#include "heap.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "network.h"
|
||||||
#include "sock.h"
|
#include "sock.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
/*
|
|
||||||
* These are the debugging calloc, malloc, and free versions
|
|
||||||
*/
|
|
||||||
#ifndef NDEBUG
|
|
||||||
|
|
||||||
void *
|
|
||||||
debugging_calloc(size_t nmemb, size_t size, const char *file,
|
|
||||||
unsigned long line)
|
|
||||||
{
|
|
||||||
void *ptr;
|
|
||||||
|
|
||||||
assert(nmemb > 0);
|
|
||||||
assert(size > 0);
|
|
||||||
|
|
||||||
ptr = calloc(nmemb, size);
|
|
||||||
fprintf(stderr, "{calloc: %p:%u x %u} %s:%lu\n", ptr, nmemb, size, file,
|
|
||||||
line);
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
debugging_malloc(size_t size, const char *file, unsigned long line)
|
|
||||||
{
|
|
||||||
void *ptr;
|
|
||||||
|
|
||||||
assert(size > 0);
|
|
||||||
|
|
||||||
ptr = malloc(size);
|
|
||||||
fprintf(stderr, "{malloc: %p:%u} %s:%lu\n", ptr, size, file, line);
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
debugging_realloc(void *ptr, size_t size, const char *file, unsigned long line)
|
|
||||||
{
|
|
||||||
void *newptr;
|
|
||||||
|
|
||||||
assert(ptr != NULL);
|
|
||||||
assert(size > 0);
|
|
||||||
|
|
||||||
newptr = realloc(ptr, size);
|
|
||||||
fprintf(stderr, "{realloc: %p -> %p:%u} %s:%lu\n", ptr, newptr, size,
|
|
||||||
file, line);
|
|
||||||
return newptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
debugging_free(void *ptr, const char *file, unsigned long line)
|
|
||||||
{
|
|
||||||
assert(ptr != NULL);
|
|
||||||
|
|
||||||
fprintf(stderr, "{free: %p} %s:%lu\n", ptr, file, line);
|
|
||||||
free(ptr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
char*
|
|
||||||
debugging_strdup(const char* s, const char* file, unsigned long line)
|
|
||||||
{
|
|
||||||
char* ptr;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
assert(s != NULL);
|
|
||||||
|
|
||||||
len = strlen(s) + 1;
|
|
||||||
ptr = malloc(len);
|
|
||||||
if (!ptr)
|
|
||||||
return NULL;
|
|
||||||
memcpy(ptr, s, len);
|
|
||||||
|
|
||||||
fprintf(stderr, "{strdup: %p:%u} %s:%lu\n", ptr, len, file, line);
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define HEADER_SIZE (1024 * 8)
|
#define HEADER_SIZE (1024 * 8)
|
||||||
/*
|
/*
|
||||||
@ -207,26 +134,6 @@ indicate_http_error(struct conn_s* connptr, int number, const char* string)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
makedaemon(void)
|
|
||||||
{
|
|
||||||
if (fork() != 0)
|
|
||||||
exit(0);
|
|
||||||
|
|
||||||
setsid();
|
|
||||||
signal(SIGHUP, SIG_IGN);
|
|
||||||
|
|
||||||
if (fork() != 0)
|
|
||||||
exit(0);
|
|
||||||
|
|
||||||
chdir("/");
|
|
||||||
umask(077);
|
|
||||||
|
|
||||||
close(0);
|
|
||||||
close(1);
|
|
||||||
close(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Safely creates filename and returns the low-level file descriptor.
|
* Safely creates filename and returns the low-level file descriptor.
|
||||||
*/
|
*/
|
||||||
@ -364,82 +271,6 @@ pidfile_create(const char *filename)
|
|||||||
fclose(fd);
|
fclose(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef HAVE_STRLCPY
|
|
||||||
/*
|
|
||||||
* Function API taken from OpenBSD. Like strncpy(), but does not 0 fill the
|
|
||||||
* buffer, and always NULL terminates the buffer. size is the size of the
|
|
||||||
* destination buffer.
|
|
||||||
*/
|
|
||||||
size_t
|
|
||||||
strlcpy(char *dst, const char *src, size_t size)
|
|
||||||
{
|
|
||||||
size_t len = strlen(src);
|
|
||||||
size_t ret = len;
|
|
||||||
|
|
||||||
if (len >= size)
|
|
||||||
len = size - 1;
|
|
||||||
|
|
||||||
memcpy(dst, src, len);
|
|
||||||
dst[len] = '\0';
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef HAVE_STRLCAT
|
|
||||||
/*
|
|
||||||
* Function API taken from OpenBSD. Like strncat(), but does not 0 fill the
|
|
||||||
* buffer, and always NULL terminates the buffer. size is the length of the
|
|
||||||
* buffer, which should be one more than the maximum resulting string
|
|
||||||
* length.
|
|
||||||
*/
|
|
||||||
size_t
|
|
||||||
strlcat(char *dst, const char *src, size_t size)
|
|
||||||
{
|
|
||||||
size_t len1 = strlen(dst);
|
|
||||||
size_t len2 = strlen(src);
|
|
||||||
size_t ret = len1 + len2;
|
|
||||||
|
|
||||||
if (len1 + len2 >= size)
|
|
||||||
len2 = size - len1 - 1;
|
|
||||||
if (len2 > 0) {
|
|
||||||
memcpy(dst + len1, src, len2);
|
|
||||||
dst[len1 + len2] = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Removes any new-line or carriage-return characters from the end of the
|
|
||||||
* string. This function is named afrer the same function in Perl.
|
|
||||||
* "length" should be the number of characters in the buffer, not including
|
|
||||||
* the trailing NULL.
|
|
||||||
*
|
|
||||||
* Returns the number of characters removed from the end of the string.
|
|
||||||
*/
|
|
||||||
size_t
|
|
||||||
chomp(char *buffer, size_t length)
|
|
||||||
{
|
|
||||||
size_t chars;
|
|
||||||
|
|
||||||
assert(buffer != NULL);
|
|
||||||
|
|
||||||
chars = 0;
|
|
||||||
|
|
||||||
--length;
|
|
||||||
while (buffer[length] == '\r' || buffer[length] == '\n') {
|
|
||||||
buffer[length--] = '\0';
|
|
||||||
chars++;
|
|
||||||
|
|
||||||
if (length < 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return chars;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Rotate the current log file. This is performed whenever a SIGHUP is
|
* Rotate the current log file. This is performed whenever a SIGHUP is
|
||||||
* received.
|
* received.
|
||||||
|
55
src/utils.h
55
src/utils.h
@ -1,4 +1,4 @@
|
|||||||
/* $Id: utils.h,v 1.17 2002-05-17 16:39:35 rjkaes Exp $
|
/* $Id: utils.h,v 1.18 2002-05-23 18:28:12 rjkaes Exp $
|
||||||
*
|
*
|
||||||
* See 'utils.h' for a detailed description.
|
* See 'utils.h' for a detailed description.
|
||||||
*
|
*
|
||||||
@ -16,67 +16,22 @@
|
|||||||
* General Public License for more details.
|
* General Public License for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _TINYPROXY_UTILS_H_
|
#ifndef TINYPROXY_UTILS_H
|
||||||
#define _TINYPROXY_UTILS_H_
|
#define TINYPROXY_UTILS_H
|
||||||
|
|
||||||
#include "tinyproxy.h"
|
|
||||||
|
|
||||||
#include "conns.h"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Error codes used within the utility functions.
|
* Forward declaration.
|
||||||
*/
|
*/
|
||||||
#define EERROR 1 /* Generic error */
|
struct conn_s;
|
||||||
#define ENOMEMORY 2 /* Out of memory (or allocation error) */
|
|
||||||
#define EOUTRANGE 3 /* The variable is out of range */
|
|
||||||
|
|
||||||
extern int send_http_message(struct conn_s *connptr, int http_code,
|
extern int send_http_message(struct conn_s *connptr, int http_code,
|
||||||
const char *error_title, const char *message);
|
const char *error_title, const char *message);
|
||||||
extern int send_http_error_message(struct conn_s *connptr);
|
extern int send_http_error_message(struct conn_s *connptr);
|
||||||
extern int indicate_http_error(struct conn_s* connptr, int number, const char *string);
|
extern int indicate_http_error(struct conn_s* connptr, int number, const char *string);
|
||||||
|
|
||||||
extern void makedaemon(void);
|
|
||||||
extern void pidfile_create(const char *path);
|
extern void pidfile_create(const char *path);
|
||||||
|
|
||||||
extern int create_file_safely(const char *filename);
|
extern int create_file_safely(const char *filename);
|
||||||
|
|
||||||
#ifndef HAVE_STRLCAT
|
|
||||||
extern size_t strlcat(char *dst, const char *src, size_t size);
|
|
||||||
#endif /* HAVE_STRLCAT */
|
|
||||||
|
|
||||||
#ifndef HAVE_STRLCPY
|
|
||||||
extern size_t strlcpy(char *dst, const char *src, size_t size);
|
|
||||||
#endif /* HAVE_STRLCPY */
|
|
||||||
|
|
||||||
extern size_t chomp(char *buffer, size_t length);
|
|
||||||
extern void rotate_log_files(void);
|
extern void rotate_log_files(void);
|
||||||
|
|
||||||
/*
|
|
||||||
* The following is to allow for better memory checking.
|
|
||||||
*/
|
|
||||||
#ifndef NDEBUG
|
|
||||||
|
|
||||||
extern void *debugging_calloc(size_t nmemb, size_t size, const char *file,
|
|
||||||
unsigned long line);
|
|
||||||
extern void *debugging_malloc(size_t size, const char *file,
|
|
||||||
unsigned long line);
|
|
||||||
extern void debugging_free(void *ptr, const char *file, unsigned long line);
|
|
||||||
extern void *debugging_realloc(void *ptr, size_t size, const char *file,
|
|
||||||
unsigned long line);
|
|
||||||
extern char *debugging_strdup(const char* s, const char* file,
|
|
||||||
unsigned long line);
|
|
||||||
|
|
||||||
# define safecalloc(x, y) debugging_calloc(x, y, __FILE__, __LINE__)
|
|
||||||
# define safemalloc(x) debugging_malloc(x, __FILE__, __LINE__)
|
|
||||||
# define saferealloc(x, y) debugging_realloc(x, y, __FILE__, __LINE__)
|
|
||||||
# define safefree(x) debugging_free(x, __FILE__, __LINE__); (x) = NULL
|
|
||||||
# define safestrdup(x) debugging_strdup(x, __FILE__, __LINE__)
|
|
||||||
#else
|
|
||||||
# define safecalloc(x, y) calloc(x, y)
|
|
||||||
# define safemalloc(x) malloc(x)
|
|
||||||
# define saferealloc(x, y) realloc(x, y)
|
|
||||||
# define safefree(x) free(x); (x) = NULL
|
|
||||||
# define safestrdup(x) strdup(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user