Main Page | Class List | File List | Class Members | File Members

lockfile.c

Go to the documentation of this file.
00001 /*
00002  *  $Id: lockfile.c,v 1.5 2005/12/15 20:19:08 ghost666 Exp $
00003  *
00004  *  rssbgr - RSS backgroud reader
00005  *  Copyright (C) 2005 Piotr 'GhosT' Wydrych
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #include "lockfile.h"
00023 
00024 void create_lockfile(void/*char *directory, char *lockfile*/)
00025 {
00026  int lockfile_d;
00027  unsigned int other_pid;
00028  FILE *lockfile_s;
00029  struct stat stats;
00030 
00031  if (mkdir(directory, S_IRWXU) == -1)
00032  {
00033   if ((errno != EEXIST) || ((stat(directory, &stats) == 0) && ((stats.st_mode & S_IFMT) != S_IFDIR)))
00034    DIE("Cannot create directory for storing settings. Program will exit.")
00035   chmod(directory, S_IRWXU);
00036  }
00037  if ((lockfile_d = open(lockfile, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
00038  {
00039   if ((errno==EEXIST) && (stat(lockfile, &stats) == 0) && ((stats.st_mode & S_IFMT) == S_IFREG) && ((lockfile_s = fopen(lockfile, "r")) != NULL) && (fscanf(lockfile_s, "%u", &other_pid) == 1))
00040   {
00041     fprintf(stdout, "Probably another copy of RSS Background Reader runs with PID %u. Program will exit.\nIf you are sure, that RSSBGR is not running, please delete the file %s\n", other_pid, lockfile);
00042     fclose(lockfile_s);
00043     _exit(1);
00044   }
00045   else
00046    DIE("Cannot create lockfile")
00047  }
00048  /*close(lockfile_d);
00049  lockfile_s=fopen(lockfile, "w");
00050  fprintf(lockfile_s, "%u", (int) getpid());
00051  fclose(lockfile_s);*/
00052  lockfile_s = fdopen(lockfile_d, "w");
00053  fprintf(lockfile_s, "%u", (int) getpid());
00054  fflush(lockfile_s);
00055  close(lockfile_d);
00056 }
00057 
00058 /* --------- */
00059 
00060 void check_lockfile(void/*char *lockfile*/)
00061 {
00062  time_t mtime, ctime;
00063  struct stat stats;
00064  
00065  if (stat(lockfile, &stats) == -1) return;
00066  mtime = stats.st_mtime;
00067  ctime = stats.st_ctime; 
00068  
00069  while(1)
00070  {
00071   sleep(CHECK_LOCKFILE_FREQUENCY);
00072   if ((stat(lockfile, &stats) == -1) || (stats.st_mtime != mtime) || (stats.st_ctime != ctime))
00073    return;
00074  }
00075 }

Generated on Fri May 26 08:40:18 2006 for RSSbgr by doxygen 1.3.6