//Needed because it defines the _POSIX_IPV6
#ifndef WEBORF_OPTIONS_H
#define WEBORF_OPTIONS_H
#define NAME PACKAGE
//#define VERSION PACKAGE_VERSION
#define SIGNATURE NAME "/" VERSION " (GNU/Linux)"
//----------System
#define ROOTUID 0 //Uid for superuser
//----------Network
#define MAXQ 40 //Queue for connect requests
#define PORT "8080" //Default port
#ifdef _POSIX_IPV6 //Enables ipv6 if supported
//Delete the following line to use IPv4 instead.
#define IPV6
#endif
//-----------Threads
#define MAXTHREAD 300 //Max threads
#define INITIALTHREAD 12 //Thread started when free threads are low and when starting
#define LOWTHREAD 3 //Minimum number of free threads, before starting new ones
#define MAXFREETHREAD 12 //Maximum number of free threads, before starting to slowly close them
#define THREADCONTROL 10 //Polling frequence in seconds
//------------Server
#define INDEX "index.html" //Default index file that weborf will search
#define BASEDIR "/var/www" //Default basedir
#define READ_TIMEOUT 5000 //Timeout before closing inactive keep-alive connections, in milliseconds
//------------Buffers
#define INBUFFER 1024 //Size for buffer with the HTTP request
#define FILEBUF 4096 //Size of reads
#define MAXSCRIPTOUT 512000 //Maximum size for a page generated by a script or internally
#define HEADBUF 1024 //Buffer for headers
#define PWDLIMIT 300 //Max size for password
#define INDEXMAXLEN 30
#define NBUFFER 15 //Buffer to contain the string representation of an integer
#define RBUFFER 128 //Buffer to contain a range
#define BUFFERED_READER_SIZE 2048
#define DATEBUFFER 50 //Buffer for text date
#define URI_LEN 256
#define PATH_LEN 1024
#define MIMETYPELEN 15 //Size of mimetype string
//Number of index pages allowed to search
#define MAXINDEXCOUNT 10
//-------------LIMITS
#define POST_MAX_SIZE 2000000 //Maximum allowed size for POST data
//-------------HTML
#define HTMLHEAD "" NAME " "
#define HTMLFOOT "
"
//#define HTMLHEAD "" NAME ""
//#define HTMLFOOT "Generated by " SIGNATURE "
"
//-------------SCRIPTS
#define SCRPT_TIMEOUT 30 //Timeout for the scripts, in seconds
#define HIDE_CGI_ERRORS //Hides all the errors.
//#define CGI_PHP "@cgibindir@/php5"
//#define CGI_PY "@cgibindir@/weborf_py_wrapper"
#define CGI_PHP "/usr/lib/cgi-bin/php5"
#define CGI_PY "/usr/lib/cgi-bin/weborf_py_wrapper"
//-------------COMPRESSING PAGES
//#define __COMPRESSION //enables support for compressing pages, comment to disable
#ifdef __COMPRESSION
#define SIZE_COMPRESS_MIN 512
#define SIZE_COMPRESS_MAX 4000000000
#define GZIPNICE 4 //Nice value for gzip process
#endif
//The following header can be disabled to increase a little the speed
//#define SEND_LAST_MODIFIED_HEADER
#ifdef HAVE_LIBMAGIC
#define SEND_MIMETYPES //Enables support to sending the mimetype to the client
#endif
//-------------RANGE
#define __RANGE //Enables support to range (partial download)
//-------------WEBDAV
#define WEBDAV //Enables webdav support
#ifdef WEBDAV
#define MAXPROPCOUNT 40 //Number of supported properties
#define HIDE_HIDDEN_FILES //Hides hidden files
#endif
//-------------Logging options
//#define THREADDBG
//#define SOCKETDBG
//#define SENDINGDBG
#define SERVERDBG
#define REQUESTDBG
#endif
weborf-0.13/cgi.c 0000644 0000765 0000765 00000030626 11537207462 010600 0000000 0000000 /*
Weborf
Copyright (C) 2010 Salvo "LtWorf" Tomaselli
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
@author Salvo "LtWorf" Tomaselli
*/
#include "options.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "mystring.h"
#include "cgi.h"
#include "types.h"
#include "instance.h"
extern char ** environ; //To reset environ vars
extern weborf_configuration_t weborf_conf;
/**
* This function will set enviromental variables mapping the HTTP request.
* Each variable will be prefixed with "HTTP_" and will be converted to
* upper case.
*/
static inline void cgi_set_http_env_vars(char *http_param) { //Sets Enviroment vars
if (http_param == NULL)
return;
char *lasts;
char *param;
int i;
int p_len;
//Removes the 1st part with the protocol
param = strtok_r(http_param, "\r\n", &lasts);
setenv("SERVER_PROTOCOL", param, true);
char hparam[200];
hparam[0] = 'H';
hparam[1] = 'T';
hparam[2] = 'T';
hparam[3] = 'P';
hparam[4] = '_';
//Cycles parameters
while ((param = strtok_r(NULL, "\r\n", &lasts)) != NULL) {
p_len = lasts-param-1;
char *value = NULL;
//Parses the parameter to split name from value
for (i = 0; i < p_len; i++) {
if (param[i] == ':' && param[i + 1] == ' ') {
param[i] = '\0';
value = ¶m[i + 2];
strToUpper(param); //Converts to upper case
strReplace(param, "-", '_');
memccpy(hparam+5,param,'\0',195);
setenv(hparam, value, true);
break;
}
}
}
}
/**
* Sets the SERVER_ADDR enviromental variable to be the string representation
* of the SERVER IP ADDRESS which is being used by the socket
* */
static inline void cgi_set_SERVER_ADDR_PORT(int sock) {
#ifdef IPV6
char loc_addr[INET6_ADDRSTRLEN];
struct sockaddr_in6 addr;
socklen_t addr_l=sizeof(struct sockaddr_in6);
getsockname(sock, (struct sockaddr *)&addr, &addr_l);
inet_ntop(AF_INET6, &addr.sin6_addr,(char*)&loc_addr, INET6_ADDRSTRLEN);
#else
char loc_addr[INET_ADDRSTRLEN];
struct sockaddr_in addr;
int addr_l=sizeof(struct sockaddr_in);
getsockname(sock, (struct sockaddr *)&addr,(socklen_t *) &addr_l);
inet_ntop(AF_INET, &addr.sin_addr,(char*)&loc_addr, INET_ADDRSTRLEN);
#endif
setenv("SERVER_ADDR",(char*)&loc_addr,true);
//TODO
setenv("SERVER_PORT",weborf_conf.port,true);
}
/**
* Sets env vars required by the CGI protocol
* SERVER_SIGNATURE
* SERVER_SOFTWARE
* SERVER_NAME
* GATEWAY_INTERFACE
* REQUEST_METHOD
* REDIRECT_STATUS (this is not specified in the CGI protocol but is needed to make php work)
* SCRIPT_FILENAME
* DOCUMENT_ROOT
* REMOTE_ADDR
* SCRIPT_NAME
* REQUEST_URI (It is expected that the request is like /blblabla?query and not in two separate locations, ? is expected to be replaced by \0)
* QUERY_STRING
* */
static inline void cgi_set_env_vars(connection_t *connection_prop,char *real_basedir) {
//Set CGI needed vars
setenv("SERVER_SIGNATURE",SIGNATURE,true);
setenv("SERVER_SOFTWARE",SIGNATURE,true);
setenv("GATEWAY_INTERFACE","CGI/1.1",true);
setenv("REQUEST_METHOD",connection_prop->method,true); //POST GET
setenv("SERVER_NAME", getenv("HTTP_HOST"),true); //TODO for older http version this header might not exist
setenv("REDIRECT_STATUS","Ciao",true); // Mah.. i'll never understand php, this env var is needed
setenv("SCRIPT_FILENAME",connection_prop->strfile,true); //This var is needed as well or php say no input file...
setenv("DOCUMENT_ROOT",real_basedir,true);
setenv("REMOTE_ADDR",connection_prop->ip_addr,true); //Client's address
setenv("SCRIPT_NAME",connection_prop->page,true); //Name of the script without complete path
//Request URI with or without a query
if (connection_prop->get_params==NULL) {
setenv("REQUEST_URI",connection_prop->page,true);
setenv("QUERY_STRING","",true);//Query after ?
} else {
setenv("QUERY_STRING",connection_prop->get_params,true);//Query after ?
//file and params were the same string.
//Joining them again temporarily
int delim=connection_prop->page_len;
connection_prop->page[delim]='?';
setenv("REQUEST_URI",connection_prop->page,true);
connection_prop->page[delim]='\0';
}
}
/**
* Sets env vars CONTENT_LENGTH and CONTENT_TYPE
* just copying their value from HTTP_CONTENT_LENGTH
* and HTTP_CONTENT_TYPE
* Hence to work the HTTP_* env variables must be
* already set
* */
static inline void cgi_set_env_content_length() {
//If Content-Length field exists
char *content_l=getenv("HTTP_CONTENT_LENGTH");
if (content_l!=NULL) {
setenv("CONTENT_LENGTH",content_l,true);
setenv("CONTENT_TYPE",getenv("HTTP_CONTENT_TYPE"),true);
}
}
/**
* Will chdir to the same directory that contains the CGI script
* */
static inline void cgi_child_chdir(connection_t *connection_prop) {
//chdir to the directory
char* last_slash=rindex(connection_prop->strfile,'/');
last_slash[0]=0;
chdir(connection_prop->strfile);
}
/**
* Closes unused ends of pipes, dups them,
* sets the correct enviromental variables requested
* by the CGI protocol and then executes the CGI.
*
* Will also set an alarm to try to prevent the script from
* running forever.
* */
static inline void cgi_execute_child(connection_t* connection_prop,string_t* post_param,char * executor,char* real_basedir,int *wpipe,int *ipipe) {
close (wpipe[0]); //Closes unused end of the pipe
close (STDOUT);
dup(wpipe[1]); //Redirects the stdout
#ifdef HIDE_CGI_ERRORS
close (STDERR);
#endif
//Redirecting standard input only if there is POST data
if (post_param->data!=NULL) {//Send post data to script's stdin
close(STDIN);
dup(ipipe[0]);
}
environ=NULL; //Clear env vars
cgi_set_http_env_vars(connection_prop->http_param);
cgi_set_SERVER_ADDR_PORT(connection_prop->sock);
cgi_set_env_vars(connection_prop,real_basedir);
cgi_set_env_content_length();
cgi_child_chdir(connection_prop);
alarm(SCRPT_TIMEOUT);//Sets the timeout for the script
execl(executor,executor,(char *)0);
#ifdef SERVERDBG
syslog(LOG_ERR,"Execution of %s failed",executor);
perror("Execution of the page failed");
#endif
exit(1);
}
static inline int cgi_waitfor_child(connection_t* connection_prop,string_t* post_param,char * executor,pid_t wpid,int *wpipe,int *ipipe) {
int sock=connection_prop->sock;
//Closing pipes, so if they're empty read is non blocking
close (wpipe[1]);
//Large buffer, must contain the output of the script
char* header_buf=malloc(MAXSCRIPTOUT+HEADBUF);
if (header_buf==NULL) { //Was unable to allocate the buffer
int state;
#ifdef SERVERDBG
syslog(LOG_CRIT,"Not enough memory to allocate buffers for CGI");
#endif
close (wpipe[0]);
if (post_param->data!=NULL) {//Pipe created and used only if there is data to send to the script
close (ipipe[0]); //Closes unused end of the pipe
close (ipipe[1]); //Closes the pipe
}
kill(wpid,SIGKILL); //Kills cgi process
waitpid (wpid,&state,0); //Removes zombie process
return ERR_NOMEM;//Returns if buffer was not allocated
}
if (post_param->data!=NULL) {//Pipe created and used only if there is data to send to the script
//Send post data to script's stdin
write(ipipe[1],post_param->data,post_param->len);
close (ipipe[0]); //Closes unused end of the pipe
close (ipipe[1]); //Closes the pipe
}
{
int state;
waitpid (wpid,&state,0); //Wait the termination of the script
}
//Reads output of the script
int e_reads=read(wpipe[0],header_buf,MAXSCRIPTOUT+HEADBUF);
//Separating header from contents
char* scrpt_buf=strstr(header_buf,"\r\n\r\n");
int reads=0;
if (scrpt_buf!=NULL) {
scrpt_buf+=2;
scrpt_buf[0]=0;
scrpt_buf=scrpt_buf+2;
reads=e_reads - (int)(scrpt_buf-header_buf);//Len of the content
} else {//Something went wrong, ignoring the output (it's missing the headers)
e_reads=0;
}
if (e_reads>0) {//There is output from script
unsigned int status; //Standard status
{
//Reading if there is another status
char*s=strstr(header_buf,"Status: ");
if (s!=NULL) {
status=(unsigned int)strtoul( s+8 , NULL, 0 );
} else {
status=200; //Standard status
}
}
/* There could be other data, which didn't fit in the buffer,
so we set reads to -1 (this will make connection non-keep-alive)
and we continue reading and writing to the socket */
if (e_reads==MAXSCRIPTOUT+HEADBUF) {
reads=-1;
connection_prop->keep_alive=false;
}
/*
Sends header,
reads is the size
true tells to use Content-Length rather than entity-length
-1 won't use any ETag, and will eventually use the current time as last-modified
*/
send_http_header(status,reads,header_buf,true,-1,connection_prop);
if (reads!=0) {//Sends the page if there is something to send
write (sock,scrpt_buf,reads);
}
if (reads==-1) {//Reading until the pipe is empty, if it wasn't fully read before
e_reads=MAXSCRIPTOUT+HEADBUF;
while (e_reads==MAXSCRIPTOUT+HEADBUF) {
e_reads=read(wpipe[0],header_buf,MAXSCRIPTOUT+HEADBUF);
write (sock,header_buf,e_reads);
}
}
//Closing pipe
close (wpipe[0]);
} else {//No output from script, maybe terminated...
send_err(connection_prop,500,"Internal server error");
}
free(header_buf);
return 0;
}
/**
Executes a CGI script with a given interpreter and sends the resulting output
executor is the path to the binary which will execute the page
post_param contains the post data sent to the page (if present). This can't be null, but the string pointer inside the struct can be null.
real_basedir is the basedir (according to the virtualhost)
connection_prop is the struct containing all the data of the request
exec_page will fork and create pipes with the child.
The child will clean all the envvars and then set new ones as needed by CGI.
Then the child will call alarm to set the timeout to its execution, and then will exec the script.
*/
int exec_page(char * executor,string_t* post_param,char* real_basedir,connection_t* connection_prop) {
#ifdef SENDINGDBG
syslog(LOG_INFO,"Executing file %s",connection_prop->strfile);
#endif
int wpid;//Child's pid
int wpipe[2];//Pipe's file descriptor
int ipipe[2];//Pipe's file descriptor, used to pass POST on script's standard input
//Pipe created and used only if there is POST data to send to the script
if (post_param->data!=NULL) {
pipe(ipipe);//Pipe to comunicate with the child
}
//Pipe to get the output of the child
pipe(wpipe);//Pipe to comunicate with the child
if ((wpid=fork())<0) { //Error, returns a no memory error
#ifdef SENDINGDBG
syslog(LOG_CRIT,"Unable to fork to execute the file %s",connection_prop->strfile);
#endif
if (post_param->data!=NULL) {
close(ipipe[0]);
close(ipipe[1]);
}
close(wpipe[1]);
close(wpipe[0]);
return ERR_NOMEM;
} else if (wpid==0) {
/* never returns */
cgi_execute_child(connection_prop,post_param,executor,real_basedir,wpipe,ipipe);
} else { //Father: reads from pipe and sends
return cgi_waitfor_child(connection_prop,post_param,executor,wpid,wpipe,ipipe);
}
return 0;
}
weborf-0.13/cgi.h 0000644 0000765 0000765 00000001707 11536415542 010602 0000000 0000000 /*
Weborf
Copyright (C) 2010 Salvo "LtWorf" Tomaselli
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
@author Salvo "LtWorf" Tomaselli
*/
#ifndef WEBORF_CGI_H
#define WEBORF_CGI_H
#include "options.h"
#include "types.h"
#define STDIN 0
#define STDOUT 1
#define STDERR 2
int exec_page(char * executor,string_t* post_param,char* real_basedir,connection_t* connection_prop);
#endif weborf-0.13/aclocal.m4 0000644 0000765 0000765 00000110657 11543367105 011533 0000000 0000000 # generated automatically by aclocal 1.11.1 -*- Autoconf -*-
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
# 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
m4_ifndef([AC_AUTOCONF_VERSION],
[m4_copy([m4_PACKAGE_VERSION], [AC_AUTOCONF_VERSION])])dnl
m4_if(m4_defn([AC_AUTOCONF_VERSION]), [2.67],,
[m4_warning([this file was generated for autoconf 2.67.
You have another version of autoconf. It may work, but is not guaranteed to.
If you have problems, you may need to regenerate the build system entirely.
To do so, use the procedure documented by the package, typically `autoreconf'.])])
# Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# AM_AUTOMAKE_VERSION(VERSION)
# ----------------------------
# Automake X.Y traces this macro to ensure aclocal.m4 has been
# generated from the m4 files accompanying Automake X.Y.
# (This private macro should not be called outside this file.)
AC_DEFUN([AM_AUTOMAKE_VERSION],
[am__api_version='1.11'
dnl Some users find AM_AUTOMAKE_VERSION and mistake it for a way to
dnl require some minimum version. Point them to the right macro.
m4_if([$1], [1.11.1], [],
[AC_FATAL([Do not call $0, use AM_INIT_AUTOMAKE([$1]).])])dnl
])
# _AM_AUTOCONF_VERSION(VERSION)
# -----------------------------
# aclocal traces this macro to find the Autoconf version.
# This is a private macro too. Using m4_define simplifies
# the logic in aclocal, which can simply ignore this definition.
m4_define([_AM_AUTOCONF_VERSION], [])
# AM_SET_CURRENT_AUTOMAKE_VERSION
# -------------------------------
# Call AM_AUTOMAKE_VERSION and AM_AUTOMAKE_VERSION so they can be traced.
# This function is AC_REQUIREd by AM_INIT_AUTOMAKE.
AC_DEFUN([AM_SET_CURRENT_AUTOMAKE_VERSION],
[AM_AUTOMAKE_VERSION([1.11.1])dnl
m4_ifndef([AC_AUTOCONF_VERSION],
[m4_copy([m4_PACKAGE_VERSION], [AC_AUTOCONF_VERSION])])dnl
_AM_AUTOCONF_VERSION(m4_defn([AC_AUTOCONF_VERSION]))])
# AM_AUX_DIR_EXPAND -*- Autoconf -*-
# Copyright (C) 2001, 2003, 2005 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# For projects using AC_CONFIG_AUX_DIR([foo]), Autoconf sets
# $ac_aux_dir to `$srcdir/foo'. In other projects, it is set to
# `$srcdir', `$srcdir/..', or `$srcdir/../..'.
#
# Of course, Automake must honor this variable whenever it calls a
# tool from the auxiliary directory. The problem is that $srcdir (and
# therefore $ac_aux_dir as well) can be either absolute or relative,
# depending on how configure is run. This is pretty annoying, since
# it makes $ac_aux_dir quite unusable in subdirectories: in the top
# source directory, any form will work fine, but in subdirectories a
# relative path needs to be adjusted first.
#
# $ac_aux_dir/missing
# fails when called from a subdirectory if $ac_aux_dir is relative
# $top_srcdir/$ac_aux_dir/missing
# fails if $ac_aux_dir is absolute,
# fails when called from a subdirectory in a VPATH build with
# a relative $ac_aux_dir
#
# The reason of the latter failure is that $top_srcdir and $ac_aux_dir
# are both prefixed by $srcdir. In an in-source build this is usually
# harmless because $srcdir is `.', but things will broke when you
# start a VPATH build or use an absolute $srcdir.
#
# So we could use something similar to $top_srcdir/$ac_aux_dir/missing,
# iff we strip the leading $srcdir from $ac_aux_dir. That would be:
# am_aux_dir='\$(top_srcdir)/'`expr "$ac_aux_dir" : "$srcdir//*\(.*\)"`
# and then we would define $MISSING as
# MISSING="\${SHELL} $am_aux_dir/missing"
# This will work as long as MISSING is not called from configure, because
# unfortunately $(top_srcdir) has no meaning in configure.
# However there are other variables, like CC, which are often used in
# configure, and could therefore not use this "fixed" $ac_aux_dir.
#
# Another solution, used here, is to always expand $ac_aux_dir to an
# absolute PATH. The drawback is that using absolute paths prevent a
# configured tree to be moved without reconfiguration.
AC_DEFUN([AM_AUX_DIR_EXPAND],
[dnl Rely on autoconf to set up CDPATH properly.
AC_PREREQ([2.50])dnl
# expand $ac_aux_dir to an absolute path
am_aux_dir=`cd $ac_aux_dir && pwd`
])
# AM_CONDITIONAL -*- Autoconf -*-
# Copyright (C) 1997, 2000, 2001, 2003, 2004, 2005, 2006, 2008
# Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 9
# AM_CONDITIONAL(NAME, SHELL-CONDITION)
# -------------------------------------
# Define a conditional.
AC_DEFUN([AM_CONDITIONAL],
[AC_PREREQ(2.52)dnl
ifelse([$1], [TRUE], [AC_FATAL([$0: invalid condition: $1])],
[$1], [FALSE], [AC_FATAL([$0: invalid condition: $1])])dnl
AC_SUBST([$1_TRUE])dnl
AC_SUBST([$1_FALSE])dnl
_AM_SUBST_NOTMAKE([$1_TRUE])dnl
_AM_SUBST_NOTMAKE([$1_FALSE])dnl
m4_define([_AM_COND_VALUE_$1], [$2])dnl
if $2; then
$1_TRUE=
$1_FALSE='#'
else
$1_TRUE='#'
$1_FALSE=
fi
AC_CONFIG_COMMANDS_PRE(
[if test -z "${$1_TRUE}" && test -z "${$1_FALSE}"; then
AC_MSG_ERROR([[conditional "$1" was never defined.
Usually this means the macro was only invoked conditionally.]])
fi])])
# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2009
# Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 10
# There are a few dirty hacks below to avoid letting `AC_PROG_CC' be
# written in clear, in which case automake, when reading aclocal.m4,
# will think it sees a *use*, and therefore will trigger all it's
# C support machinery. Also note that it means that autoscan, seeing
# CC etc. in the Makefile, will ask for an AC_PROG_CC use...
# _AM_DEPENDENCIES(NAME)
# ----------------------
# See how the compiler implements dependency checking.
# NAME is "CC", "CXX", "GCJ", or "OBJC".
# We try a few techniques and use that to set a single cache variable.
#
# We don't AC_REQUIRE the corresponding AC_PROG_CC since the latter was
# modified to invoke _AM_DEPENDENCIES(CC); we would have a circular
# dependency, and given that the user is not expected to run this macro,
# just rely on AC_PROG_CC.
AC_DEFUN([_AM_DEPENDENCIES],
[AC_REQUIRE([AM_SET_DEPDIR])dnl
AC_REQUIRE([AM_OUTPUT_DEPENDENCY_COMMANDS])dnl
AC_REQUIRE([AM_MAKE_INCLUDE])dnl
AC_REQUIRE([AM_DEP_TRACK])dnl
ifelse([$1], CC, [depcc="$CC" am_compiler_list=],
[$1], CXX, [depcc="$CXX" am_compiler_list=],
[$1], OBJC, [depcc="$OBJC" am_compiler_list='gcc3 gcc'],
[$1], UPC, [depcc="$UPC" am_compiler_list=],
[$1], GCJ, [depcc="$GCJ" am_compiler_list='gcc3 gcc'],
[depcc="$$1" am_compiler_list=])
AC_CACHE_CHECK([dependency style of $depcc],
[am_cv_$1_dependencies_compiler_type],
[if test -z "$AMDEP_TRUE" && test -f "$am_depcomp"; then
# We make a subdir and do the tests there. Otherwise we can end up
# making bogus files that we don't know about and never remove. For
# instance it was reported that on HP-UX the gcc test will end up
# making a dummy file named `D' -- because `-MD' means `put the output
# in D'.
mkdir conftest.dir
# Copy depcomp to subdir because otherwise we won't find it if we're
# using a relative directory.
cp "$am_depcomp" conftest.dir
cd conftest.dir
# We will build objects and dependencies in a subdirectory because
# it helps to detect inapplicable dependency modes. For instance
# both Tru64's cc and ICC support -MD to output dependencies as a
# side effect of compilation, but ICC will put the dependencies in
# the current directory while Tru64 will put them in the object
# directory.
mkdir sub
am_cv_$1_dependencies_compiler_type=none
if test "$am_compiler_list" = ""; then
am_compiler_list=`sed -n ['s/^#*\([a-zA-Z0-9]*\))$/\1/p'] < ./depcomp`
fi
am__universal=false
m4_case([$1], [CC],
[case " $depcc " in #(
*\ -arch\ *\ -arch\ *) am__universal=true ;;
esac],
[CXX],
[case " $depcc " in #(
*\ -arch\ *\ -arch\ *) am__universal=true ;;
esac])
for depmode in $am_compiler_list; do
# Setup a source with many dependencies, because some compilers
# like to wrap large dependency lists on column 80 (with \), and
# we should not choose a depcomp mode which is confused by this.
#
# We need to recreate these files for each test, as the compiler may
# overwrite some of them when testing with obscure command lines.
# This happens at least with the AIX C compiler.
: > sub/conftest.c
for i in 1 2 3 4 5 6; do
echo '#include "conftst'$i'.h"' >> sub/conftest.c
# Using `: > sub/conftst$i.h' creates only sub/conftst1.h with
# Solaris 8's {/usr,}/bin/sh.
touch sub/conftst$i.h
done
echo "${am__include} ${am__quote}sub/conftest.Po${am__quote}" > confmf
# We check with `-c' and `-o' for the sake of the "dashmstdout"
# mode. It turns out that the SunPro C++ compiler does not properly
# handle `-M -o', and we need to detect this. Also, some Intel
# versions had trouble with output in subdirs
am__obj=sub/conftest.${OBJEXT-o}
am__minus_obj="-o $am__obj"
case $depmode in
gcc)
# This depmode causes a compiler race in universal mode.
test "$am__universal" = false || continue
;;
nosideeffect)
# after this tag, mechanisms are not by side-effect, so they'll
# only be used when explicitly requested
if test "x$enable_dependency_tracking" = xyes; then
continue
else
break
fi
;;
msvisualcpp | msvcmsys)
# This compiler won't grok `-c -o', but also, the minuso test has
# not run yet. These depmodes are late enough in the game, and
# so weak that their functioning should not be impacted.
am__obj=conftest.${OBJEXT-o}
am__minus_obj=
;;
none) break ;;
esac
if depmode=$depmode \
source=sub/conftest.c object=$am__obj \
depfile=sub/conftest.Po tmpdepfile=sub/conftest.TPo \
$SHELL ./depcomp $depcc -c $am__minus_obj sub/conftest.c \
>/dev/null 2>conftest.err &&
grep sub/conftst1.h sub/conftest.Po > /dev/null 2>&1 &&
grep sub/conftst6.h sub/conftest.Po > /dev/null 2>&1 &&
grep $am__obj sub/conftest.Po > /dev/null 2>&1 &&
${MAKE-make} -s -f confmf > /dev/null 2>&1; then
# icc doesn't choke on unknown options, it will just issue warnings
# or remarks (even with -Werror). So we grep stderr for any message
# that says an option was ignored or not supported.
# When given -MP, icc 7.0 and 7.1 complain thusly:
# icc: Command line warning: ignoring option '-M'; no argument required
# The diagnosis changed in icc 8.0:
# icc: Command line remark: option '-MP' not supported
if (grep 'ignoring option' conftest.err ||
grep 'not supported' conftest.err) >/dev/null 2>&1; then :; else
am_cv_$1_dependencies_compiler_type=$depmode
break
fi
fi
done
cd ..
rm -rf conftest.dir
else
am_cv_$1_dependencies_compiler_type=none
fi
])
AC_SUBST([$1DEPMODE], [depmode=$am_cv_$1_dependencies_compiler_type])
AM_CONDITIONAL([am__fastdep$1], [
test "x$enable_dependency_tracking" != xno \
&& test "$am_cv_$1_dependencies_compiler_type" = gcc3])
])
# AM_SET_DEPDIR
# -------------
# Choose a directory name for dependency files.
# This macro is AC_REQUIREd in _AM_DEPENDENCIES
AC_DEFUN([AM_SET_DEPDIR],
[AC_REQUIRE([AM_SET_LEADING_DOT])dnl
AC_SUBST([DEPDIR], ["${am__leading_dot}deps"])dnl
])
# AM_DEP_TRACK
# ------------
AC_DEFUN([AM_DEP_TRACK],
[AC_ARG_ENABLE(dependency-tracking,
[ --disable-dependency-tracking speeds up one-time build
--enable-dependency-tracking do not reject slow dependency extractors])
if test "x$enable_dependency_tracking" != xno; then
am_depcomp="$ac_aux_dir/depcomp"
AMDEPBACKSLASH='\'
fi
AM_CONDITIONAL([AMDEP], [test "x$enable_dependency_tracking" != xno])
AC_SUBST([AMDEPBACKSLASH])dnl
_AM_SUBST_NOTMAKE([AMDEPBACKSLASH])dnl
])
# Generate code to set up dependency tracking. -*- Autoconf -*-
# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008
# Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
#serial 5
# _AM_OUTPUT_DEPENDENCY_COMMANDS
# ------------------------------
AC_DEFUN([_AM_OUTPUT_DEPENDENCY_COMMANDS],
[{
# Autoconf 2.62 quotes --file arguments for eval, but not when files
# are listed without --file. Let's play safe and only enable the eval
# if we detect the quoting.
case $CONFIG_FILES in
*\'*) eval set x "$CONFIG_FILES" ;;
*) set x $CONFIG_FILES ;;
esac
shift
for mf
do
# Strip MF so we end up with the name of the file.
mf=`echo "$mf" | sed -e 's/:.*$//'`
# Check whether this is an Automake generated Makefile or not.
# We used to match only the files named `Makefile.in', but
# some people rename them; so instead we look at the file content.
# Grep'ing the first line is not enough: some people post-process
# each Makefile.in and add a new line on top of each file to say so.
# Grep'ing the whole file is not good either: AIX grep has a line
# limit of 2048, but all sed's we know have understand at least 4000.
if sed -n 's,^#.*generated by automake.*,X,p' "$mf" | grep X >/dev/null 2>&1; then
dirpart=`AS_DIRNAME("$mf")`
else
continue
fi
# Extract the definition of DEPDIR, am__include, and am__quote
# from the Makefile without running `make'.
DEPDIR=`sed -n 's/^DEPDIR = //p' < "$mf"`
test -z "$DEPDIR" && continue
am__include=`sed -n 's/^am__include = //p' < "$mf"`
test -z "am__include" && continue
am__quote=`sed -n 's/^am__quote = //p' < "$mf"`
# When using ansi2knr, U may be empty or an underscore; expand it
U=`sed -n 's/^U = //p' < "$mf"`
# Find all dependency output files, they are included files with
# $(DEPDIR) in their names. We invoke sed twice because it is the
# simplest approach to changing $(DEPDIR) to its actual value in the
# expansion.
for file in `sed -n "
s/^$am__include $am__quote\(.*(DEPDIR).*\)$am__quote"'$/\1/p' <"$mf" | \
sed -e 's/\$(DEPDIR)/'"$DEPDIR"'/g' -e 's/\$U/'"$U"'/g'`; do
# Make sure the directory exists.
test -f "$dirpart/$file" && continue
fdir=`AS_DIRNAME(["$file"])`
AS_MKDIR_P([$dirpart/$fdir])
# echo "creating $dirpart/$file"
echo '# dummy' > "$dirpart/$file"
done
done
}
])# _AM_OUTPUT_DEPENDENCY_COMMANDS
# AM_OUTPUT_DEPENDENCY_COMMANDS
# -----------------------------
# This macro should only be invoked once -- use via AC_REQUIRE.
#
# This code is only required when automatic dependency tracking
# is enabled. FIXME. This creates each `.P' file that we will
# need in order to bootstrap the dependency handling code.
AC_DEFUN([AM_OUTPUT_DEPENDENCY_COMMANDS],
[AC_CONFIG_COMMANDS([depfiles],
[test x"$AMDEP_TRUE" != x"" || _AM_OUTPUT_DEPENDENCY_COMMANDS],
[AMDEP_TRUE="$AMDEP_TRUE" ac_aux_dir="$ac_aux_dir"])
])
# Do all the work for Automake. -*- Autoconf -*-
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
# 2005, 2006, 2008, 2009 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 16
# This macro actually does too much. Some checks are only needed if
# your package does certain things. But this isn't really a big deal.
# AM_INIT_AUTOMAKE(PACKAGE, VERSION, [NO-DEFINE])
# AM_INIT_AUTOMAKE([OPTIONS])
# -----------------------------------------------
# The call with PACKAGE and VERSION arguments is the old style
# call (pre autoconf-2.50), which is being phased out. PACKAGE
# and VERSION should now be passed to AC_INIT and removed from
# the call to AM_INIT_AUTOMAKE.
# We support both call styles for the transition. After
# the next Automake release, Autoconf can make the AC_INIT
# arguments mandatory, and then we can depend on a new Autoconf
# release and drop the old call support.
AC_DEFUN([AM_INIT_AUTOMAKE],
[AC_PREREQ([2.62])dnl
dnl Autoconf wants to disallow AM_ names. We explicitly allow
dnl the ones we care about.
m4_pattern_allow([^AM_[A-Z]+FLAGS$])dnl
AC_REQUIRE([AM_SET_CURRENT_AUTOMAKE_VERSION])dnl
AC_REQUIRE([AC_PROG_INSTALL])dnl
if test "`cd $srcdir && pwd`" != "`pwd`"; then
# Use -I$(srcdir) only when $(srcdir) != ., so that make's output
# is not polluted with repeated "-I."
AC_SUBST([am__isrc], [' -I$(srcdir)'])_AM_SUBST_NOTMAKE([am__isrc])dnl
# test to see if srcdir already configured
if test -f $srcdir/config.status; then
AC_MSG_ERROR([source directory already configured; run "make distclean" there first])
fi
fi
# test whether we have cygpath
if test -z "$CYGPATH_W"; then
if (cygpath --version) >/dev/null 2>/dev/null; then
CYGPATH_W='cygpath -w'
else
CYGPATH_W=echo
fi
fi
AC_SUBST([CYGPATH_W])
# Define the identity of the package.
dnl Distinguish between old-style and new-style calls.
m4_ifval([$2],
[m4_ifval([$3], [_AM_SET_OPTION([no-define])])dnl
AC_SUBST([PACKAGE], [$1])dnl
AC_SUBST([VERSION], [$2])],
[_AM_SET_OPTIONS([$1])dnl
dnl Diagnose old-style AC_INIT with new-style AM_AUTOMAKE_INIT.
m4_if(m4_ifdef([AC_PACKAGE_NAME], 1)m4_ifdef([AC_PACKAGE_VERSION], 1), 11,,
[m4_fatal([AC_INIT should be called with package and version arguments])])dnl
AC_SUBST([PACKAGE], ['AC_PACKAGE_TARNAME'])dnl
AC_SUBST([VERSION], ['AC_PACKAGE_VERSION'])])dnl
_AM_IF_OPTION([no-define],,
[AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE", [Name of package])
AC_DEFINE_UNQUOTED(VERSION, "$VERSION", [Version number of package])])dnl
# Some tools Automake needs.
AC_REQUIRE([AM_SANITY_CHECK])dnl
AC_REQUIRE([AC_ARG_PROGRAM])dnl
AM_MISSING_PROG(ACLOCAL, aclocal-${am__api_version})
AM_MISSING_PROG(AUTOCONF, autoconf)
AM_MISSING_PROG(AUTOMAKE, automake-${am__api_version})
AM_MISSING_PROG(AUTOHEADER, autoheader)
AM_MISSING_PROG(MAKEINFO, makeinfo)
AC_REQUIRE([AM_PROG_INSTALL_SH])dnl
AC_REQUIRE([AM_PROG_INSTALL_STRIP])dnl
AC_REQUIRE([AM_PROG_MKDIR_P])dnl
# We need awk for the "check" target. The system "awk" is bad on
# some platforms.
AC_REQUIRE([AC_PROG_AWK])dnl
AC_REQUIRE([AC_PROG_MAKE_SET])dnl
AC_REQUIRE([AM_SET_LEADING_DOT])dnl
_AM_IF_OPTION([tar-ustar], [_AM_PROG_TAR([ustar])],
[_AM_IF_OPTION([tar-pax], [_AM_PROG_TAR([pax])],
[_AM_PROG_TAR([v7])])])
_AM_IF_OPTION([no-dependencies],,
[AC_PROVIDE_IFELSE([AC_PROG_CC],
[_AM_DEPENDENCIES(CC)],
[define([AC_PROG_CC],
defn([AC_PROG_CC])[_AM_DEPENDENCIES(CC)])])dnl
AC_PROVIDE_IFELSE([AC_PROG_CXX],
[_AM_DEPENDENCIES(CXX)],
[define([AC_PROG_CXX],
defn([AC_PROG_CXX])[_AM_DEPENDENCIES(CXX)])])dnl
AC_PROVIDE_IFELSE([AC_PROG_OBJC],
[_AM_DEPENDENCIES(OBJC)],
[define([AC_PROG_OBJC],
defn([AC_PROG_OBJC])[_AM_DEPENDENCIES(OBJC)])])dnl
])
_AM_IF_OPTION([silent-rules], [AC_REQUIRE([AM_SILENT_RULES])])dnl
dnl The `parallel-tests' driver may need to know about EXEEXT, so add the
dnl `am__EXEEXT' conditional if _AM_COMPILER_EXEEXT was seen. This macro
dnl is hooked onto _AC_COMPILER_EXEEXT early, see below.
AC_CONFIG_COMMANDS_PRE(dnl
[m4_provide_if([_AM_COMPILER_EXEEXT],
[AM_CONDITIONAL([am__EXEEXT], [test -n "$EXEEXT"])])])dnl
])
dnl Hook into `_AC_COMPILER_EXEEXT' early to learn its expansion. Do not
dnl add the conditional right here, as _AC_COMPILER_EXEEXT may be further
dnl mangled by Autoconf and run in a shell conditional statement.
m4_define([_AC_COMPILER_EXEEXT],
m4_defn([_AC_COMPILER_EXEEXT])[m4_provide([_AM_COMPILER_EXEEXT])])
# When config.status generates a header, we must update the stamp-h file.
# This file resides in the same directory as the config header
# that is generated. The stamp files are numbered to have different names.
# Autoconf calls _AC_AM_CONFIG_HEADER_HOOK (when defined) in the
# loop where config.status creates the headers, so we can generate
# our stamp files there.
AC_DEFUN([_AC_AM_CONFIG_HEADER_HOOK],
[# Compute $1's index in $config_headers.
_am_arg=$1
_am_stamp_count=1
for _am_header in $config_headers :; do
case $_am_header in
$_am_arg | $_am_arg:* )
break ;;
* )
_am_stamp_count=`expr $_am_stamp_count + 1` ;;
esac
done
echo "timestamp for $_am_arg" >`AS_DIRNAME(["$_am_arg"])`/stamp-h[]$_am_stamp_count])
# Copyright (C) 2001, 2003, 2005, 2008 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# AM_PROG_INSTALL_SH
# ------------------
# Define $install_sh.
AC_DEFUN([AM_PROG_INSTALL_SH],
[AC_REQUIRE([AM_AUX_DIR_EXPAND])dnl
if test x"${install_sh}" != xset; then
case $am_aux_dir in
*\ * | *\ *)
install_sh="\${SHELL} '$am_aux_dir/install-sh'" ;;
*)
install_sh="\${SHELL} $am_aux_dir/install-sh"
esac
fi
AC_SUBST(install_sh)])
# Copyright (C) 2003, 2005 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 2
# Check whether the underlying file-system supports filenames
# with a leading dot. For instance MS-DOS doesn't.
AC_DEFUN([AM_SET_LEADING_DOT],
[rm -rf .tst 2>/dev/null
mkdir .tst 2>/dev/null
if test -d .tst; then
am__leading_dot=.
else
am__leading_dot=_
fi
rmdir .tst 2>/dev/null
AC_SUBST([am__leading_dot])])
# Add --enable-maintainer-mode option to configure. -*- Autoconf -*-
# From Jim Meyering
# Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2008
# Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 5
# AM_MAINTAINER_MODE([DEFAULT-MODE])
# ----------------------------------
# Control maintainer-specific portions of Makefiles.
# Default is to disable them, unless `enable' is passed literally.
# For symmetry, `disable' may be passed as well. Anyway, the user
# can override the default with the --enable/--disable switch.
AC_DEFUN([AM_MAINTAINER_MODE],
[m4_case(m4_default([$1], [disable]),
[enable], [m4_define([am_maintainer_other], [disable])],
[disable], [m4_define([am_maintainer_other], [enable])],
[m4_define([am_maintainer_other], [enable])
m4_warn([syntax], [unexpected argument to AM@&t@_MAINTAINER_MODE: $1])])
AC_MSG_CHECKING([whether to am_maintainer_other maintainer-specific portions of Makefiles])
dnl maintainer-mode's default is 'disable' unless 'enable' is passed
AC_ARG_ENABLE([maintainer-mode],
[ --][am_maintainer_other][-maintainer-mode am_maintainer_other make rules and dependencies not useful
(and sometimes confusing) to the casual installer],
[USE_MAINTAINER_MODE=$enableval],
[USE_MAINTAINER_MODE=]m4_if(am_maintainer_other, [enable], [no], [yes]))
AC_MSG_RESULT([$USE_MAINTAINER_MODE])
AM_CONDITIONAL([MAINTAINER_MODE], [test $USE_MAINTAINER_MODE = yes])
MAINT=$MAINTAINER_MODE_TRUE
AC_SUBST([MAINT])dnl
]
)
AU_DEFUN([jm_MAINTAINER_MODE], [AM_MAINTAINER_MODE])
# Check to see how 'make' treats includes. -*- Autoconf -*-
# Copyright (C) 2001, 2002, 2003, 2005, 2009 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 4
# AM_MAKE_INCLUDE()
# -----------------
# Check to see how make treats includes.
AC_DEFUN([AM_MAKE_INCLUDE],
[am_make=${MAKE-make}
cat > confinc << 'END'
am__doit:
@echo this is the am__doit target
.PHONY: am__doit
END
# If we don't find an include directive, just comment out the code.
AC_MSG_CHECKING([for style of include used by $am_make])
am__include="#"
am__quote=
_am_result=none
# First try GNU make style include.
echo "include confinc" > confmf
# Ignore all kinds of additional output from `make'.
case `$am_make -s -f confmf 2> /dev/null` in #(
*the\ am__doit\ target*)
am__include=include
am__quote=
_am_result=GNU
;;
esac
# Now try BSD make style include.
if test "$am__include" = "#"; then
echo '.include "confinc"' > confmf
case `$am_make -s -f confmf 2> /dev/null` in #(
*the\ am__doit\ target*)
am__include=.include
am__quote="\""
_am_result=BSD
;;
esac
fi
AC_SUBST([am__include])
AC_SUBST([am__quote])
AC_MSG_RESULT([$_am_result])
rm -f confinc confmf
])
# Fake the existence of programs that GNU maintainers use. -*- Autoconf -*-
# Copyright (C) 1997, 1999, 2000, 2001, 2003, 2004, 2005, 2008
# Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 6
# AM_MISSING_PROG(NAME, PROGRAM)
# ------------------------------
AC_DEFUN([AM_MISSING_PROG],
[AC_REQUIRE([AM_MISSING_HAS_RUN])
$1=${$1-"${am_missing_run}$2"}
AC_SUBST($1)])
# AM_MISSING_HAS_RUN
# ------------------
# Define MISSING if not defined so far and test if it supports --run.
# If it does, set am_missing_run to use it, otherwise, to nothing.
AC_DEFUN([AM_MISSING_HAS_RUN],
[AC_REQUIRE([AM_AUX_DIR_EXPAND])dnl
AC_REQUIRE_AUX_FILE([missing])dnl
if test x"${MISSING+set}" != xset; then
case $am_aux_dir in
*\ * | *\ *)
MISSING="\${SHELL} \"$am_aux_dir/missing\"" ;;
*)
MISSING="\${SHELL} $am_aux_dir/missing" ;;
esac
fi
# Use eval to expand $SHELL
if eval "$MISSING --run true"; then
am_missing_run="$MISSING --run "
else
am_missing_run=
AC_MSG_WARN([`missing' script is too old or missing])
fi
])
# Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# AM_PROG_MKDIR_P
# ---------------
# Check for `mkdir -p'.
AC_DEFUN([AM_PROG_MKDIR_P],
[AC_PREREQ([2.60])dnl
AC_REQUIRE([AC_PROG_MKDIR_P])dnl
dnl Automake 1.8 to 1.9.6 used to define mkdir_p. We now use MKDIR_P,
dnl while keeping a definition of mkdir_p for backward compatibility.
dnl @MKDIR_P@ is magic: AC_OUTPUT adjusts its value for each Makefile.
dnl However we cannot define mkdir_p as $(MKDIR_P) for the sake of
dnl Makefile.ins that do not define MKDIR_P, so we do our own
dnl adjustment using top_builddir (which is defined more often than
dnl MKDIR_P).
AC_SUBST([mkdir_p], ["$MKDIR_P"])dnl
case $mkdir_p in
[[\\/$]]* | ?:[[\\/]]*) ;;
*/*) mkdir_p="\$(top_builddir)/$mkdir_p" ;;
esac
])
# Helper functions for option handling. -*- Autoconf -*-
# Copyright (C) 2001, 2002, 2003, 2005, 2008 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 4
# _AM_MANGLE_OPTION(NAME)
# -----------------------
AC_DEFUN([_AM_MANGLE_OPTION],
[[_AM_OPTION_]m4_bpatsubst($1, [[^a-zA-Z0-9_]], [_])])
# _AM_SET_OPTION(NAME)
# ------------------------------
# Set option NAME. Presently that only means defining a flag for this option.
AC_DEFUN([_AM_SET_OPTION],
[m4_define(_AM_MANGLE_OPTION([$1]), 1)])
# _AM_SET_OPTIONS(OPTIONS)
# ----------------------------------
# OPTIONS is a space-separated list of Automake options.
AC_DEFUN([_AM_SET_OPTIONS],
[m4_foreach_w([_AM_Option], [$1], [_AM_SET_OPTION(_AM_Option)])])
# _AM_IF_OPTION(OPTION, IF-SET, [IF-NOT-SET])
# -------------------------------------------
# Execute IF-SET if OPTION is set, IF-NOT-SET otherwise.
AC_DEFUN([_AM_IF_OPTION],
[m4_ifset(_AM_MANGLE_OPTION([$1]), [$2], [$3])])
# Check to make sure that the build environment is sane. -*- Autoconf -*-
# Copyright (C) 1996, 1997, 2000, 2001, 2003, 2005, 2008
# Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 5
# AM_SANITY_CHECK
# ---------------
AC_DEFUN([AM_SANITY_CHECK],
[AC_MSG_CHECKING([whether build environment is sane])
# Just in case
sleep 1
echo timestamp > conftest.file
# Reject unsafe characters in $srcdir or the absolute working directory
# name. Accept space and tab only in the latter.
am_lf='
'
case `pwd` in
*[[\\\"\#\$\&\'\`$am_lf]]*)
AC_MSG_ERROR([unsafe absolute working directory name]);;
esac
case $srcdir in
*[[\\\"\#\$\&\'\`$am_lf\ \ ]]*)
AC_MSG_ERROR([unsafe srcdir value: `$srcdir']);;
esac
# Do `set' in a subshell so we don't clobber the current shell's
# arguments. Must try -L first in case configure is actually a
# symlink; some systems play weird games with the mod time of symlinks
# (eg FreeBSD returns the mod time of the symlink's containing
# directory).
if (
set X `ls -Lt "$srcdir/configure" conftest.file 2> /dev/null`
if test "$[*]" = "X"; then
# -L didn't work.
set X `ls -t "$srcdir/configure" conftest.file`
fi
rm -f conftest.file
if test "$[*]" != "X $srcdir/configure conftest.file" \
&& test "$[*]" != "X conftest.file $srcdir/configure"; then
# If neither matched, then we have a broken ls. This can happen
# if, for instance, CONFIG_SHELL is bash and it inherits a
# broken ls alias from the environment. This has actually
# happened. Such a system could not be considered "sane".
AC_MSG_ERROR([ls -t appears to fail. Make sure there is not a broken
alias in your environment])
fi
test "$[2]" = conftest.file
)
then
# Ok.
:
else
AC_MSG_ERROR([newly created file is older than distributed files!
Check your system clock])
fi
AC_MSG_RESULT(yes)])
# Copyright (C) 2009 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 1
# AM_SILENT_RULES([DEFAULT])
# --------------------------
# Enable less verbose build rules; with the default set to DEFAULT
# (`yes' being less verbose, `no' or empty being verbose).
AC_DEFUN([AM_SILENT_RULES],
[AC_ARG_ENABLE([silent-rules],
[ --enable-silent-rules less verbose build output (undo: `make V=1')
--disable-silent-rules verbose build output (undo: `make V=0')])
case $enable_silent_rules in
yes) AM_DEFAULT_VERBOSITY=0;;
no) AM_DEFAULT_VERBOSITY=1;;
*) AM_DEFAULT_VERBOSITY=m4_if([$1], [yes], [0], [1]);;
esac
AC_SUBST([AM_DEFAULT_VERBOSITY])dnl
AM_BACKSLASH='\'
AC_SUBST([AM_BACKSLASH])dnl
_AM_SUBST_NOTMAKE([AM_BACKSLASH])dnl
])
# Copyright (C) 2001, 2003, 2005 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# AM_PROG_INSTALL_STRIP
# ---------------------
# One issue with vendor `install' (even GNU) is that you can't
# specify the program used to strip binaries. This is especially
# annoying in cross-compiling environments, where the build's strip
# is unlikely to handle the host's binaries.
# Fortunately install-sh will honor a STRIPPROG variable, so we
# always use install-sh in `make install-strip', and initialize
# STRIPPROG with the value of the STRIP variable (set by the user).
AC_DEFUN([AM_PROG_INSTALL_STRIP],
[AC_REQUIRE([AM_PROG_INSTALL_SH])dnl
# Installed binaries are usually stripped using `strip' when the user
# run `make install-strip'. However `strip' might not be the right
# tool to use in cross-compilation environments, therefore Automake
# will honor the `STRIP' environment variable to overrule this program.
dnl Don't test for $cross_compiling = yes, because it might be `maybe'.
if test "$cross_compiling" != no; then
AC_CHECK_TOOL([STRIP], [strip], :)
fi
INSTALL_STRIP_PROGRAM="\$(install_sh) -c -s"
AC_SUBST([INSTALL_STRIP_PROGRAM])])
# Copyright (C) 2006, 2008 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 2
# _AM_SUBST_NOTMAKE(VARIABLE)
# ---------------------------
# Prevent Automake from outputting VARIABLE = @VARIABLE@ in Makefile.in.
# This macro is traced by Automake.
AC_DEFUN([_AM_SUBST_NOTMAKE])
# AM_SUBST_NOTMAKE(VARIABLE)
# ---------------------------
# Public sister of _AM_SUBST_NOTMAKE.
AC_DEFUN([AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE($@)])
# Check how to create a tarball. -*- Autoconf -*-
# Copyright (C) 2004, 2005 Free Software Foundation, Inc.
#
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# serial 2
# _AM_PROG_TAR(FORMAT)
# --------------------
# Check how to create a tarball in format FORMAT.
# FORMAT should be one of `v7', `ustar', or `pax'.
#
# Substitute a variable $(am__tar) that is a command
# writing to stdout a FORMAT-tarball containing the directory
# $tardir.
# tardir=directory && $(am__tar) > result.tar
#
# Substitute a variable $(am__untar) that extract such
# a tarball read from stdin.
# $(am__untar) < result.tar
AC_DEFUN([_AM_PROG_TAR],
[# Always define AMTAR for backward compatibility.
AM_MISSING_PROG([AMTAR], [tar])
m4_if([$1], [v7],
[am__tar='${AMTAR} chof - "$$tardir"'; am__untar='${AMTAR} xf -'],
[m4_case([$1], [ustar],, [pax],,
[m4_fatal([Unknown tar format])])
AC_MSG_CHECKING([how to create a $1 tar archive])
# Loop over all known methods to create a tar archive until one works.
_am_tools='gnutar m4_if([$1], [ustar], [plaintar]) pax cpio none'
_am_tools=${am_cv_prog_tar_$1-$_am_tools}
# Do not fold the above two line into one, because Tru64 sh and
# Solaris sh will not grok spaces in the rhs of `-'.
for _am_tool in $_am_tools
do
case $_am_tool in
gnutar)
for _am_tar in tar gnutar gtar;
do
AM_RUN_LOG([$_am_tar --version]) && break
done
am__tar="$_am_tar --format=m4_if([$1], [pax], [posix], [$1]) -chf - "'"$$tardir"'
am__tar_="$_am_tar --format=m4_if([$1], [pax], [posix], [$1]) -chf - "'"$tardir"'
am__untar="$_am_tar -xf -"
;;
plaintar)
# Must skip GNU tar: if it does not support --format= it doesn't create
# ustar tarball either.
(tar --version) >/dev/null 2>&1 && continue
am__tar='tar chf - "$$tardir"'
am__tar_='tar chf - "$tardir"'
am__untar='tar xf -'
;;
pax)
am__tar='pax -L -x $1 -w "$$tardir"'
am__tar_='pax -L -x $1 -w "$tardir"'
am__untar='pax -r'
;;
cpio)
am__tar='find "$$tardir" -print | cpio -o -H $1 -L'
am__tar_='find "$tardir" -print | cpio -o -H $1 -L'
am__untar='cpio -i -H $1 -d'
;;
none)
am__tar=false
am__tar_=false
am__untar=false
;;
esac
# If the value was cached, stop now. We just wanted to have am__tar
# and am__untar set.
test -n "${am_cv_prog_tar_$1}" && break
# tar/untar a dummy directory, and stop if the command works
rm -rf conftest.dir
mkdir conftest.dir
echo GrepMe > conftest.dir/file
AM_RUN_LOG([tardir=conftest.dir && eval $am__tar_ >conftest.tar])
rm -rf conftest.dir
if test -s conftest.tar; then
AM_RUN_LOG([$am__untar /dev/null 2>&1 && break
fi
done
rm -rf conftest.dir
AC_CACHE_VAL([am_cv_prog_tar_$1], [am_cv_prog_tar_$1=$_am_tool])
AC_MSG_RESULT([$am_cv_prog_tar_$1])])
AC_SUBST([am__tar])
AC_SUBST([am__untar])
]) # _AM_PROG_TAR
weborf-0.13/cachedir.c 0000644 0000765 0000765 00000016541 11537207462 011600 0000000 0000000 /*
Weborf
Copyright (C) 2010 Salvo "LtWorf" Tomaselli
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
@author Salvo "LtWorf" Tomaselli
*/
#include "options.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "cachedir.h"
#include "utils.h"
#include "types.h"
#include "instance.h"
char *cachedir=NULL;
/**
Generates the filename for the cached entity and stores it in the buffer
*/
static inline void cached_filename(unsigned int uprefix,connection_t *connection_prop, char *buffer) {
snprintf(buffer,PATH_LEN,"%s/%u-%llu-%llu-%ld",cachedir,uprefix,(unsigned long long int)connection_prop->strfile_stat.st_ino,(unsigned long long int)connection_prop->strfile_stat.st_dev,connection_prop->strfile_stat.st_mtime);
}
/**
Returns true if the caching is enabled and
false otherwise
*/
bool cache_is_enabled() {
if (cachedir==NULL)
return false;
return true;
}
/**
Sends a cached item if present and returns true.
Returns false on cache miss.
*/
bool cache_send_item(unsigned int uprefix,connection_t* connection_prop) {//Try to send the cached file instead
int cachedfd=cache_get_item_fd(uprefix,connection_prop);
if (cachedfd==-1)
return false;
int oldfd=connection_prop->strfile_fd;
connection_prop->strfile_fd=cachedfd;
/*
replaces the stat of the directory with the stat of the cached file
it is safe here since get_cached_item has already been executed
*/
fstat(connection_prop->strfile_fd, &connection_prop->strfile_stat);
write_file(connection_prop);
//Restore file descriptor so it can be closed later
connection_prop->strfile_fd=oldfd;
//Closes the cache file descriptor
close(cachedfd);
return true;
}
/**
This function returns the file descriptor to the file containing the cached
item.
If a cache miss occurs -1 will be returned
If cache is not in use (cache_init was not called) it will always return -1
uprefix is an integer that must be unique for each call of get_cached_dir.
It's purpose is to distinguish between calls that will eventually generate an
HTML file and calls that will generate XML or other data.
So the directory would be the same but the generated content is different.
Acquires a shared lock on the file, so if the file is already opened in write mode
the lock will fail and the function will return the same result of a cache miss.
*/
int cache_get_item_fd(unsigned int uprefix,connection_t* connection_prop) {
if (!cachedir) return -1;
char fname[PATH_LEN];
//Get the filename
cached_filename(uprefix,connection_prop,fname);
int fd=open(fname,O_RDONLY);
if (fd==-1) return -1; //Cache miss
//Acquire lock on the file and return the file descriptor
if (flock(fd,LOCK_SH|LOCK_NB)==0)
return fd;
//Lock could not be acquired
close(fd);
return -1;
}
/**
Same as cache_get_item_fd but here the file is created and opened for
reading and writing, and will be created if it doesn't exist
An exclusive lock will be acquired over the file, since it is being opened
for writing too.
*/
int cache_get_item_fd_wr(unsigned int uprefix,connection_t *connection_prop) {
if (!cachedir) return -1;
char fname[PATH_LEN];
//Get the filename
cached_filename(uprefix,connection_prop,fname);
int fd = open(fname,O_RDWR| O_CREAT,S_IRUSR|S_IWUSR);
//Acquire the exclusive lock in a non-blocking way
if (flock(fd,LOCK_EX|LOCK_NB)==0) {
return fd;
}
close(fd);
return -1;
}
/**
Stores the content of the buffer "content" in cache, for the size specified by content_len
The cache file will have an exclusive lock to prevent multiple threads accessing the same file
in write mode.
Lock is acquired in a non-blocking way, so if the file is already locked the method will just return
without writing anything on the file. This behavior is wanted because it is assumed that the lock is held
by another thread writing the same content on the file, waiting for the lock to be released would lead to
override the content of the file with the same content.
*/
void cache_store_item(unsigned int uprefix,connection_t* connection_prop, char *content, size_t content_len) {
if (!cachedir) return;
char fname[PATH_LEN];
cached_filename(uprefix,connection_prop,fname);
int fd=open(fname,O_WRONLY| O_CREAT,S_IRUSR|S_IWUSR);
if (fd==-1) return; //Just do nothing in case of error
//Try to acquire lock in a non-blocking way, and exit if another thread has that lock
if (flock(fd,LOCK_EX|LOCK_NB)!=0) {
close(fd);
return;
}
write(fd,content,content_len);
close(fd);
return;
}
/**
This function initializes a cache directory.
If the dir is not a directory or it is impossible to stat
weborf will terminate.
If it is impossible to delete and create files in it, weborf
will just log a warning.
*/
void cache_init(char* dir) {
cachedir=dir;
{
//Check if it exists
struct stat stat_buf;
if (stat(dir, &stat_buf)!=0) {
write(2,"Unable to stat cache directory\n",31);
#ifdef SERVERDBG
syslog(LOG_ERR,"Unable to stat cache directory");
#endif
exit(10);
}
//Check it is a directory
if (!S_ISDIR(stat_buf.st_mode)) {
write(2,"--cache parameter must be a directory\n",38);
#ifdef SERVERDBG
syslog(LOG_ERR,"--cache parameter must be a directory");
#endif
exit(10);
}
}
//check we have permissions
if (access(dir,W_OK|R_OK)!=0) {
write(2,"no read or write permissions on cache dir\n",42);
#ifdef SERVERDBG
syslog(LOG_ERR,"no read or write permissions on cache dir");
#endif
exit(10);
}
}
/**
Removes all the files contained in the cache directory.
The cache must have been already initialized for this to work
Returns 0 on success, -1 otherwise
*/
int cache_clear() {
if (!cachedir) return -1;
//Empty directory
DIR *dp = opendir(cachedir); //Open dir
struct dirent entry;
struct dirent *result;
int return_code;
int retval=0;
if (dp == NULL) {
return 1;
}
char*file=malloc(PATH_LEN);//Buffer for path
if (file==NULL)
return -1;
//Cycles trough dir's elements
for (return_code=readdir_r(dp,&entry,&result); result!=NULL && return_code==0; return_code=readdir_r(dp,&entry,&result)) { //Cycles trough dir's elements
//skips dir . and .. but not all hidden files
if (entry.d_name[0]=='.' && (entry.d_name[1]==0 || (entry.d_name[1]=='.' && entry.d_name[2]==0)))
continue;
snprintf(file,PATH_LEN,"%s/%s",cachedir, entry.d_name);
if (unlink(file)!=0) retval=-1;
}
closedir(dp);
free(file);
return retval;
}
weborf-0.13/cachedir.h 0000644 0000765 0000765 00000002275 11536415542 011603 0000000 0000000 /*
Weborf
Copyright (C) 2010 Salvo "LtWorf" Tomaselli
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
@author Salvo "LtWorf" Tomaselli
*/
#ifndef WEBORF_CACHEDIR_H
#define WEBORF_CACHEDIR_H
#include "types.h"
bool cache_send_item(unsigned int uprefix,connection_t* connection_prop);
int cache_get_item_fd(unsigned int uprefix,connection_t* connection_prop);
int cache_get_item_fd_wr(unsigned int uprefix,connection_t *connection_prop);
void cache_store_item(unsigned int uprefix,connection_t* connection_prop, char *content, size_t content_len);
void cache_init(char *dir);
int cache_clear();
bool cache_is_enabled();
#endif weborf-0.13/queue.c 0000644 0000765 0000765 00000005757 11537207462 011171 0000000 0000000 /*
Weborf
Copyright (C) 2007 Giuseppe Pappalardo
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
@author Giuseppe Pappalardo
@author Salvo "LtWorf" Tomaselli
@author Salvo Rinaldi
*/
#include "options.h"
#include
#include
#include
#include "queue.h"
/**
Inits the syncronized queue, allocating memory.
Requires the syn_queue_t struct and the size of the queue itself.
To deallocate the queue, use the q_free function.
*/
int q_init(syn_queue_t * q, int size) {
q->num = q->head = q->tail = 0;
q->size = size;
q->data = (int *) malloc(sizeof(int) * size);
if (q->data == NULL) { //Error, unable to allocate memory
return 1;
}
pthread_mutex_init(&q->mutex, NULL);
pthread_cond_init(&q->for_space, NULL);
pthread_cond_init(&q->for_data, NULL);
q->n_wait_dt = q->n_wait_sp = 0;
return 0;
}
/** Frees the memory taken by the queue.
Requires the pointer to the queue struct
*/
void q_free(syn_queue_t * q) {
free(q->data);
}
int q_get(syn_queue_t * q, int *val) {
pthread_mutex_lock(&q->mutex);
while (q->num == 0) {
q->n_wait_dt++;
pthread_cond_wait(&q->for_data, &q->mutex);
}
*val = q->data[q->head]; //Sets the value
q->head = (q->head + 1) % q->size; //Moves the head
q->num--; //Reduces count of the queue
/*if ((q->num == q->size) && (q->n_wait_sp > 0)) {
q->n_wait_sp--;
pthread_cond_signal(&q->for_space);
} // unlock also needed after signal*/
pthread_mutex_unlock(&q->mutex); // or threads blocked on wait
return 0; // will not proceed
}
int q_put(syn_queue_t * q, int val) {
pthread_mutex_lock(&q->mutex);
//Wakes up a sleeping thread
if (q->n_wait_dt > 0) {
q->n_wait_dt--;
pthread_cond_signal(&q->for_data);
} // unlock also needed after signal
//Fails if queue is full
if (q->num == q->size) {
pthread_mutex_unlock(&q->mutex); // or threads blocked on wait
return 1; // will not proceed
//while (q->num == q->size) {
//q->n_wait_sp++;
//pthread_cond_wait(&q->for_space, &q->mutex);
}
q->data[q->tail] = val; //Set the data in position
q->tail = (q->tail + 1) % q->size; //Moves the tail
q->num++; //Increases count of filled positions
pthread_mutex_unlock(&q->mutex); // or threads blocked on wait
return 0; // will not proceed
}
weborf-0.13/queue.h 0000644 0000765 0000765 00000001741 11536415542 011162 0000000 0000000 /*
Weborf
Copyright (C) 2007 Giuseppe Pappalardo
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
@author Giuseppe Pappalardo
@author Salvo "LtWorf" Tomaselli
*/
#ifndef WEBORF_QUEUE_H
#define WEBORF_QUEUE_H
#include "types.h"
int q_init(syn_queue_t * q, int size);
int q_put(syn_queue_t * q, int val);
int q_get(syn_queue_t * q, int *val);
void q_free(syn_queue_t * q);
#endif
weborf-0.13/TODOlist 0000644 0000765 0000765 00000000320 11536415542 011241 0000000 0000000 - Allow external script for modurl, must have the headers...
- use openSSL (instead of using read and write, use wrappers so openssl or direct accesso to socket can be used)
- Create a Doxyfile to use doxygen weborf-0.13/README 0000644 0000765 0000765 00000001141 11536415542 010537 0000000 0000000 - CONFIGURATION
Base directory, port and interface to listen to can be set using command line
To see command line options run weborf -h
Further configuration can be done by modifying options.h.in and configuring and compiling again.
Explaination of paramethers can be found within the file itself.
You might want to do (if you downloaded from svn, you don't really have a choice):
autoreconf -f -i
- COMPILING
make clean
./configure
make
- RUN
./weborf
- INSTALL (Not mandatory)
make install (as root)
then you will have weborf's manual
man weborf
- UNINSTALL (deprecated ;-))
make uninstall (as root)
weborf-0.13/qweborf/ 0000755 0000765 0000765 00000000000 11543375266 011415 5 0000000 0000000 weborf-0.13/qweborf/whelper.py 0000644 0000765 0000765 00000024216 11542642140 013345 0000000 0000000 # -*- coding: utf-8 -*-
# Weborf
# Copyright (C) 2010 Salvo "LtWorf" Tomaselli
#
# Weborf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# author Salvo "LtWorf" Tomaselli
import os
import subprocess
import socket
import threading
import nhelper
from PyQt4 import QtCore
class weborf_runner():
def __init__(self,logfunction):
self.logclass=logfunction
self.logclass.logger("Software initialized")
self.child=None
self.socket=None
self.listener=None
self.waiter=None
self.methods=[]
self.username=None
self.password=None
self.ipv6=True
self._running=False
self.version=None
self.weborf=self._test_weborf()
pass
def _test_weborf(self):
'''Tests if weborf binary is existing.
It will return true if everything is OK
and false otherwise.'''
ret=0
out=""
try:
p = subprocess.Popen(["weborf", "-v"], bufsize=1024, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out=p.stdout.read().split("\n")[0]
p.stdin.close()
ret=p.wait()
except:
ret=3
if ret==0:
self.logclass.logger(out,self.logclass.DBG_NOTICE)
self.version=out.split(' ')[1]
else:
self.logclass.logger('Unable to find weborf',self.logclass.DBG_ERROR)
return False
#Determining if has ipv6 support
try:
p = subprocess.Popen(["weborf", "-h"], bufsize=1024, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
out=p.stdout.read()
p.stdin.close()
ret=p.wait()
except:
out=''
pass
if 'Compiled for IPv6' in out:
self.ipv6=True
self.logclass.logger('Server has IPv6 support',self.logclass.DBG_NOTICE)
else:
self.ipv6=False
self.logclass.logger('Server lacks IPv6 support',self.logclass.DBG_WARNING)
return True
def start(self,options):
'''Starts weborf,
returns True if it is correctly started'''
if not self.weborf:
self.logclass.logger('Unable to find weborf',self.logclass.DBG_ERROR)
return False
if len(options['path'])==0:
self.logclass.logger('Path not specified',self.logclass.DBG_ERROR)
return False
self.logclass.logger("Starting weborf...")
auth_socket=self.__create_auth_socket()
self.__start_weborf(options,auth_socket)
self.__listen_auth_socket(options)
self.username=options['username']
self.password=options['password']
#Deciding which HTTP methods will be enabled
self.methods=['GET']
if options['dav']:
self.methods.append('OPTIONS')
self.methods.append('PROPFIND')
self.logclass.logger("DAV access enabled",self.logclass.DBG_NOTICE)
#If writing is enabled
if options['write']:
self.methods.append('PUT')
self.methods.append('DELETE')
self.methods.append('COPY')
self.methods.append('MOVE')
self.methods.append('MKCOL')
self.logclass.logger('Writing access enabled. This could pose security threat',self.logclass.DBG_WARNING)
return True
def __create_auth_socket(self):
'''Creates a unix socket and returns the path to it'''
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sockname="/tmp/weborf_auth_socket%d.socket" % os.getuid()
try:
os.remove(sockname)
except OSError:
pass
self.socket.bind(sockname)
return sockname
def __listen_auth_socket(self,options):
self.listener=__listener__(self.socket)
QtCore.QObject.connect(self.listener, QtCore.SIGNAL("new_socket(PyQt_PyObject)"), self.socket_cback)
self.listener.start()
@QtCore.pyqtSignature("PyQt_PyObject")
def socket_cback(self,sock):
'''Recieves connection requests and decides if they have to be authorized or denied'''
data = sock.recv(4096).split('\r\n')
uri = data[0]
client = data[1]
method = data[2]
username = data[3]
password = data[4]
### Checking if the request must be allowed or denied
allow=True
if self.username!=None: #Must check username and password
allow= self.username==username and self.password==password
if method not in self.methods:
allow=False
if allow:
self.logclass.logger("%s - %s %s" % (client,method, uri))
else:
sock.send(' ')
self.logclass.logger("DENIED: %s - %s %s" % (client,method, uri))
sock.close()
pass
def __start_weborf(self,options,auth_socket):
'''Starts a weborf in a subprocess'''
cmdline=["weborf", "-p",str(options['port']),"-b",str(options['path']),"-x","-I","....","-a",auth_socket]
if options['tar']:
cmdline.append('--tar')
if options['ip']!=None:
cmdline.append('-i')
cmdline.append(options['ip'])
self.logclass.logger(' '.join(cmdline))
self.child = subprocess.Popen(
cmdline
, bufsize=1024, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
self.loglinks(options)
self.waiter=__waiter__(self.child) #Starts thread to wait for weborf termination
QtCore.QObject.connect(self.waiter, QtCore.SIGNAL("child_terminated(PyQt_PyObject,PyQt_PyObject)"), self._child_terminated)
self.waiter.start()
self._running=True
return True
def loglinks(self,options):
'''Prints to the log all the links that weborf is listening to'''
if options['ip']==None:
addrs4=nhelper.getaddrs(False)
if self.ipv6:
addrs6=nhelper.getaddrs(True)
else:
addrs6=tuple()
else:
if self.ipv6:
#address can be both ipv6 or mapped ipv4
if '.' in options['ip']:
addrs6=(options['ip'],)
addrs4=(options['ip'][7:],)
else: #Normal ipv6
addrs4=tuple()
addrs6=(options['ip'],)
else:
addrs6=tuple()
addrs4=(options['ip'],)
#Output of addresses binded
for i in addrs4:
url='http://%s:%d/' % (i,options['port'])
logentry='Address: %s' % (url,url)
self.logclass.logger(logentry)
for i in addrs6:
url='http://[%s]:%d/' % (i,options['port'])
logentry='Address: %s' % (url,url)
self.logclass.logger(logentry)
@QtCore.pyqtSignature("PyQt_PyObject,PyQt_PyObject")
def _child_terminated(self,child,exit_code):
'''Called when the child process is terminated
param child is for now ignored'''
if exit_code != 0:
self.logclass.logger('Weborf terminated with exit code %d'%exit_code,self.logclass.DBG_ERROR)
else:
self.logclass.logger("Termination complete",self.logclass.DBG_NOTICE)
self._running=False
pass
def stop(self):
'''Stop weborf and correlated processes.
Will return True if it goes well
It should be called only if start succeeded'''
if self._running:
self.logclass.logger("Sending terminate signal and waiting for termination...")
self.child.stdin.close()
self.child.terminate()
self.socket.close() #Closing socket, so the accept will fail and the thread can terminate
self.listener.stop()
return True
class __listener__(QtCore.QThread):
'''This class is used to listen to a socket.
It will accept connections and send those connection
using new_socket(PyQt_PyObject).
'''
def __init__(self,socket):
QtCore.QThread.__init__(self)
self.socket=socket
self.socket.settimeout(2.0)
self.cycle=True
def stop(self):
self.cycle=False
def run(self):
self.socket.listen(1)
while self.cycle:
try:
sock, addr = self.socket.accept()
self.emit(QtCore.SIGNAL("new_socket(PyQt_PyObject)"),sock)
except:
pass
pass
class __waiter__(QtCore.QThread):
'''This class creates a separate thread that will wait for the
termination of weborf, and performs a callback when it occurs.
A more normal way to do this would have been to handle SIGCHLD but
the use of QT libraries prevents the use of signals apparently.
connect to child_terminated(PyQt_PyObject,PyQt_PyObject) to handle the event'''
def __init__(self,child):
'''child: child process to wait
'''
QtCore.QThread.__init__(self)
self.child=child
def run(self):
#wait termination for the child process
t=self.child.wait()
#Sends callback
self.emit(QtCore.SIGNAL("child_terminated(PyQt_PyObject,PyQt_PyObject)"),self.child,t)
weborf-0.13/qweborf/qweborf 0000744 0000765 0000765 00000001517 11536415541 012723 0000000 0000000 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# Weborf
# Copyright (C) 2010 Salvo "LtWorf" Tomaselli
#
# Relational is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# author Salvo "LtWorf" Tomaselli
import qweborf.qweborf
qweborf.qweborf.q_main()
weborf-0.13/qweborf/main.ui 0000644 0000765 0000765 00000040660 11542622223 012611 0000000 0000000
Form
0
0
532
536
QWeborf
-
0
Basic
-
QFormLayout::ExpandingFieldsGrow
-
Directory to share
txtPath
-
-
Insert here the absolute path of the directory to share
-
⌫
-
80
16777215
QDialogButtonBox::Open
Intermediate
-
0
-
Connection
-
Port
spinPort
-
Select the port. It must be free and not blocked by a firewall.
65536
8080
-
Address
cmbAddress
-
Select the IP address to bind. Leave the default value if you don't know what this means.
-
Bind to all
-
Other
-
Send directories as tar.gz
-
Authentication
-
QFormLayout::ExpandingFieldsGrow
-
WebDav
chkDav
-
If checked, clients will be able to mount the shared directory as a DAV share.
Enable WebDav
-
Writing
chkWrite
-
Authentication
false
chkEnableAuth
-
Enable
-
Username
txtUsername
-
false
Insert here the desired username
-
Password
txtPassword
-
false
Insert here the password
QLineEdit::Password
-
false
If checked, clients will be able to create directories and write file on the server.
Allow clients to write files on the server
Advanced
-
Advanced users are supposed to use the terminal
-
0
-
true
true
-
0
-
⌫
-
About
-
-
Start sharing the directory
Start sharing
true
-
false
Stop sharing the directory
Stop sharing
-
Stop sharing and exit the program.
QDialogButtonBox::Close
-
txtPath
cmdClearPath
cmdOpen
spinPort
cmbAddress
chkTar
chkDav
chkWrite
chkEnableAuth
txtUsername
txtPassword
tabWidget
cmdClearLog
cmdAbout
cmdStart
cmdStop
cmdClose
txtLog
cmdStop
clicked()
Form
stop_sharing()
526
495
457
544
cmdStart
clicked()
Form
start_sharing()
125
495
122
545
cmdOpen
clicked(QAbstractButton*)
Form
select_path()
517
40
528
68
cmdClose
clicked(QAbstractButton*)
Form
terminate()
513
525
528
447
cmdAbout
clicked()
Form
about()
494
426
265
275
cmdClearPath
clicked()
txtPath
clear()
415
44
360
44
cmdClearLog
clicked()
txtLog
clear()
470
359
398
359
chkDav
clicked(bool)
chkWrite
setEnabled(bool)
238
181
152
193
chkEnableAuth
clicked(bool)
txtPassword
setEnabled(bool)
169
220
196
276
chkEnableAuth
clicked(bool)
txtUsername
setEnabled(bool)
139
214
128
239
auth_toggle(int)
stop_sharing()
start_sharing()
select_path()
dav_toggle(int)
terminate()
about()
weborf-0.13/qweborf/qweborf.desktop 0000744 0000765 0000765 00000000343 11536656120 014367 0000000 0000000 [Desktop Entry]
Name=qweborf
GenericName=qweborf
Comment=Shares files using the HTTP protocol
Comment[it]=Condivide file usando il protocollo HTTP
Exec=qweborf
Icon=network-wired
Terminal=0
Type=Application
Categories=Network;
weborf-0.13/qweborf/qweborf.py 0000744 0000765 0000765 00000012276 11543366546 013366 0000000 0000000 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# Weborf
# Copyright (C) 2010 Salvo "LtWorf" Tomaselli
#
# Weborf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# author Salvo "LtWorf" Tomaselli
from PyQt4 import QtCore, QtGui
import main
import whelper
import nhelper
import sys
import os
class qweborfForm (QtGui.QWidget):
'''This class is the form used for the survey, needed to intercept the events.
It also sends the data with http POST to a page hosted on galileo'''
DBG_DEFAULT=0
DBG_WARNING=1
DBG_ERROR=2
DBG_NOTICE=3
def setUi(self,ui):
self.ui=ui
self.weborf=whelper.weborf_runner(self)
self.started=False
if self.weborf.version>= '0.13':
self.ui.chkTar.setEnabled(True)
else:
self.ui.chkTar.setEnabled(False)
#Listing addresses
for i in nhelper.getaddrs(self.weborf.ipv6):
self.ui.cmbAddress.addItem(i,None)
self.defaultdir=str(QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.HomeLocation))
self.ui.txtPath.setText(self.defaultdir)
def logger(self,data,level=DBG_DEFAULT):
'''logs an entry, showing it in the GUI'''
if level==self.DBG_WARNING:
data='WARNING: %s' % data
elif level==self.DBG_ERROR:
data='ERROR: %s' % data
elif level==self.DBG_NOTICE:
data='NOTICE: %s' % data
self.ui.txtLog.moveCursor(QtGui.QTextCursor.End)
self.ui.txtLog.insertHtml(data+'
')
self.ui.txtLog.moveCursor(QtGui.QTextCursor.End)
def setDefaultValues(self):
'''Sets default values into the form GUI. It has to be
called after the form has been initialized'''
pass
def stop_sharing(self):
if self.weborf.stop():
self.ui.cmdStart.setEnabled(True)
self.ui.cmdStop.setEnabled(False)
self.ui.tabWidget.setEnabled(True)
self.started=False
def about(self):
self.logger('
Qweborf 0.13')
self.logger('This program comes with ABSOLUTELY NO WARRANTY.'
' This is free software, and you are welcome to redistribute it under certain conditions.'
' For details see the GPLv3 Licese.')
self.logger('Homepage')
self.logger('Salvo \'LtWorf\' Tomaselli <tiposchi@tiscali.it>')
def terminate(self):
if self.started:
self.stop_sharing()
sys.exit(0)
def start_sharing(self):
options={} #Dictionary with the chosen options
options['path']=self.ui.txtPath.text()
if self.ui.chkEnableAuth.isChecked():
options['username']=self.ui.txtUsername.text()
options['password']=self.ui.txtPassword.text()
else:
options['username']=None
options['password']=None
options['port'] = self.ui.spinPort.value()
options['dav'] = self.ui.chkDav.isChecked()
if self.ui.chkDav.isChecked():
options['write'] = self.ui.chkWrite.isChecked()
else:
options['write'] = False
options['tar'] = self.ui.chkTar.isChecked()
if self.ui.cmbAddress.currentIndex()==0:
options['ip']=None
else:
options['ip']=str(self.ui.cmbAddress.currentText())
if self.weborf.start(options):
self.ui.cmdStart.setEnabled(False)
self.ui.cmdStop.setEnabled(True)
self.ui.tabWidget.setEnabled(False)
self.started=True
def select_path(self):
#filename = QtGui.QFileDialog
#dial=QtGui.QFileDialog()
dirname= QtGui.QFileDialog.getExistingDirectory(
self,
QtGui.QApplication.translate("Form", "Directory to share", None, QtGui.QApplication.UnicodeUTF8),
self.defaultdir,
QtGui.QFileDialog.ShowDirsOnly
)
self.ui.txtPath.setText(dirname)
def q_main():
import sys
app = QtGui.QApplication(sys.argv)
Form = qweborfForm()
ui = main.Ui_Form()
ui.setupUi(Form)
Form.setUi(ui)
Form.show()
res=app.exec_()
Form.terminate()
sys.exit(res)
if __name__ == "__main__":
q_main()
weborf-0.13/qweborf/nhelper.py 0000644 0000765 0000765 00000015763 11542642154 013350 0000000 0000000 # -*- coding: utf-8 -*-
# Weborf
# Copyright (C) 2011 Salvo "LtWorf" Tomaselli
#
# Weborf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# original code http://carnivore.it/2010/07/22/python_-_getifaddrs
# edited Salvo "LtWorf" Tomaselli
from ctypes import *
from socket import AF_INET, AF_INET6, AF_PACKET, inet_ntop
from sys import platform
def getifaddrs():
# getifaddr structs
class ifa_ifu_u(Union):
_fields_ = [
( "ifu_broadaddr", c_void_p ),
( "ifu_dstaddr", c_void_p )
]
class ifaddrs(Structure):
_fields_ = [
( "ifa_next", c_void_p ),
( "ifa_name", c_char_p ),
( "ifa_flags", c_uint ),
( "ifa_addr", c_void_p ),
( "ifa_netmask", c_void_p ),
( "ifa_ifu", ifa_ifu_u ),
( "ifa_data", c_void_p )
]
# AF_UNKNOWN / generic
if platform.startswith( "darwin" ) or platform.startswith( "freebsd" ):
class sockaddr ( Structure ):
_fields_ = [
("sa_len", c_uint8 ),
("sa_family", c_uint8 ),
("sa_data", (c_uint8 * 14) ) ]
else:
class sockaddr(Structure):
_fields_ = [
( "sa_family", c_uint16 ),
( "sa_data", (c_uint8 * 14) )
]
# AF_INET / IPv4
class in_addr(Union):
_fields_ = [
("s_addr", c_uint32),
]
class sockaddr_in(Structure):
_fields_ = [
("sin_family", c_short),
("sin_port", c_ushort),
("sin_addr", in_addr),
("sin_zero", (c_char * 8) ), # padding
]
# AF_INET6 / IPv6
class in6_u(Union):
_fields_ = [
("u6_addr8", (c_uint8 * 16) ),
("u6_addr16", (c_uint16 * 8) ),
("u6_addr32", (c_uint32 * 4) )
]
class in6_addr(Union):
_fields_ = [
("in6_u", in6_u),
]
class sockaddr_in6(Structure):
_fields_ = [
("sin6_family", c_short),
("sin6_port", c_ushort),
("sin6_flowinfo", c_uint32),
("sin6_addr", in6_addr),
("sin6_scope_id", c_uint32),
]
# AF_PACKET / Linux
class sockaddr_ll( Structure ):
_fields_ = [
("sll_family", c_uint16 ),
("sll_protocol", c_uint16 ),
("sll_ifindex", c_uint32 ),
("sll_hatype", c_uint16 ),
("sll_pktype", c_uint8 ),
("sll_halen", c_uint8 ),
("sll_addr", (c_uint8 * 8) )
]
# AF_LINK / BSD|OSX
class sockaddr_dl( Structure ):
_fields_ = [
("sdl_len", c_uint8 ),
("sdl_family", c_uint8 ),
("sdl_index", c_uint16 ),
("sdl_type", c_uint8 ),
("sdl_nlen", c_uint8 ),
("sdl_alen", c_uint8 ),
("sdl_slen", c_uint8 ),
("sdl_data", (c_uint8 * 46) )
]
libc = CDLL("libc.so.6")
ptr = c_void_p(None)
result = libc.getifaddrs(pointer(ptr))
if result:
return None
ifa = ifaddrs.from_address(ptr.value)
result = {}
while True:
name = ifa.ifa_name
# name = ifa.ifa_name.decode('UTF-8') # use this for python3
if name not in result:
result[name] = {}
sa = sockaddr.from_address(ifa.ifa_addr)
if sa.sa_family not in result[name]:
result[name][sa.sa_family] = []
data = {}
if sa.sa_family == AF_INET:
if ifa.ifa_addr is not None:
si = sockaddr_in.from_address(ifa.ifa_addr)
data['addr'] = inet_ntop(si.sin_family,si.sin_addr)
if ifa.ifa_netmask is not None:
si = sockaddr_in.from_address(ifa.ifa_netmask)
data['netmask'] = inet_ntop(si.sin_family,si.sin_addr)
if sa.sa_family == AF_INET6:
if ifa.ifa_addr is not None:
si = sockaddr_in6.from_address(ifa.ifa_addr)
data['addr'] = inet_ntop(si.sin6_family,si.sin6_addr)
if data['addr'].startswith('fe80:'):
data['scope'] = si.sin6_scope_id
if ifa.ifa_netmask is not None:
si = sockaddr_in6.from_address(ifa.ifa_netmask)
data['netmask'] = inet_ntop(si.sin6_family,si.sin6_addr)
if sa.sa_family == AF_PACKET:
if ifa.ifa_addr is not None:
si = sockaddr_ll.from_address(ifa.ifa_addr)
addr = ""
for i in range(si.sll_halen):
addr += "%02x:" % si.sll_addr[i]
addr = addr[:-1]
data['addr'] = addr
if len(data) > 0:
result[name][sa.sa_family].append(data)
if ifa.ifa_next:
ifa = ifaddrs.from_address(ifa.ifa_next)
else:
break
libc.freeifaddrs(ptr)
return result
def getaddrs(ipv6=True):
'''Returns the list of the IP addresses it is possible to bind to
ipv6: if true, ONLY ipv6 addresses will be returned (ipv4 will be mapped to ipv6)
'''
ifaces=getifaddrs()
l_ipv4=[]
l_ipv6=[]
#Cycle the interfaces
for iface in ifaces:
#Cycle address family
for family in ifaces[iface]:
#print("\t%s" % { AF_INET: 'IPv4', AF_INET6 : 'IPv6', AF_PACKET: 'HW' }[family])
#Cycle addresses
for addr in ifaces[iface][family]:
if 'addr' in addr:
if family==AF_INET:
l_ipv4.append(str(addr['addr']))
elif family==AF_INET6:
l_ipv6.append(str(addr['addr']))
if ipv6==True:
#Transforming ipv4 into ipv6-mapped
for i in range(len(l_ipv4)):
l_ipv4[i] = '::ffff:%s' % l_ipv4[i]
return l_ipv4+l_ipv6
else:
return l_ipv4
def printifconfig():
ifaces=getifaddrs()
for iface in ifaces:
print(iface)
for family in ifaces[iface]:
print("\t%s" % { AF_INET: 'IPv4', AF_INET6 : 'IPv6', AF_PACKET: 'HW' }[family])
for addr in ifaces[iface][family]:
for i in ['addr','netmask','scope']:
if i in addr:
print("\t\t%s %s" % (i, str(addr[i])))
print("")
weborf-0.13/qweborf/qweborf.1 0000644 0000765 0000765 00000001224 11536415541 013054 0000000 0000000 .TH "qweborf" "1"
.SH "NAME"
qweborf \(em Shares files using the HTTP protocol.
.SH "SYNOPSIS"
.PP
\fBqweborf\fR
.SH "DESCRIPTION"
.PP
Qweborf provides an easy to use interface to share local files using the HTTP protocol.
It can enable writing on the server, webdav and authentication.
.SH "AUTHOR"
.PP
This manual page was written by Salvo 'LtWorf' Tomaselli for
the \fBDebian GNU/Linux\fP system (but may be used by others). Permission is
granted to copy, distribute and/or modify this document under
the terms of the GNU General Public License
version 3 or any later version published by the Free Software Foundation.
weborf-0.13/buffered_reader.c 0000644 0000765 0000765 00000011443 11537207462 013136 0000000 0000000 /*
Weborf
Copyright (C) 2009 Salvo "LtWorf" Tomaselli
Weborf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
@author Salvo "LtWorf" Tomaselli
*/
#include "options.h"
#include
#include
#include
#include
#include "buffered_reader.h"
#include "types.h"
/**
This funcion inits the struct allocating a buffer of the specified size.
It will return 0 on success and 1 on fail.
*/
int buffer_init(buffered_read_t * buf, ssize_t size) {
buf->buffer = malloc(sizeof(char) * size);
buf->size = size;
buffer_reset(buf);
return (buf->buffer == NULL) ? 1 : 0;
}
/**
Resets the pointers, so the buffer is ready for new reads on new
file descriptor
*/
void buffer_reset (buffered_read_t * buf) {
buf->end = buf->start = buf->buffer;
}
/**
This function will free the memory allocated by the buffer used in the struct.
*/
void buffer_free(buffered_read_t * buf) {
free(buf->buffer);
}
static ssize_t buffer_fill(int fd, buffered_read_t * buf) {
ssize_t r;
buf->start = buf->buffer;
//Timeout implementation
struct pollfd monitor[1];
monitor[0].fd = fd; //File descriptor to monitor
monitor[0].events = POLLIN; //Monitor on input events
//Waits the timeout or reads the data.
//If timeout is reached and no input is available
//will behave like the stream is closed.
if (poll(monitor, 1, READ_TIMEOUT) == 0) {
r = 0;
} else {
r = read(fd, buf->buffer, buf->size);
}
if (r <= 0) { //End of the stream
buf->end = buf->start;
} else {
buf->end = buf->start + r;
}
return r;
}
/**
This function is designed to be similar to a normal read, but it uses an internal
buffer.
When the buffer is empty, it will try to fill it.
An important difference from the normal read is that this function will wait until
the requested amount of bytes are available, or until the timeout occurs.
Timeout duration is defined with the READ_TIMEOUT define.
On some special cases, the read data could be less than the requested one. For example if
end of file is reached and it is impossible to do further reads.
*/
ssize_t buffer_read(int fd, void *b, ssize_t count, buffered_read_t * buf) {
ssize_t wrote = 0; //Count of written bytes
ssize_t available, needed; //Available bytes in buffer, and requested bytes remaining
while (wrote < count) {
available = buf->end - buf->start;
needed = count - wrote;
if (needed <= available) { //More data in buffer than needed
memcpy(b, buf->start, needed);
buf->start += needed;
return wrote + needed;
} else { //Requesting more data than available
if (available > 0) {//Empty the remaining data in the buffer
memcpy(b, buf->start, available);
b += available;
wrote += available;
}
//Filing the buffer again
if (buffer_fill(fd,buf) <= 0) { //End of the stream
return wrote;
}
}
} /*while */
return wrote;
}
/**
* This function returns how many bytes must be read in order to
* read enough data for it to end with the string needle.
* strlen(needle) must be added to the returned value to include the
* needle string in the result of the read.
*
* If the string needle is not in the buffer then the entire size
* of the data present in cache will be return.
* */
size_t buffer_strstr(int fd, buffered_read_t * buf, char * needle) {
if (buf->end - buf->start==0) {
buffer_fill(fd,buf);
}
buf->end[0]=0;
char *r=strstr(buf->start,needle);
if (r==NULL) {
return buf->end - buf->start;
} else {
return r-buf->start;
}
}
/*#include
#include
#include
#include
int main () {
buffered_read_t buf;
buffer_init(&buf, 30);
int fp=open("/home/salvo/.bash_history",O_RDONLY);
char * k=malloc (600);
int end=0;
while (1) {
int size=buffer_strstr(fp,&buf,"\n")+1;
end=buffer_read(fp,k,size,&buf);
if (end