| 184 | } |
| 185 | |
| 186 | int main(int argc, char** argv) |
| 187 | { |
| 188 | int serverfd; |
| 189 | |
| 190 | if (argc < 2) { |
| 191 | std::cerr << "need arguments:" << std::endl; |
| 192 | std::cerr << |
| 193 | "[port] - Open a server on this TCP port and forward connections to the local DBUS session" |
| 194 | " (the DBUS_SESSION_BUS_ADDRESS environment variable must be set)"; |
| 195 | std::cerr << |
| 196 | "[port] [fake dbus path] - Open a server on the fake dbus path and forward connections to the given local TCP port"; |
| 197 | std::cerr << "" |
| 198 | "The last argument may be the --bind-only option, in which case the application only tries to" |
| 199 | "open the server, but does not wait for clients to connect. This is useful to test whether the" |
| 200 | "server port/path is available."; |
| 201 | return 10; |
| 202 | } |
| 203 | |
| 204 | bool waitForClients = true; |
| 205 | |
| 206 | if (std::string(argv[argc - 1]) == "--bind-only") { |
| 207 | waitForClients = false; |
| 208 | argc -= 1; |
| 209 | } |
| 210 | |
| 211 | std::string dbusAddress(getenv("DBUS_SESSION_BUS_ADDRESS")); |
| 212 | |
| 213 | std::string path; |
| 214 | |
| 215 | if (argc == 2) { |
| 216 | if (waitForClients && debug) |
| 217 | std::cout << "forwarding from the local TCP port " << argv[1] << " to the local DBUS session at " << |
| 218 | dbusAddress.data() << std::endl; |
| 219 | |
| 220 | if (dbusAddress.empty()) { |
| 221 | std::cerr << "The DBUS_SESSION_BUS_ADDRESS environment variable is not set" << std::endl; |
| 222 | return 1; |
| 223 | } |
| 224 | |
| 225 | // Open a TCP server |
| 226 | |
| 227 | std::string abstractPrefix("unix:abstract="); |
| 228 | |
| 229 | if (dbusAddress.substr(0, abstractPrefix.size()) != abstractPrefix) { |
| 230 | std::cerr << "DBUS_SESSION_BUS_ADDRESS does not seem to use an abstract unix domain socket as expected" << |
| 231 | std::endl; |
| 232 | return 2; |
| 233 | } |
| 234 | |
| 235 | path = dbusAddress.substr(abstractPrefix.size(), dbusAddress.size() - abstractPrefix.size()); |
| 236 | if (path.find(",guid=") != std::string::npos) |
| 237 | path = path.substr(0, path.find(",guid=")); |
| 238 | |
| 239 | // Mark it as an abstract unix domain socket |
| 240 | serverfd = socket(AF_INET, SOCK_STREAM, 0); |
| 241 | |
| 242 | if (serverfd < 0) { |
| 243 | if (waitForClients) |