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 | |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 12 | #include "shill/logging.h" |
| 13 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 14 | using base::Callback; |
| 15 | |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 16 | namespace shill { |
| 17 | |
| 18 | static gboolean DispatchIOHandler(GIOChannel *chan, |
| 19 | GIOCondition cond, |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 20 | gpointer data) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 21 | GlibIOInputHandler *handler = reinterpret_cast<GlibIOInputHandler *>(data); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 22 | unsigned char buf[4096]; |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 23 | gsize len = 0; |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 24 | gint fd = g_io_channel_unix_get_fd(chan); |
| 25 | GError *err = 0; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 26 | |
| 27 | if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 28 | LOG(FATAL) << "Unexpected GLib error condition " << cond << " on poll(" |
| 29 | << fd << "): " << strerror(errno); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 30 | |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 31 | GIOStatus status = g_io_channel_read_chars( |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 32 | chan, reinterpret_cast<gchar *>(buf), sizeof(buf), &len, &err); |
| 33 | if (err) { |
| 34 | LOG(WARNING) << "GLib error code " << err->domain << "/" << err->code |
| 35 | << " (" << err->message << ") on read(" << fd << "):" |
| 36 | << strerror(errno); |
| 37 | g_error_free(err); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 38 | } |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 39 | if (status == G_IO_STATUS_AGAIN) |
| 40 | return TRUE; |
| 41 | if (status != G_IO_STATUS_NORMAL) |
| 42 | LOG(FATAL) << "Unexpected GLib return status " << status; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 43 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 44 | InputData input_data(buf, len); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 45 | handler->callback().Run(&input_data); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 46 | |
Julius Werner | 22e244a | 2012-11-13 18:25:19 -0800 | [diff] [blame] | 47 | return TRUE; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 50 | GlibIOInputHandler::GlibIOInputHandler( |
| 51 | int fd, const Callback<void(InputData *)> &callback) |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 52 | : channel_(g_io_channel_unix_new(fd)), |
| 53 | callback_(callback), |
| 54 | source_id_(G_MAXUINT) { |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 55 | // To avoid blocking in g_io_channel_read_chars() due to its internal buffer, |
| 56 | // set the channel to unbuffered, which in turns requires encoding to be NULL. |
| 57 | // This assumes raw binary data are read from |fd| via the channel. |
| 58 | CHECK_EQ(G_IO_STATUS_NORMAL, g_io_channel_set_encoding(channel_, NULL, NULL)); |
| 59 | g_io_channel_set_buffered(channel_, FALSE); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 60 | g_io_channel_set_close_on_unref(channel_, TRUE); |
| 61 | } |
| 62 | |
| 63 | GlibIOInputHandler::~GlibIOInputHandler() { |
| 64 | g_source_remove(source_id_); |
| 65 | g_io_channel_shutdown(channel_, TRUE, NULL); |
| 66 | g_io_channel_unref(channel_); |
| 67 | } |
| 68 | |
| 69 | void GlibIOInputHandler::Start() { |
| 70 | if (source_id_ == G_MAXUINT) { |
| 71 | source_id_ = g_io_add_watch(channel_, |
| 72 | static_cast<GIOCondition>( |
| 73 | G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 74 | DispatchIOHandler, this); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | void GlibIOInputHandler::Stop() { |
| 79 | if (source_id_ != G_MAXUINT) { |
| 80 | g_source_remove(source_id_); |
| 81 | source_id_ = G_MAXUINT; |
| 82 | } |
| 83 | } |
| 84 | |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 85 | } // namespace shill |