2
* Copyright (C) 2011 Savoir-Faire Linux Inc.
3
* Author: Rafaël Carré <rafael.carre@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
40
#include "fileutils.h"
43
// returns true if directory exists
44
bool check_dir(const char *path)
46
DIR *dir = opendir(path);
48
if (!dir) { // doesn't exist
49
if (mkdir(path, 0755) != 0) { // couldn't create the dir
59
static char *program_dir = NULL;
61
void set_program_dir(char *program_path)
63
program_dir = dirname(program_path);
66
const char *get_program_dir()
73
const char * const xdg_env = XDG_CACHE_HOME;
74
std::string path = xdg_env ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
76
if (!check_dir(path.c_str()))
81
if (!check_dir(path.c_str()))
84
std::string pidfile = path + "/" PIDFILE;
85
std::ifstream is(pidfile.c_str());
88
// PID file exists. Check if the former process is still alive or
89
// not. If alive, give user a hint.
93
if (kill(oldPid, 0) == 0) {
94
// Use cerr because logging has not been initialized
95
std::cerr << "There is already a sflphoned daemon running in " <<
96
"the system. Starting Failed." << std::endl;
102
std::ofstream os(pidfile.c_str());
105
perror(pidfile.c_str());