diff options
author | Landry Breuil <landry@rhaalovely.net> | 2012-01-04 11:27:15 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2012-01-06 13:40:50 +0000 |
commit | 60e474ead5db1d8be3ee542caf40fb08650353b1 (patch) | |
tree | 300b096a771c390cf5f197a841fc2f6049180b9b /src/openbsd/up-native.c | |
parent | 6c15694dbbbfa82b3dc30338fa56079921576ab1 (diff) | |
download | upower-60e474ead5db1d8be3ee542caf40fb08650353b1.tar.gz |
openbsd: use a singleton pattern to access /dev/apm
up_apm_get_fd() opens /dev/apm only if it's not already opened.
Signed-off-by: Richard Hughes <richard@hughsie.com>
Diffstat (limited to 'src/openbsd/up-native.c')
-rw-r--r-- | src/openbsd/up-native.c | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/openbsd/up-native.c b/src/openbsd/up-native.c index b908c55..020cb6e 100644 --- a/src/openbsd/up-native.c +++ b/src/openbsd/up-native.c @@ -53,6 +53,21 @@ up_apm_native_get_path(UpApmNative * native) return native->path; } +int +up_apm_get_fd() +{ + static int apm_fd = 0; + if (apm_fd == 0) { + g_debug("apm_fd is not initialized yet, opening"); + /* open /dev/apm */ + if ((apm_fd = open("/dev/apm", O_RDONLY)) == -1) { + if (errno != ENXIO && errno != ENOENT) + g_error("cannot open device file"); + } + } + return apm_fd; +} + /** * up_native_get_native_path: * @object: the native tracking object @@ -74,20 +89,14 @@ up_native_get_native_path (GObject *object) gboolean up_native_is_laptop() { - int apm_fd; struct apm_power_info bstate; struct sensordev acpiac; if (up_native_get_sensordev("acpiac0", &acpiac)) return TRUE; - if ((apm_fd = open("/dev/apm", O_RDONLY)) == -1) { - if (errno != ENXIO && errno != ENOENT) - g_error("cannot open device file"); - } - if (-1 == ioctl(apm_fd, APM_IOC_GETPOWER, &bstate)) - g_error("ioctl on fd %d failed : %s", apm_fd, g_strerror(errno)); - close(apm_fd); + if (-1 == ioctl(up_apm_get_fd(), APM_IOC_GETPOWER, &bstate)) + g_error("ioctl on apm fd failed : %s", g_strerror(errno)); return bstate.ac_state != APM_AC_UNKNOWN; } |