OW_PidFile.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_PidFile.hpp"
00038 #include "OW_File.hpp"
00039 #include "OW_FileSystem.hpp"
00040 #include "OW_String.hpp"
00041 
00042 #include <stdio.h>
00043 #include <unistd.h>
00044 #include <sys/types.h>
00045 #include <signal.h>
00046 #include <errno.h>
00047 #include <sys/stat.h>
00048 #include <fcntl.h>
00049 
00050 
00051 namespace OW_NAMESPACE
00052 {
00053 
00054 namespace PidFile
00055 {
00061 int 
00062 readPid(const char *pidfile)
00063 {
00064    FILE *f;
00065    int pid = -1;
00066    if (!(f = fopen(pidfile,"r")))
00067       return -1;
00068    fscanf(f,"%d", &pid);
00069    fclose(f);
00070    return pid;
00071 }
00077 int 
00078 checkPid(const char *pidfile)
00079 {
00080    int pid = readPid(pidfile);
00081    // Amazing ! _I_ am already holding the pid file...
00082    if ((!pid) || (pid == getpid()))
00083       return -1;
00084    // Check if the process exists
00085    if (kill(pid, 0) && errno == ESRCH)
00086       return -1;
00087    return pid;
00088 }
00095 int 
00096 writePid(const char *pidfile)
00097 {
00098    FILE *f;
00099    int fd;
00100    int pid;
00101    int lerrno;
00102    if ((fd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1)
00103    {
00104       return -1;
00105    }
00106    if ((f = fdopen(fd, "r+")) == NULL)
00107    {
00108       lerrno = errno;
00109       close(fd);
00110       errno = lerrno;
00111       return -1;
00112    }
00113    File OWf(::dup(fd)); // need a dup, since the FILE* will close it.
00114    if (OWf.tryLock() == -1)
00115    {
00116       lerrno = errno;
00117       fscanf(f, "%d", &pid);
00118       fclose(f);
00119       errno = lerrno;
00120       return -1;
00121    }
00122    pid = getpid();
00123    if (!fprintf(f,"%d\n", pid))
00124    {
00125       lerrno = errno;
00126       fclose(f);
00127       errno = lerrno;
00128       return -1;
00129    }
00130    fflush(f);
00131    OWf.unlock();
00132    fclose(f);
00133    return pid;
00134 }
00140 int 
00141 removePid(const char *pidfile)
00142 {
00143    return FileSystem::removeFile(pidfile);
00144 }
00145 
00146 } // end namespace PidFile
00147 } // end namespace OW_NAMESPACE
00148 

Generated on Thu Feb 9 08:48:08 2006 for openwbem by  doxygen 1.4.6