/*******************************************************************************
* Copyright (C) 2004-2008 Intel Corp. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
*
* - Neither the name of Intel Corp. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "iatshareddata.h"
#include <cstdlib>
#include <cstdio>
#include <cerrno>
#include <csignal>
#include <syslog.h>
#include <unistd.h>
#include <fcntl.h>
#include "types.h"
#include "Protocol.h"
#include "version.h"
#include "ATVersion.h"
#include "glue.h"
#ifdef __sun
#include <signal.h>
#endif
#ifdef DAEMON
#include "daemonize.h"
#endif //DAEMON
/* Change this to whatever your daemon is called */
{
static int _running = 0;
if (running >= 0)
{
}
if (_running == 1)
{
return true;
}
else
{
return false;
}
}
{
#ifdef DEBUGLOG
#endif
}
//This needs to be global for termination action
void exitcleanup()
{
//syslog(EVENTLOG_INFORMATION_TYPE, "Service stopped\n");
closelog();
if (-1 != lock_pid_file_fd) {
lock_pid_file_fd = -1;
}
}
//Action termination
#ifdef __sun
extern "C"
#endif
{
PRINT("LMS Service received - Signal:%d Err:(%d) Code:(%d)\n", signum, sinfo->si_errno, sinfo->si_code);
if (isRunning()) {
isRunning(0);
}
}
void setTerminationHandler()
{
int sigSet = 0;
// Termination signal handler.
// Set up the structure to specify the termination action.
if (sigSet != 0) {
}
}
/*
* return: 1 if the lock is real, 0 if not real, -1 on error
**/
{
int lfp;
int lockpid;
if (lfp < 0) {
"unable to open lock file %s, code=%d (%s)",
return -1;
}
if (count < 1) {
"unable to read lock file %s, code=%d (%s)",
return -1;
}
if (lockpid <= 1) {
return 0;
}
/* See if the process still exists */
/* Process exists, lock is real */
return 1;
}
else {
/* Process is gone */
return 0;
}
}
/**
* lock_pid_file - creates a pid file and writes current process pid into it
*
* lockfile - name of a file to be created
*
* return: 0 on success, -1 on fatal error, -2 on error
**/
{
int error = 0;
int haserror = 0;
/* Create the lock file as the current user */
#ifdef __sun
if (lfp < 0) {
if (lock_is_real(lockfile)) {
"The LMS service is already running!");
}
else {
}
return -2;
}
else {
"unable to lock file %s, code=%d (%s)",
return -1;
}
}
#else
if (lfp < 0) {
"unable to create lock file %s, code=%d (%s)",
return -1;
}
if (EWOULDBLOCK == errno) {
} else {
"unable to lock file %s, code=%d (%s)",
return -2;
}
return -1;
}
"unable to fruncate lock file %s, code=%d (%s)",
return -2;
}
if (-1 == written) {
haserror = 1;
haserror = 1;
haserror = 1;
}
if (1 == haserror) {
"unable to write pid into lock file %s, code=%d (%s)",
return -2;
}
#endif // __sun
}
return 0;
}
{
bool alreadyFailed = false;
bool firstLoop = true;
bool init = false;
int connectCount = 0;
return 0;
}
umask(022);
#ifdef DAEMON
daemonize();
#else
#endif
if (-2 == lockresult) {
}
if (0 != lockresult) {
}
isRunning(1);
//syslog(EVENTLOG_INFORMATION_TYPE, "Service started\n");
while (isRunning()) {
if (!prot.IsInitialized()) {
if (init) {
#ifdef DEBUGLOG
#endif
init = false;
}
if (firstLoop) {
firstLoop = false;
}
// Failed to connect to the HECI driver.
// Sleep for a second and try again.
connectCount++;
if (connectCount >= QUICK_CONNECT_COUNT) {
} else {
}
continue;
}
init = true;
firstLoop = false;
connectCount = 0;
#ifdef DEBUGLOG
#endif
}
if (!prot.SocketsCreated()) {
if (!prot.CreateSockets()) {
if (!alreadyFailed) {
#ifdef DEBUGLOG
#endif
alreadyFailed = true;
}
continue;
} else {
alreadyFailed = false;
}
}
// Select on active sockets (IANA ports and open connections).
}
return 0;
}