//.........这里部分代码省略.........
case 'r':
resource_path = optarg;
printf("Setting resource path to \"%s\"\n", resource_path);
break;
case 'h':
fprintf(stderr, "Usage: test-server "
"[--port=<p>] [--ssl] "
"[-d <log bitfield>] "
"[--resource_path <path>]\n");
exit(1);
}
}
#if !defined(LWS_NO_DAEMONIZE) && !defined(WIN32)
/*
* normally lock path would be /var/lock/lwsts or similar, to
* simplify getting started without having to take care about
* permissions or running as root, set to /tmp/.lwsts-lock
*/
if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
fprintf(stderr, "Failed to daemonize\n");
return 1;
}
#endif
for (n = 0; n < ARRAY_SIZE(sigs); n++) {
_ev_init(&signals[n], signal_cb);
ev_signal_set(&signals[n], sigs[n]);
ev_signal_start(loop, &signals[n]);
}
#ifndef _WIN32
/* we will only try to log things according to our debug_level */
setlogmask(LOG_UPTO (LOG_DEBUG));
openlog("lwsts", syslog_options, LOG_DAEMON);
#endif
/* tell the library what debug level to emit and to send it to syslog */
lws_set_log_level(debug_level, lwsl_emit_syslog);
lwsl_notice("libwebsockets test server libev - license LGPL2.1+SLE\n");
lwsl_notice("(C) Copyright 2010-2016 Andy Green <[email protected]>\n");
printf("Using resource path \"%s\"\n", resource_path);
info.iface = iface;
info.protocols = protocols;
info.extensions = exts;
info.ssl_cert_filepath = NULL;
info.ssl_private_key_filepath = NULL;
if (use_ssl) {
if (strlen(resource_path) > sizeof(cert_path) - 32) {
lwsl_err("resource path too long\n");
return -1;
}
sprintf(cert_path, "%s/libwebsockets-test-server.pem",
resource_path);
if (strlen(resource_path) > sizeof(key_path) - 32) {
lwsl_err("resource path too long\n");
return -1;
}
sprintf(key_path, "%s/libwebsockets-test-server.key.pem",
resource_path);
/**
* Initialize flow-control context and process command-line arguments
*
* Sets fc->r to zero upon success, a non-zero errno code, otherwise.
*
* @param fc The flow control context
* @param argc The number of command-line arguments
* @param argv The command-line arguments
* @return Upon success, zero. A non-zero errno code, otherwise.
*/
static void flow_init( struct flow_context *fc ) {
int r;
int i;
struct sockaddr_in sa;
socklen_t sa_len;
sighandler_t old_sh;
// determine the service name
if ( NULL == fc->ident ) {
memcpy(
flow_progname,
fc->argv[ 0 ],
MIN(
strlen( fc->argv[ 0 ] ),
sizeof( flow_progname - 1 )
)
);
fc->ident = basename( flow_progname );
}
// use getopt(3), create a usage(), etc
// options could include verbosity
#ifdef HAVE_SYSLOG
// enable logging
openlog( fc->ident, 0, LOG_DAEMON );
setlogmask( LOG_UPTO( LOG_INFO ) );
// could be conditional, e.g. if command-line argument exists to enable / disable debug
setlogmask( setlogmask( 0 ) | LOG_MASK( LOG_DEBUG ) );
#endif
// set the global variable for signal handlers
_fc = fc;
I( "installing signal handlers.." );
for( i = 0; i < ARRAY_SIZE( signals_to_catch ); i++ ) {
old_sh = signal( signals_to_catch[ i ], sighandler );
if ( SIG_ERR == old_sh ) {
r = errno;
E( "signal(2) failed" );
goto out;
}
}
I( "opening signal pipe.." );
r = pipe( fc->signal_fd );
if ( EXIT_SUCCESS != r ) {
r = errno;
E( "pipe(2) failed" );
goto out;
}
D( "opened signal pipe as fd's %d, %d", fc->signal_fd[ 0 ], fc->signal_fd[ 1 ] );
cas( & fc->highest_fd, fc->signal_fd[ 0 ] );
I( "opening server socket.." );
r = socket( AF_INET, SOCK_DGRAM, 0 );
if ( -1 == r ) {
r = errno;
E( "socket(2) failed" );
goto out;
}
fc->server_socket = r;
D( "opened server socket as fd %d", fc->server_socket );
I( "binding server socket.." );
sa.sin_family = AF_INET;
sa.sin_port = htons( fc->server_port );
sa.sin_addr.s_addr = htonl( INADDR_ANY );
sa_len = sizeof( sa );
r = bind( fc->server_socket, (struct sockaddr *) & sa, sa_len );
if ( -1 == r ) {
r = errno;
E( "bind(2) failed" );
goto out;
}
D( "bound server socket to port %d", fc->server_port );
cas( & fc->highest_fd, fc->server_socket );
out:
fc->r = r;
}
开发者ID:Syanna,项目名称:firmware,代码行数:96,代码来源:flow.c
示例7: LOG_UPTO
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* This file is part of the Arduino Che Cosa project.
*/
#include "Cosa/Trace.hh"
Trace trace;
uint8_t trace_log_mask = LOG_UPTO(LOG_INFO);
bool
Trace::begin(IOStream::Device* dev, const char* banner)
{
set_device(dev);
if (banner != NULL) {
print_P(banner);
println();
}
return (true);
}
void
Trace::fatal_P(const char* file, int line, const char* expr)
{
开发者ID:Dzenik,项目名称:Cosa,代码行数:31,代码来源:Trace.cpp
示例8: init_logging
/*
* Initialize the logging
*
* Called once per process, including forked children.
*/
void
init_logging(
const char * name,
u_int32 def_syslogmask,
int is_daemon
)
{
static int was_daemon;
const char * cp;
const char * pname;
/*
* ntpd defaults to only logging sync-category events, when
* NLOG() is used to conditionalize. Other libntp clients
* leave it alone so that all NLOG() conditionals will fire.
* This presumes all bits lit in ntp_syslogmask can't be
* configured via logconfig and all lit is thereby a sentinel
* that ntp_syslogmask is still at its default from libntp,
* keeping in mind this function is called in forked children
* where it has already been called in the parent earlier.
* Forked children pass 0 for def_syslogmask.
*/
if (INIT_NTP_SYSLOGMASK == ntp_syslogmask &&
0 != def_syslogmask)
ntp_syslogmask = def_syslogmask; /* set more via logconfig */
/*
* Logging. This may actually work on the gizmo board. Find a name
* to log with by using the basename
*/
cp = strrchr(name, DIR_SEP);
if (NULL == cp)
pname = name;
else
pname = 1 + cp; /* skip DIR_SEP */
progname = estrdup(pname);
#ifdef SYS_WINNT /* strip ".exe" */
cp = strrchr(progname, '.');
if (NULL != cp && !strcasecmp(cp, ".exe"))
progname[cp - progname] = '\0';
#endif
#if !defined(VMS)
if (is_daemon)
was_daemon = TRUE;
# ifndef LOG_DAEMON
openlog(progname, LOG_PID);
# else /* LOG_DAEMON */
# ifndef LOG_NTP
# define LOG_NTP LOG_DAEMON
# endif
openlog(progname, LOG_PID | LOG_NDELAY, (was_daemon)
? LOG_NTP
: 0);
# ifdef DEBUG
if (debug)
setlogmask(LOG_UPTO(LOG_DEBUG));
else
# endif /* DEBUG */
setlogmask(LOG_UPTO(LOG_DEBUG)); /* @@@ was INFO */
# endif /* LOG_DAEMON */
#endif /* !VMS */
}
int main(int argc, char **argv)
{
struct lws_context_creation_info info;
char interface_name[128] = "";
uv_timer_t timeout_watcher;
const char *iface = NULL;
char cert_path[1024];
char key_path[1024];
int use_ssl = 0;
int opts = 0;
int n = 0;
#ifndef _WIN32
int syslog_options = LOG_PID | LOG_PERROR;
#endif
#ifndef LWS_NO_DAEMONIZE
int daemonize = 0;
#endif
/*
* take care to zero down the info struct, he contains random garbaage
* from the stack otherwise
*/
memset(&info, 0, sizeof info);
info.port = 7681;
while (n >= 0) {
n = getopt_long(argc, argv, "eci:hsap:d:Dr:", options, NULL);
if (n < 0)
continue;
switch (n) {
case 'e':
opts |= LWS_SERVER_OPTION_LIBEV;
break;
#ifndef LWS_NO_DAEMONIZE
case 'D':
daemonize = 1;
#ifndef _WIN32
syslog_options &= ~LOG_PERROR;
#endif
break;
#endif
case 'd':
debug_level = atoi(optarg);
break;
case 's':
use_ssl = 1;
break;
case 'a':
opts |= LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT;
break;
case 'p':
info.port = atoi(optarg);
break;
case 'i':
strncpy(interface_name, optarg, sizeof interface_name);
interface_name[(sizeof interface_name) - 1] = '\0';
iface = interface_name;
break;
case 'c':
close_testing = 1;
fprintf(stderr, " Close testing mode -- closes on "
"client after 50 dumb increments"
"and suppresses lws_mirror spam\n");
break;
case 'r':
resource_path = optarg;
printf("Setting resource path to \"%s\"\n", resource_path);
break;
case 'h':
fprintf(stderr, "Usage: test-server "
"[--port=<p>] [--ssl] "
"[-d <log bitfield>] "
"[--resource_path <path>]\n");
exit(1);
}
}
#if !defined(WIN32)
#if !defined(LWS_NO_DAEMONIZE)
/*
* normally lock path would be /var/lock/lwsts or similar, to
* simplify getting started without having to take care about
* permissions or running as root, set to /tmp/.lwsts-lock
*/
if (daemonize && lws_daemonize("/tmp/.lwsts-lock")) {
fprintf(stderr, "Failed to daemonize\n");
return 1;
}
#endif
/* we will only try to log things according to our debug_level */
setlogmask(LOG_UPTO (LOG_DEBUG));
openlog("lwsts", syslog_options, LOG_DAEMON);
#endif
/* tell the library what debug level to emit and to send it to syslog */
lws_set_log_level(debug_level, lwsl_emit_syslog);
lwsl_notice("libwebsockets test server libuv - license LGPL2.1+SLE\n");
lwsl_notice("(C) Copyright 2010-2016 Andy Green <[email protected]>\n");
//.........这里部分代码省略.........
int main(int argc, char **argv)
{
struct libwebsocket_context *context;
int opts = 0;
char interface_name[128] = "";
const char *interface = NULL;
int syslog_options = LOG_PID | LOG_PERROR;
int listen_port = 7681;
struct lws_context_creation_info info;
int debug_level = 7;
int daemonize = 0;
memset(&info, 0, sizeof info);
char c;
int opt_index = 0;
while ((c = getopt_long(argc, argv, "hd:k:p:i:DK:", long_opts,
&opt_index)) != -1) {
switch (c) {
case 'D':
daemonize = 1;
syslog_options &= ~LOG_PERROR;
break;
case 'd':
debug_level = atoi(optarg);
break;
case 'p':
listen_port = atoi(optarg);
break;
case 'k':
strncpy(keyfile,optarg,sizeof(keyfile));
keyfile[sizeof(keyfile)-1] = 0;
break;
case 'K':
strncpy(knownhostfile,optarg,sizeof(knownhostfile));
knownhostfile[sizeof(knownhostfile)-1] = 0;
break;
case 'i':
strncpy(interface_name, optarg, sizeof interface_name);
interface_name[(sizeof interface_name) - 1] = '\0';
interface = interface_name;
break;
case '?':
case 'h':
fprintf(stderr,
"Usage: %s [OPTIONS]...\n"
"Websocket-speaking daemon to acquire authentication tokens\n"
"from compatible websites.\n\n"
" -p,--port NUM listen on port NUM. (default:%i)\n"
" -d,--debug NUM set debug level to NUM.\n"
" -k,--keyfile FILE specify IdentityFile for ssh.\n"
" -K,--knownhosts FILE specify special ssh known_hosts file\n"
" to store host keys acquired by authd\n"
" (defaults to %s)\n"
" -D,--daemonize run in background.\n"
" --help show this message and exit.\n",
argv[0],listen_port,knownhostfile);
exit(1);
}
}
/*
* normally lock path would be /var/lock/lwsts or similar, to
* simplify getting started without having to take care about
* permissions or running as root, set to /tmp/.lwsts-lock
*/
if (daemonize && lws_daemonize("/tmp/.lwstecho-lock")) {
fprintf(stderr, "Failed to daemonize\n");
return 1;
}
/* we will only try to log things according to our debug_level */
setlogmask(LOG_UPTO (LOG_DEBUG));
openlog("lwsts", syslog_options, LOG_DAEMON);
/* tell the library what debug level to emit and to send it to syslog */
lws_set_log_level(debug_level, lwsl_emit_syslog);
info.port = listen_port;
info.iface = interface;
info.protocols = protocols;
#ifndef LWS_NO_EXTENSIONS
info.extensions = libwebsocket_get_internal_extensions();
#endif
info.gid = -1;
info.uid = -1;
info.options = opts;
context = libwebsocket_create_context(&info);
if (context == NULL) {
lwsl_err("libwebsocket init failed\n");
return -1;
}
signal(SIGINT, sighandler);
/* setup temp file: */
int fd = mkstemp(tfname);
if (fd == -1) {
//.........这里部分代码省略.........
请发表评论