Replace unnecessary open(2)/close(2) with stat(2) and do not sort result

of glob(3) when counting /dev/ttyv*.

Reviewed by:	marcus
This commit is contained in:
Jung-uk Kim 2009-02-02 22:31:54 +00:00
parent 58fe8a1226
commit dc2795b91d
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=227470
2 changed files with 16 additions and 19 deletions

View file

@ -7,7 +7,7 @@
PORTNAME= consolekit PORTNAME= consolekit
PORTVERSION= 0.3.0 PORTVERSION= 0.3.0
PORTREVISION= 3 PORTREVISION= 4
CATEGORIES= sysutils gnome CATEGORIES= sysutils gnome
MASTER_SITES= http://people.freedesktop.org/~mccann/dist/ MASTER_SITES= http://people.freedesktop.org/~mccann/dist/
DISTFILES= ConsoleKit-${PORTVERSION}${EXTRACT_SUFX} DISTFILES= ConsoleKit-${PORTVERSION}${EXTRACT_SUFX}

View file

@ -1,5 +1,5 @@
--- src/ck-sysdeps-freebsd.c.orig 2008-01-23 09:30:44.000000000 -0500 --- src/ck-sysdeps-freebsd.c.orig 2008-04-03 20:36:21.000000000 -0400
+++ src/ck-sysdeps-freebsd.c 2008-02-06 22:50:57.000000000 -0500 +++ src/ck-sysdeps-freebsd.c 2009-01-30 17:03:00.000000000 -0500
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
@ -24,7 +24,7 @@
*stat = NULL; *stat = NULL;
} }
@@ -318,38 +317,40 @@ gboolean @@ -327,38 +326,38 @@ gboolean
ck_get_max_num_consoles (guint *num) ck_get_max_num_consoles (guint *num)
{ {
int max_consoles; int max_consoles;
@ -41,19 +41,16 @@
- if (res == 0) { - if (res == 0) {
- goto done; - goto done;
- } - }
- + g.gl_offs = 0;
+ glob ("/dev/ttyv*", GLOB_DOOFFS | GLOB_NOSORT, NULL, &g);
+ for (i = 0; i < g.gl_pathc && g.gl_pathv[i] != NULL; i++) {
+ struct stat sb;
+ char *cdev;
- while ((t = getttyent ()) != NULL) { - while ((t = getttyent ()) != NULL) {
- if (t->ty_status & TTY_ON && strncmp (t->ty_name, "ttyv", 4) == 0) - if (t->ty_status & TTY_ON && strncmp (t->ty_name, "ttyv", 4) == 0)
+ g.gl_offs = 0;
+ glob ("/dev/ttyv*", GLOB_DOOFFS, NULL, &g);
+ for (i = 0; i < g.gl_pathc && g.gl_pathv[i] != NULL; i++) {
+ int fd;
+ char *cdev;
+
+ cdev = g.gl_pathv[i]; + cdev = g.gl_pathv[i];
+ fd = open (cdev, O_RDONLY | O_NOCTTY); + if (stat (cdev, &sb) > -1 && S_ISCHR (sb.st_mode)) {
+ if (fd > -1) {
+ close (fd);
max_consoles++; max_consoles++;
+ } else { + } else {
+ break; + break;
@ -83,7 +80,7 @@
} }
char * char *
@@ -360,7 +361,12 @@ ck_get_console_device_for_num (guint num @@ -369,7 +368,12 @@ ck_get_console_device_for_num (guint num
/* The device number is always one less than the VT number. */ /* The device number is always one less than the VT number. */
num--; num--;
@ -97,7 +94,7 @@
return device; return device;
} }
@@ -370,6 +376,7 @@ ck_get_console_num_from_device (const ch @@ -379,6 +383,7 @@ ck_get_console_num_from_device (const ch
guint *num) guint *num)
{ {
guint n; guint n;
@ -105,7 +102,7 @@
gboolean ret; gboolean ret;
n = 0; n = 0;
@@ -379,7 +386,11 @@ ck_get_console_num_from_device (const ch @@ -388,7 +393,11 @@ ck_get_console_num_from_device (const ch
return FALSE; return FALSE;
} }
@ -118,7 +115,7 @@
/* The VT number is always one more than the device number. */ /* The VT number is always one more than the device number. */
n++; n++;
ret = TRUE; ret = TRUE;
@@ -399,6 +410,7 @@ ck_get_active_console_num (int consol @@ -408,6 +417,7 @@ ck_get_active_console_num (int consol
gboolean ret; gboolean ret;
int res; int res;
int active; int active;
@ -126,7 +123,7 @@
g_assert (console_fd != -1); g_assert (console_fd != -1);
@@ -411,7 +423,12 @@ ck_get_active_console_num (int consol @@ -420,7 +430,12 @@ ck_get_active_console_num (int consol
goto out; goto out;
} }