What's new? | Help | Directory | Sign in
Google
             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2008, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#if defined(LINUX) && !defined(OS_MACOSX)
#ifdef OFFICIAL_BUILD
// The notification API has not been finalized for official builds.
int main(int argc, char *argv[]) {
return -1;
}
#else
#include <string>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>

#include <gtk/gtk.h>
#include "gears/notifier/notifier.h"
#include "gears/notifier/notifier_process_posix.h"
#include "third_party/glint/include/platform.h"

class LinuxNotifier : public Notifier {
public:
LinuxNotifier(bool debug);
virtual bool Initialize();
virtual int Run();
virtual void Terminate();
virtual void RequestQuit();

private:
bool StartAsDaemon();
virtual bool RegisterProcess();
virtual bool UnregisterProcess();

bool debug_;

DISALLOW_EVIL_CONSTRUCTORS(LinuxNotifier);
};

LinuxNotifier::LinuxNotifier(bool debug) : debug_(debug) {
}

bool LinuxNotifier::RegisterProcess() {
NotifierPosixUtils::RegisterIPC();
return true;
}

bool LinuxNotifier::UnregisterProcess() {
NotifierPosixUtils::UnregisterIPC();
return true;
}

// Start the instance as a background daemon process.
// TODO(dimich): Consider replacing this code with daemon() API, which exists
// on Linux.
bool LinuxNotifier::StartAsDaemon() {
// Clear file creation mask.
umask(0);

// Get maximum number of file descriptors.
rlimit rl = { 0 };
if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
LOG(("getrlimit failed with errno=%d\n", errno));
return false;
}

// Fork off the parent process in order to become a session leader to lose
// controlling TTY.
pid_t pid = fork();
if (pid < 0 ) {
LOG(("fork failed with errno=%d\n", errno));
return false;
} else if (pid > 0) {
exit(EXIT_SUCCESS); // parent process
}

// Create a new sid for the child process.
if (setsid() < 0) {
LOG(("setsid failed with errno=%d\n", errno));
return false;
}

// Ensure future open won't allocate controlling TTYs.
struct sigaction sig_action = { 0 };
sig_action.sa_handler = SIG_IGN;
sigemptyset(&sig_action.sa_mask);
sig_action.sa_flags = 0;
if (sigaction(SIGHUP, &sig_action, NULL) < 0) {
LOG(("sigaction failed with errno=%d\n", errno));
}
pid = fork();
if (pid < 0) {
LOG(("fork failed with errno=%d\n", errno));
return false;
} else if (pid > 0) {
exit(EXIT_SUCCESS); // parent process
}

// Change the current working directory to the root so we won't prevent file
// systems from being unmounted.
if (chdir("/") < 0) {
LOG(("chdir failed with errno=%d\n", errno));
return false;
}

// Close all open file descriptors.
if (rl.rlim_max == RLIM_INFINITY) {
rl.rlim_max = 1024;
}
for (int i = 0; i < static_cast<int>(rl.rlim_max); ++i) {
close(i);
}

// Attach standard file descriptors to /dev/null.
open("/dev/null", O_RDWR);
dup(0);
dup(0);

return true;
}

bool LinuxNotifier::Initialize() {
// Check if the Notifier process is already running.
if (NotifierPosixUtils::FindNotifierProcess() != 0) {
return false;
}

// Start as a daemon process. Do this only if debug option is not present.
if (!debug_) {
if (!StartAsDaemon()) {
return false;
}
}

// Single instance check - *nix way, with a lock file.
if (!NotifierPosixUtils::CreateLockFile()) {
return false;
}

// This will setup IPC and call LinuxNotifier::RegisterProcess
return Notifier::Initialize();
}

void LinuxNotifier::Terminate() {
NotifierPosixUtils::DeleteLockFile();
Notifier::Terminate();
}

void LinuxNotifier::RequestQuit() {
running_ = false;
// TODO(levin): Should this be part of glint::platform,
// since it relies on knowing what loop is running there?
gtk_main_quit();
}

int LinuxNotifier::Run() {
running_ = true;
while (running_) {
glint::platform()->RunMessageLoop();
}
return 0;
}

int main(int argc, char *argv[]) {
LOG(("Gears Notifier started.\n"));

bool debug = false;
#ifdef DEBUG
if (argc > 1 && strcmp(argv[1], "-debug") == 0) {
debug = true;
}
#endif // DEBUG

LinuxNotifier notifier(debug);

int retval = -1;
if (notifier.Initialize()) {
retval = notifier.Run();
notifier.Terminate();
}

LOG(("Gears Notifier terminated.\n"));
return retval;
}

#endif // OFFICIAL_BUILD
#endif // defined(LINUX) && !defined(OS_MACOSX)
Show details Hide details

Change log

r2747 by gears.daemon on Aug 21, 2008   Diff
[Author: levin]

Start notifier process on demand.

PRESUBMIT=passed
R=jianli
CC=gears-eng@googlegroups.com
DELTA=116  (111 added, 2 deleted, 3
changed)
OCL=8024354
SCL=8062903
Go to: 
Project members, sign in to write a code review

Older revisions

r2641 by gears.daemon on Aug 07, 2008   Diff
[Author: jianli]

Implement user activity detection for
Linux platform:
 * Detect input activity: including
...
r2542 by gears.daemon on Jul 30, 2008   Diff
[Author: levin]

Fix unit tests on linux and make
notifications work.

...
r2484 by gears.daemon on Jul 23, 2008   Diff
[Author: dimich]

OSX notifications - first step.
- Safari plugin can launch notifier
process (or find it if it is already
...
All revisions of this file

File info

Size: 5871 bytes, 211 lines