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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
/*
* Copyright (C) 2013 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "UbuntuMtpDatabase.h"
#include <MtpServer.h>
#include <MtpStorage.h>
#include <iostream>
#include <stdint.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <pwd.h>
#include <libintl.h>
#include <locale.h>
#include <hybris/properties/properties.h>
#include <glog/logging.h>
#include <core/dbus/bus.h>
#include <core/dbus/object.h>
#include <core/dbus/property.h>
#include <core/dbus/service.h>
#include <core/dbus/signal.h>
#include <core/dbus/asio/executor.h>
#include <core/dbus/types/stl/tuple.h>
#include <core/dbus/types/stl/vector.h>
#include <core/dbus/types/struct.h>
namespace dbus = core::dbus;
using namespace android;
namespace core
{
dbus::Bus::Ptr the_session_bus()
{
static dbus::Bus::Ptr session_bus = std::make_shared<dbus::Bus>(dbus::WellKnownBus::session);
return session_bus;
}
struct UnityGreeter
{
struct Properties
{
struct IsActive
{
inline static std::string name()
{
return "IsActive";
};
typedef UnityGreeter Interface;
typedef bool ValueType;
static const bool readable = true;
static const bool writable = false;
};
};
};
}
namespace core
{
namespace dbus
{
namespace traits
{
template<>
struct Service<core::UnityGreeter>
{
inline static const std::string& interface_name()
{
static const std::string s
{
"com.canonical.UnityGreeter"
};
return s;
}
};
}
}
}
namespace
{
struct FileSystemConfig
{
static const int file_perm = 0664;
static const int directory_perm = 0755;
};
}
class MtpDaemon
{
private:
struct passwd *userdata;
dbus::Bus::Ptr bus;
boost::thread dbus_thread;
// Mtp stuff
MtpServer* server;
MtpStorage* home_storage;
MtpStorage* sd_card;
MtpDatabase* mtp_database;
// Security
std::shared_ptr<core::dbus::Property<core::UnityGreeter::Properties::IsActive> > is_active;
bool screen_locked = true;
// inotify stuff
boost::thread notifier_thread;
boost::thread io_service_thread;
asio::io_service io_svc;
asio::io_service::work work;
asio::posix::stream_descriptor stream_desc;
asio::streambuf buf;
int inotify_fd;
int watch_fd;
int media_fd;
// storage
std::map<std::string, std::tuple<MtpStorage*, bool> > removables;
bool home_storage_added;
void add_removable_storage(const char *path, const char *name)
{
static int storageID = MTP_STORAGE_REMOVABLE_RAM;
/* TODO check removable file system type to set maximum file size */
MtpStorage *removable = new MtpStorage(
storageID,
path,
name,
1024 * 1024 * 100, /* 100 MB reserved space, to avoid filling the disk */
true,
UINT64_C(4294967295) /* 4GB-1, we assume vfat here */);
storageID++;
if (!screen_locked) {
mtp_database->addStoragePath(path,
std::string(),
removable->getStorageID(),
true);
server->addStorage(removable);
}
removables.insert(std::pair<std::string, std::tuple<MtpStorage*, bool> >
(name,
std::make_tuple(removable,
screen_locked ? false : true)));
}
void add_mountpoint_watch(const std::string& path)
{
VLOG(1) << "Adding notify watch for " << path;
watch_fd = inotify_add_watch(inotify_fd,
path.c_str(),
IN_CREATE | IN_DELETE);
}
void read_more_notify()
{
VLOG(1) << __PRETTY_FUNCTION__;
stream_desc.async_read_some(buf.prepare(buf.max_size()),
boost::bind(&MtpDaemon::inotify_handler,
this,
asio::placeholders::error,
asio::placeholders::bytes_transferred));
}
void inotify_handler(const boost::system::error_code&,
std::size_t transferred)
{
size_t processed = 0;
while(transferred - processed >= sizeof(inotify_event))
{
const char* cdata = processed + asio::buffer_cast<const char*>(buf.data());
const inotify_event* ievent = reinterpret_cast<const inotify_event*>(cdata);
path storage_path ("/media");
processed += sizeof(inotify_event) + ievent->len;
storage_path /= userdata->pw_name;
if (ievent->len > 0 && ievent->mask & IN_CREATE)
{
if (ievent->wd == media_fd) {
VLOG(1) << "media root was created for user " << ievent->name;
add_mountpoint_watch(storage_path.string());
} else {
VLOG(1) << "Storage was added: " << ievent->name;
storage_path /= ievent->name;
add_removable_storage(storage_path.string().c_str(), ievent->name);
}
}
else if (ievent->len > 0 && ievent->mask & IN_DELETE)
{
VLOG(1) << "Storage was removed: " << ievent->name;
// Try to match to which storage was removed.
BOOST_FOREACH(std::string name, removables | boost::adaptors::map_keys) {
if (name == ievent->name) {
auto t = removables.at(name);
MtpStorage *storage = std::get<0>(t);
VLOG(2) << "removing storage id "
<< storage->getStorageID();
server->removeStorage(storage);
mtp_database->removeStorage(storage->getStorageID());
}
}
}
}
read_more_notify();
}
void drive_bus()
{
try {
bus->run();
}
catch (...) {
PLOG(ERROR) << "There was an unexpected error in DBus; terminating.";
server->stop();
}
}
public:
MtpDaemon(int fd):
stream_desc(io_svc),
work(io_svc),
buf(1024)
{
userdata = getpwuid (getuid());
// Removable storage hacks
inotify_fd = inotify_init();
if (inotify_fd <= 0)
PLOG(FATAL) << "Unable to initialize inotify";
VLOG(1) << "using inotify fd " << inotify_fd << " for daemon";
stream_desc.assign(inotify_fd);
notifier_thread = boost::thread(&MtpDaemon::read_more_notify, this);
io_service_thread = boost::thread(boost::bind(&asio::io_service::run, &io_svc));
// MTP database.
mtp_database = new UbuntuMtpDatabase();
// MTP server
server = new MtpServer(
fd,
mtp_database,
false,
userdata->pw_gid,
FileSystemConfig::file_perm,
FileSystemConfig::directory_perm);
// security / screen locking
bus = core::the_session_bus();
bus->install_executor(core::dbus::asio::make_executor(bus));
dbus_thread = boost::thread(&MtpDaemon::drive_bus, this);
auto greeter_service = dbus::Service::use_service(bus, "com.canonical.UnityGreeter");
dbus::Object::Ptr greeter = greeter_service->object_for_path(dbus::types::ObjectPath("/"));
is_active = greeter->get_property<core::UnityGreeter::Properties::IsActive>();
}
void initStorage()
{
char product_name[PROP_VALUE_MAX];
// Local storage
property_get ("ro.product.model", product_name, "Ubuntu Touch device");
home_storage = new MtpStorage(
MTP_STORAGE_FIXED_RAM,
userdata->pw_dir,
product_name,
1024 * 1024 * 100, /* 100 MB reserved space, to avoid filling the disk */
false,
0 /* Do not check sizes for internal storage */);
mtp_database->addStoragePath(std::string(userdata->pw_dir) + "/Documents",
gettext("Documents"),
MTP_STORAGE_FIXED_RAM, false);
mtp_database->addStoragePath(std::string(userdata->pw_dir) + "/Music",
gettext("Music"),
MTP_STORAGE_FIXED_RAM, false);
mtp_database->addStoragePath(std::string(userdata->pw_dir) + "/Videos",
gettext("Videos"),
MTP_STORAGE_FIXED_RAM, false);
mtp_database->addStoragePath(std::string(userdata->pw_dir) + "/Pictures",
gettext("Pictures"),
MTP_STORAGE_FIXED_RAM, false);
mtp_database->addStoragePath(std::string(userdata->pw_dir) + "/Downloads",
gettext("Downloads"),
MTP_STORAGE_FIXED_RAM, false);
home_storage_added = false;
// Get any already-mounted removable storage.
path p(std::string("/media/") + userdata->pw_name);
if (exists(p)) {
std::vector<path> v;
copy(directory_iterator(p), directory_iterator(), std::back_inserter(v));
for (std::vector<path>::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it)
{
add_removable_storage(it->string().c_str(), it->leaf().c_str());
}
// make sure we can catch any new removable storage that gets added.
add_mountpoint_watch(p.string());
} else {
media_fd = inotify_add_watch(inotify_fd,
"/media",
IN_CREATE | IN_DELETE);
}
}
~MtpDaemon()
{
// Cleanup
inotify_rm_watch(inotify_fd, watch_fd);
io_svc.stop();
dbus_thread.detach();
notifier_thread.detach();
io_service_thread.join();
close(inotify_fd);
}
void run()
{
if (is_active->get()) {
is_active->changed().connect([this](bool active)
{
if (!active) {
screen_locked = active;
VLOG(2) << "device was unlocked, adding storage";
if (home_storage && !home_storage_added) {
server->addStorage(home_storage);
home_storage_added = true;
}
BOOST_FOREACH(std::string name, removables | boost::adaptors::map_keys) {
auto t = removables.at(name);
MtpStorage *storage = std::get<0>(t);
bool added = std::get<1>(t);
if (!added) {
mtp_database->addStoragePath(storage->getPath(),
std::string(),
storage->getStorageID(),
true);
server->addStorage(storage);
}
}
}
});
} else {
screen_locked = false;
VLOG(2) << "device is not locked, adding storage";
if (home_storage) {
server->addStorage(home_storage);
home_storage_added = true;
}
BOOST_FOREACH(std::string name, removables | boost::adaptors::map_keys) {
auto t = removables.at(name);
MtpStorage *storage = std::get<0>(t);
bool added = std::get<1>(t);
if (!added) {
mtp_database->addStoragePath(storage->getPath(),
std::string(),
storage->getStorageID(),
true);
server->addStorage(storage);
}
}
}
// start the MtpServer main loop
server->run();
}
};
int main(int argc, char** argv)
{
google::InitGoogleLogging(argv[0]);
bindtextdomain("mtp-server", "/usr/share/locale");
setlocale(LC_ALL, "");
textdomain("mtp-server");
LOG(INFO) << "MTP server starting...";
int fd = open("/dev/mtp_usb", O_RDWR);
if (fd < 0)
{
LOG(ERROR) << "Error opening /dev/mtp_usb, aborting now...";
return 1;
}
try {
MtpDaemon *d = new MtpDaemon(fd);
d->initStorage();
d->run();
delete d;
}
catch (std::exception& e) {
/* If the daemon fails to initialize, ignore the error but
* make sure to propagate the message and return with an
* error return code.
*/
LOG(ERROR) << "Could not start the MTP server:" << e.what();
}
}
|