vfs: check userland buffers before reading them.
[haiku.git] / src / apps / cortex / AddOnHost / AddOnHostApp.cpp
blobe481c4aa69089c51b8a4d572d1de7261dbdfa433
1 /*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions, and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (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.
32 // AddOnHostApp.cpp
34 #include "AddOnHostApp.h"
35 #include "AddOnHostProtocol.h"
37 #include <Alert.h>
38 #include <Debug.h>
39 #include <MediaRoster.h>
41 #include <cstdlib>
42 #include <cstring>
44 __USE_CORTEX_NAMESPACE
45 using namespace addon_host;
48 App::App()
49 : BApplication(g_appSignature)
54 App::~App()
59 bool
60 App::QuitRequested()
62 return true;
66 void
67 App::MessageReceived(BMessage* message)
69 status_t err;
71 // message->PrintToStream();
73 switch(message->what) {
74 case M_INSTANTIATE:
76 // fetch node info
77 dormant_node_info info;
78 const void *data;
79 ssize_t dataSize;
80 err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
81 if (err < B_OK) {
82 PRINT((
83 "!!! App::MessageReceived(M_INSTANTIATE):\n"
84 " missing 'info'\n"));
85 break;
87 if (dataSize != sizeof(info)) {
88 PRINT((
89 "* App::MessageReceived(M_INSTANTIATE):\n"
90 " warning: 'info' size mismatch\n"));
91 if (dataSize > ssize_t(sizeof(info)))
92 dataSize = sizeof(info);
94 memcpy(reinterpret_cast<void *>(&info), data, dataSize);
96 // attempt to instantiate
97 BMediaRoster* r = BMediaRoster::Roster();
98 media_node node;
99 err = r->InstantiateDormantNode(info, &node);
101 // if(err == B_OK)
102 // // reference it
103 // err = r->GetNodeFor(node.node, &node);
105 // send status
106 if (err == B_OK) {
107 BMessage m(M_INSTANTIATE_COMPLETE);
108 m.AddInt32("node_id", node.node);
109 message->SendReply(&m);
111 else {
112 BMessage m(M_INSTANTIATE_FAILED);
113 m.AddInt32("error", err);
114 message->SendReply(&m);
117 break;
120 case M_RELEASE:
122 // fetch node info
123 live_node_info info;
124 const void *data;
125 ssize_t dataSize;
126 err = message->FindData("info", B_RAW_TYPE, &data, &dataSize);
127 if (err < B_OK) {
128 PRINT((
129 "!!! App::MessageReceived(M_RELEASE):\n"
130 " missing 'info'\n"));
131 break;
133 if (dataSize != sizeof(info)) {
134 PRINT((
135 "* App::MessageReceived(M_RELEASE):\n"
136 " warning: 'info' size mismatch\n"));
137 if(dataSize > ssize_t(sizeof(info)))
138 dataSize = sizeof(info);
140 memcpy(reinterpret_cast<void *>(&info), data, dataSize);
142 // attempt to release
143 BMediaRoster* r = BMediaRoster::Roster();
144 media_node node;
145 err = r->ReleaseNode(info.node);
147 // send status
148 if (err == B_OK) {
149 BMessage m(M_RELEASE_COMPLETE);
150 m.AddInt32("node_id", info.node.node);
151 message->SendReply(&m);
153 else {
154 BMessage m(M_RELEASE_FAILED);
155 m.AddInt32("error", err);
156 message->SendReply(&m);
159 break;
162 default:
163 _inherited::MessageReceived(message);
169 main(int argc, char** argv)
171 App app;
172 if (argc < 2 || strcmp(argv[1], "--addon-host") != 0) {
173 BAlert* alert = new BAlert("Cortex AddOnHost",
174 "This program runs in the background, and is started automatically "
175 "by Cortex when necessary. You probably don't want to start it manually.",
176 "Continue", "Quit");
177 alert->SetShortcut(1, B_ESCAPE);
178 int32 response = alert->Go();
180 if(response == 1)
181 return 0;
183 app.Run();
184 return 0;