~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to client/start_server.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2010, Google Inc.
2
 
// All rights reserved.
3
 
//
4
 
// Redistribution and use in source and binary forms, with or without
5
 
// modification, are permitted provided that the following conditions are
6
 
// met:
7
 
//
8
 
//     * Redistributions of source code must retain the above copyright
9
 
// notice, this list of conditions and the following disclaimer.
10
 
//     * Redistributions in binary form must reproduce the above
11
 
// copyright notice, this list of conditions and the following disclaimer
12
 
// in the documentation and/or other materials provided with the
13
 
// distribution.
14
 
//     * Neither the name of Google Inc. nor the names of its
15
 
// contributors may be used to endorse or promote products derived from
16
 
// this software without specific prior written permission.
17
 
//
18
 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
 
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 
 
30
 
#include "client/session.h"
31
 
 
32
 
#ifdef OS_WINDOWS
33
 
#include <string.h>
34
 
#include <windows.h>
35
 
#include <sddl.h>
36
 
#include <shlobj.h>
37
 
#include "third_party/mozc/sandbox/restricted_token_utils.h"
38
 
#else
39
 
#include <spawn.h>  // for posix_spawnp().
40
 
#include <unistd.h>
41
 
#include <errno.h>
42
 
#include <sys/types.h>
43
 
#include <sys/wait.h>
44
 
#endif
45
 
#include "base/base.h"
46
 
#include "base/const.h"
47
 
#include "base/file_stream.h"
48
 
#include "base/process.h"
49
 
#include "base/run_level.h"
50
 
#include "base/util.h"
51
 
#include "ipc/ipc.h"
52
 
#include "ipc/named_event.h"
53
 
#ifdef OS_MACOSX
54
 
#include "base/mac_util.h"
55
 
#endif
56
 
 
57
 
namespace mozc {
58
 
namespace client {
59
 
namespace {
60
 
const char kServerName[] = "session";
61
 
 
62
 
// Wait at most kServerWaitTimeout msec until server gets ready
63
 
const uint32 kServerWaitTimeout = 20000;  // 20 sec
64
 
 
65
 
// for every 1000m sec, check server
66
 
const uint32 kRetryIntervalForServer = 1000;
67
 
 
68
 
// Try 20 times to check mozc_server is running
69
 
const uint32 kTrial = 20;
70
 
 
71
 
#ifdef _DEBUG
72
 
// Load special flags for server.
73
 
// This should be enabled on debug build
74
 
const string LoadServerFlags() {
75
 
  const char kServerFlagsFile[] = "mozc_server_flags.txt";
76
 
  const string filename = Util::JoinPath(Util::GetUserProfileDirectory(),
77
 
                                         kServerFlagsFile);
78
 
  string flags;
79
 
  InputFileStream ifs(filename.c_str());
80
 
  if (ifs) {
81
 
    getline(ifs, flags);
82
 
  }
83
 
  VLOG(1) << "New server flag: " << flags;
84
 
  return flags;
85
 
}
86
 
#endif
87
 
}  // namespace
88
 
 
89
 
// initialize default path
90
 
StartServerHandler::StartServerHandler()
91
 
    : server_program_(Util::GetServerPath()),
92
 
      restricted_(false) {}
93
 
 
94
 
StartServerHandler::~StartServerHandler() {}
95
 
 
96
 
bool StartServerHandler::StartServer(SessionInterface *session) {
97
 
  if (server_program().empty()) {
98
 
    LOG(ERROR) << "Server path is empty";
99
 
    return false;
100
 
  }
101
 
 
102
 
  // ping first
103
 
  if (session->PingServer()) {
104
 
    return true;
105
 
  }
106
 
 
107
 
  string arg;
108
 
 
109
 
#ifdef OS_WINDOWS
110
 
  // When mozc is not used as a default IME and some applications (like notepad)
111
 
  // are registered in "Start up", mozc_server may not be launched successfully.
112
 
  // This is because the Explorer launches start-up processes inside a group job
113
 
  // and the process inside a job cannot make our sandboxed child processes.
114
 
  // The group job is unregistered after 60 secs (default).
115
 
  //
116
 
  // Here we relax the sandbox restriction if process is in a job.
117
 
  // In order to keep security, the mozc_server is launched
118
 
  // with restricted mode.
119
 
 
120
 
  const bool process_in_job = RunLevel::IsProcessInJob();
121
 
  if (process_in_job || restricted_) {
122
 
    LOG(WARNING)
123
 
        << "Parent process is in job. start with restricted mode";
124
 
    arg += "--restricted";
125
 
  }
126
 
#endif
127
 
 
128
 
#ifdef _DEBUG
129
 
  // In oreder to test the Session treatment (timeout/size constratins),
130
 
  // Server flags can be configurable on DEBUG build
131
 
  if (!arg.empty()) {
132
 
    arg += " ";
133
 
  }
134
 
  arg += LoadServerFlags();
135
 
#endif  // _DEBUG
136
 
 
137
 
  NamedEventListener listener(kServerName);
138
 
  const bool listener_is_available = listener.IsAvailable();
139
 
 
140
 
  size_t pid = 0;
141
 
#ifdef OS_WINDOWS
142
 
  mozc::Process::SecurityInfo info;
143
 
  info.primary_level = sandbox::USER_INTERACTIVE;
144
 
  info.impersonation_level = sandbox::USER_RESTRICTED_SAME_ACCESS;
145
 
  info.job_level =  process_in_job ?
146
 
      sandbox::JOB_NO_JOB : sandbox::JOB_LOCKDOWN;
147
 
  info.integrity_level = sandbox::INTEGRITY_LEVEL_LOW;
148
 
  info.allow_ui_operation = false;
149
 
  info.in_system_dir = true;  // use system dir not to lock current directory
150
 
  info.creation_flags = CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW;
151
 
 
152
 
  DWORD tmp_pid = 0;
153
 
  const bool result = mozc::Process::SpawnProcessAs(
154
 
      server_program(), arg, info, &tmp_pid);
155
 
  pid = static_cast<size_t>(tmp_pid);
156
 
 
157
 
  if (!result) {
158
 
    LOG(ERROR) << "Can't start process: " << ::GetLastError();
159
 
    return false;
160
 
  }
161
 
#elif defined(OS_MACOSX)
162
 
  // Use launchd API instead of spawning process.  It doesn't use
163
 
  // server_program() at all.
164
 
  const bool result = MacUtil::StartLaunchdServce(
165
 
      "Converter", reinterpret_cast<pid_t *>(&pid));
166
 
  if (!result) {
167
 
      LOG(ERROR) << "Can't start process";
168
 
      return false;
169
 
    }
170
 
#else
171
 
  const bool result = mozc::Process::SpawnProcess(server_program(),
172
 
                                                  arg,
173
 
                                                  &pid);
174
 
  if (!result) {
175
 
    LOG(ERROR) << "Can't start process: " << strerror(result);
176
 
    return false;
177
 
  }
178
 
#endif  // OS_WINDOWS
179
 
 
180
 
  // maybe another process will launch mozc_server at the same time.
181
 
  if (session->PingServer()) {
182
 
    VLOG(1) << "Another process has launched the server";
183
 
    return true;
184
 
  }
185
 
 
186
 
  // Common part:
187
 
  // Wait until mozc_server becomes ready to process requests
188
 
  if (listener_is_available) {
189
 
    const int ret = listener.WaitEventOrProcess(kServerWaitTimeout, pid);
190
 
    switch (ret) {
191
 
      case NamedEventListener::TIMEOUT:
192
 
        LOG(WARNING) << "seems that Google Japanese Input Converter is not "
193
 
                     << "ready within " << kServerWaitTimeout << " msec";
194
 
        break;
195
 
      case NamedEventListener::EVENT_SIGNALED:
196
 
        VLOG(1) << "Google Japanese Input Converter is launched successfully "
197
 
                << "within " << kServerWaitTimeout << " msec";
198
 
        break;
199
 
      case NamedEventListener::PROCESS_SIGNALED:
200
 
        LOG(ERROR) << "Mozc server is terminated";
201
 
        // Mozc may be terminated because another client launches mozc_server
202
 
        if (session->PingServer()) {
203
 
          return true;
204
 
        }
205
 
        return false;
206
 
    }
207
 
  } else {
208
 
    // maybe another process is trying to launch mozc_server.
209
 
    LOG(ERROR) << "cannot make NamedEventListener ";
210
 
    Util::Sleep(kRetryIntervalForServer);
211
 
  }
212
 
 
213
 
  // Try to connect mozc_server just in case.
214
 
  for (int trial = 0; trial < kTrial; ++trial) {
215
 
    if (session->PingServer()) {
216
 
      return true;
217
 
    }
218
 
    Util::Sleep(kRetryIntervalForServer);
219
 
  }
220
 
 
221
 
  LOG(ERROR) << "Google Japanese Input Converter cannot be launched";
222
 
 
223
 
  return false;
224
 
}
225
 
 
226
 
bool StartServerHandler::ForceTerminateServer(const string &name) {
227
 
  return IPCClient::TerminateServer(name);
228
 
}
229
 
 
230
 
bool StartServerHandler::WaitServer(uint32 pid) {
231
 
  const int kTimeout = 10000;
232
 
  return Process::WaitProcess(static_cast<size_t>(pid), kTimeout);
233
 
}
234
 
 
235
 
void StartServerHandler::OnFatal(
236
 
    StartServerHandlerInterface::ServerErrorType type) {
237
 
  LOG(ERROR) << "OnFatal is called: " << static_cast<int>(type);
238
 
 
239
 
  string error_type;
240
 
  switch (type) {
241
 
    case StartServerHandlerInterface::SERVER_TIMEOUT:
242
 
      error_type = "server_timeout";
243
 
      break;
244
 
    case StartServerHandlerInterface::SERVER_BROKEN_MESSAGE:
245
 
      error_type = "server_broken_message";
246
 
      break;
247
 
    case StartServerHandlerInterface::SERVER_VERSION_MISMATCH:
248
 
      error_type = "server_version_mismatch";
249
 
      break;
250
 
    case StartServerHandlerInterface::SERVER_SHUTDOWN:
251
 
      error_type = "server_shutdown";
252
 
      break;
253
 
    case StartServerHandlerInterface::SERVER_FATAL:
254
 
      error_type = "server_fatal";
255
 
      break;
256
 
    default:
257
 
      LOG(ERROR) << "Unknown error: " << type;
258
 
      return;
259
 
  }
260
 
 
261
 
  Process::LaunchErrorMessageDialog(error_type);
262
 
}
263
 
}  // client
264
 
}  // mozc