~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to .pc/gcc47-fixes.patch/daemon/src/fileutils.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2012-05-19 21:46:37 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120519214637-la8rbrford5kj6m3
Tags: 1.1.0-1
* New upstream release 
  - Fixes "FTBFS with libccrtp-dev/2.0.2 from experimental" (Closes: #663282)
* NEW Maintainer: Debian VoIP Team - Thanks Francois for your work.
  - (Closes: #665789: O: sflphone -- SIP and IAX2 compatible VoIP phone)
* Added Build-Depends: libdbus-c++-bin
* Add gcc47-fixes.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2011 Savoir-Faire Linux Inc.
 
3
 *  Author: Rafaël Carré <rafael.carre@savoirfairelinux.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
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.
 
29
 */
 
30
 
 
31
#include <libgen.h>
 
32
#include <dirent.h>
 
33
#include <sys/stat.h>
 
34
#include <fstream>
 
35
#include <cstdlib>
 
36
#include <signal.h>
 
37
#include <string>
 
38
#include <sstream>
 
39
#include <iostream>
 
40
#include "fileutils.h"
 
41
 
 
42
namespace fileutils {
 
43
// returns true if directory exists
 
44
bool check_dir(const char *path)
 
45
{
 
46
    DIR *dir = opendir(path);
 
47
 
 
48
    if (!dir) { // doesn't exist
 
49
        if (mkdir(path, 0755) != 0) {   // couldn't create the dir
 
50
            perror(path);
 
51
            return false;
 
52
        }
 
53
    } else
 
54
        closedir(dir);
 
55
 
 
56
    return true;
 
57
}
 
58
 
 
59
static char *program_dir = NULL;
 
60
 
 
61
void set_program_dir(char *program_path)
 
62
{
 
63
    program_dir = dirname(program_path);
 
64
}
 
65
 
 
66
const char *get_program_dir()
 
67
{
 
68
    return program_dir;
 
69
}
 
70
 
 
71
bool create_pidfile()
 
72
{
 
73
    const char * const xdg_env = XDG_CACHE_HOME;
 
74
    std::string path = xdg_env ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
 
75
 
 
76
    if (!check_dir(path.c_str()))
 
77
        return false;
 
78
 
 
79
    path += "sflphone";
 
80
 
 
81
    if (!check_dir(path.c_str()))
 
82
        return false;
 
83
 
 
84
    std::string pidfile = path + "/" PIDFILE;
 
85
    std::ifstream is(pidfile.c_str());
 
86
 
 
87
    if (is) {
 
88
        // PID file exists. Check if the former process is still alive or
 
89
        // not. If alive, give user a hint.
 
90
        int oldPid;
 
91
        is >> oldPid;
 
92
 
 
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;
 
97
            return false;
 
98
        }
 
99
    }
 
100
 
 
101
    // write pid file
 
102
    std::ofstream os(pidfile.c_str());
 
103
 
 
104
    if (!os) {
 
105
        perror(pidfile.c_str());
 
106
        return false;
 
107
    } else {
 
108
        os << getpid();
 
109
    }
 
110
 
 
111
    return true;
 
112
}
 
113
}