Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 5 | #include "shill/glib_io_input_handler.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 6 | |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 7 | #include <errno.h> |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 9 | #include <string.h> |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 10 | #include <glib.h> |
| 11 | |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <base/string_util.h> |
| 16 | #include <base/stringprintf.h> |
| 17 | |
| 18 | #include "shill/error.h" |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 19 | #include "shill/logging.h" |
| 20 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 21 | using base::Callback; |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 22 | using base::StringPrintf; |
| 23 | using std::string; |
| 24 | using std::vector; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 25 | |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 26 | namespace shill { |
| 27 | |
| 28 | static gboolean DispatchIOHandler(GIOChannel *chan, |
| 29 | GIOCondition cond, |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 30 | gpointer data) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 31 | GlibIOInputHandler *handler = reinterpret_cast<GlibIOInputHandler *>(data); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 32 | unsigned char buf[4096]; |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 33 | gsize len = 0; |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 34 | gint fd = g_io_channel_unix_get_fd(chan); |
| 35 | GError *err = 0; |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 36 | vector<string> error_conditions; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 37 | |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 38 | if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) { |
| 39 | string condition = base::StringPrintf( |
| 40 | "Unexpected GLib error condition %#x on poll(%d): %s", |
| 41 | cond, fd, strerror(errno)); |
| 42 | LOG(WARNING) << condition; |
| 43 | error_conditions.push_back(condition); |
| 44 | } |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 45 | |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 46 | GIOStatus status = g_io_channel_read_chars( |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 47 | chan, reinterpret_cast<gchar *>(buf), sizeof(buf), &len, &err); |
| 48 | if (err) { |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 49 | string condition = base::StringPrintf( |
| 50 | "GLib error code %d/%d (%s) on read(%d): %s", |
| 51 | err->domain, err->code, err->message, fd, strerror(errno)); |
| 52 | LOG(WARNING) << condition; |
| 53 | error_conditions.push_back(condition); |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 54 | g_error_free(err); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 55 | } |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 56 | if (status == G_IO_STATUS_AGAIN) |
| 57 | return TRUE; |
Julius Werner | d69c958 | 2012-12-27 14:42:56 -0800 | [diff] [blame] | 58 | if (status == G_IO_STATUS_ERROR) { |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 59 | string condition = base::StringPrintf( |
| 60 | "Unexpected GLib return status: %d", status); |
| 61 | LOG(ERROR) << condition; |
| 62 | error_conditions.push_back(condition); |
| 63 | Error error(Error::kOperationFailed, JoinString(error_conditions, ';')); |
| 64 | handler->error_callback().Run(error); |
| 65 | return FALSE; |
| 66 | } |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 67 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 68 | InputData input_data(buf, len); |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 69 | handler->input_callback().Run(&input_data); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 70 | |
Julius Werner | d69c958 | 2012-12-27 14:42:56 -0800 | [diff] [blame] | 71 | if (status == G_IO_STATUS_EOF) { |
| 72 | LOG(INFO) << "InputHandler on fd " << fd << " closing due to EOF."; |
| 73 | CHECK(len == 0); |
| 74 | return FALSE; |
| 75 | } |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 76 | return TRUE; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 79 | GlibIOInputHandler::GlibIOInputHandler( |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 80 | int fd, |
| 81 | const InputCallback &input_callback, |
| 82 | const ErrorCallback &error_callback) |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 83 | : channel_(g_io_channel_unix_new(fd)), |
Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 84 | input_callback_(input_callback), |
| 85 | error_callback_(error_callback), |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 86 | source_id_(G_MAXUINT) { |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 87 | // To avoid blocking in g_io_channel_read_chars() due to its internal buffer, |
| 88 | // set the channel to unbuffered, which in turns requires encoding to be NULL. |
| 89 | // This assumes raw binary data are read from |fd| via the channel. |
| 90 | CHECK_EQ(G_IO_STATUS_NORMAL, g_io_channel_set_encoding(channel_, NULL, NULL)); |
| 91 | g_io_channel_set_buffered(channel_, FALSE); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 92 | g_io_channel_set_close_on_unref(channel_, TRUE); |
| 93 | } |
| 94 | |
| 95 | GlibIOInputHandler::~GlibIOInputHandler() { |
| 96 | g_source_remove(source_id_); |
| 97 | g_io_channel_shutdown(channel_, TRUE, NULL); |
| 98 | g_io_channel_unref(channel_); |
| 99 | } |
| 100 | |
| 101 | void GlibIOInputHandler::Start() { |
| 102 | if (source_id_ == G_MAXUINT) { |
| 103 | source_id_ = g_io_add_watch(channel_, |
| 104 | static_cast<GIOCondition>( |
| 105 | G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 106 | DispatchIOHandler, this); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | void GlibIOInputHandler::Stop() { |
| 111 | if (source_id_ != G_MAXUINT) { |
| 112 | g_source_remove(source_id_); |
| 113 | source_id_ = G_MAXUINT; |
| 114 | } |
| 115 | } |
| 116 | |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 117 | } // namespace shill |