vfs: check userland buffers before reading them.
[haiku.git] / src / servers / power / power_button_monitor.cpp
blobfa0e0ab2d347c20a271e31f70970a163c11023b3
1 /*
2 * Copyright 2005-2014, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Rene Gollent
7 * Nathan Whitehorn
8 */
11 #include "power_button_monitor.h"
13 #include <string.h>
15 #include <Directory.h>
16 #include <Messenger.h>
17 #include <Roster.h>
18 #include <String.h>
20 #include <RosterPrivate.h>
23 static const char* kBasePath = "/dev/power/button";
26 PowerButtonMonitor::PowerButtonMonitor()
28 fFDs()
30 BDirectory dir;
31 if (dir.SetTo(kBasePath) != B_OK)
32 return;
34 entry_ref ref;
35 while (dir.GetNextRef(&ref) == B_OK) {
36 if (strncmp(ref.name, "power", 5) == 0) {
37 BString path;
38 path.SetToFormat("%s/%s", kBasePath, ref.name);
39 int fd = open(path.String(), O_RDONLY);
40 if (fd > 0)
41 fFDs.insert(fd);
47 PowerButtonMonitor::~PowerButtonMonitor()
49 for (std::set<int>::iterator it = fFDs.begin(); it != fFDs.end(); ++it)
50 close(*it);
54 void
55 PowerButtonMonitor::HandleEvent(int fd)
57 uint8 button_pressed;
58 if (read(fd, &button_pressed, 1) != 1)
59 return;
61 if (button_pressed) {
62 BRoster roster;
63 BRoster::Private rosterPrivate(roster);
65 rosterPrivate.ShutDown(false, false, false);