process.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /**
00023  * \file process.cpp
00024  * \version $Id: process.cpp 995 2006-06-09 12:24:39Z edmanm $
00025  * \brief Process information and pidfile functions 
00026  */
00027 
00028 #include <QDir>
00029 #include <QFile>
00030 #include <QFileInfo>
00031 #include <QTextStream>
00032 #include <vidalia.h>
00033 
00034 #include "string.h"
00035 #include "process.h"
00036 
00037 
00038 /** Returns the PID of the current process. */
00039 qint64
00040 get_pid()
00041 {
00042 #if defined(Q_OS_WIN)
00043   return (qint64)GetCurrentProcessId();
00044 #else
00045   return (qint64)getpid();
00046 #endif
00047 }
00048 
00049 /** Returns true if a process with the given PID is running. */
00050 bool
00051 is_process_running(qint64 pid)
00052 {
00053 #if defined(Q_OS_WIN)
00054   QHash<qint64, QString> procList = win32_process_list();
00055   if (procList.contains(pid)) {
00056     /* A process with this ID exists. Check if it's Vidalia. */
00057     QString exeFile = procList.value(pid);
00058     QString vidaliaExe = QFileInfo(Vidalia::applicationFilePath()).fileName();
00059     return (exeFile.toLower() == vidaliaExe.toLower());
00060   }
00061   return false;
00062 #else
00063   /* Send the "null" signal to check if a process exists */
00064   if (kill((pid_t)pid, 0) < 0) {
00065     return (errno != ESRCH);
00066   }
00067   return true;
00068 #endif
00069 }
00070 
00071 /** Writes the given file to disk containing the current process's PID. */
00072 bool
00073 write_pidfile(QString pidFileName, QString *errmsg)
00074 {
00075   /* Make sure the directory exists */
00076   QDir pidFileDir = QFileInfo(pidFileName).absoluteDir();
00077   if (!pidFileDir.exists()) {
00078     pidFileDir.mkpath(QDir::convertSeparators(pidFileDir.absolutePath()));
00079   }
00080 
00081   /* Try to open (and create if it doesn't exist) the pidfile */
00082   QFile pidfile(pidFileName);
00083   if (!pidfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
00084     return err(errmsg, pidfile.errorString());
00085   }
00086 
00087   /* Write our current PID to it */
00088   QTextStream pidstream(&pidfile);
00089   pidstream << get_pid();
00090   return true;
00091 }
00092 
00093 /** Reads the given pidfile and returns the value contained in it. If the file
00094  * does not exist 0 is returned. Returns -1 if an error occurs. */
00095 qint64 
00096 read_pidfile(QString pidFileName, QString *errmsg)
00097 {
00098   qint64 pid;
00099   
00100   /* Open the pidfile, if it exists */
00101   QFile pidfile(pidFileName);
00102   if (!pidfile.exists()) {
00103     return 0;
00104   }
00105   if (!pidfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
00106     if (errmsg) {
00107       *errmsg = pidfile.errorString();
00108     }
00109     return -1; 
00110   }
00111   
00112   /* Read the PID in from the file */
00113   QTextStream pidstream(&pidfile);
00114   pidstream >> pid;
00115   return pid;
00116 }
00117 

Generated on Mon Oct 23 20:08:16 2006 for Vidalia by  doxygen 1.5.0