Class: Sys::Admin
- Inherits:
-
Object
- Object
- Sys::Admin
- Defined in:
- ext/sys/admin.c
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
The version of this library
1.5.2
Class Method Summary collapse
-
.get_group(v_value) ⇒ Object
Returns a Group object for the given
nameorgid. -
.Sys::Admin.get_login ⇒ Object
Returns the login for the process.
-
.get_user(v_value) ⇒ Object
Returns a User object for the given
nameoruid. -
.groups ⇒ Object
In block form, yields a Group object for each group on the system.
-
.users ⇒ Object
In block form, yields a User object for each user on the system.
Class Method Details
.get_group(name) ⇒ Object .get_group(gid) ⇒ Object
Returns a Group object for the given name or gid. Raises an Admin::Error if a group cannot be found for that name or GID.
142 143 144 145 146 147 148 149 150 151 |
# File 'ext/sys/admin.c', line 142
static VALUE admin_get_group(VALUE klass, VALUE v_value){
VALUE v_group;
if(FIXNUM_P(v_value))
v_group = get_group_by_num(v_value);
else
v_group = get_group_by_name(v_value);
return v_group;
}
|
.Sys::Admin.get_login ⇒ Object
Returns the login for the process. If this is called from a process that has no controlling terminal, then it resorts to returning the “LOGNAME” or “USER” environment variable. If neither of those is defined, then nil is returned.
Note that this method will probably return the real user login, but may return the effective user login. YMMV depending on your platform and how the program is run.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'ext/sys/admin.c', line 61
static VALUE admin_get_login(VALUE klass){
#ifdef HAVE_GETLOGIN_R
char login[_POSIX_LOGIN_NAME_MAX];
if(!getlogin_r(login, _POSIX_LOGIN_NAME_MAX))
return rb_str_new2(login);
#elif HAVE_GETLOGIN
char* login = getlogin();
if(login)
return rb_str_new2(login);
#endif
#ifdef HAVE_GETPWUID_R
uid_t uid;
char buf[USER_BUF_SIZE];
struct passwd pwd;
struct passwd* pwdbuf;
uid = getuid();
if(getpwuid_r(uid, &pwd, buf, USER_BUF_SIZE, &pwdbuf) != 0)
return rb_str_new2(pwdbuf->pw_name);
#elif HAVE_GETPWUID
uid_t uid;
struct passwd* pwd;
uid = getuid();
if((pwd = getpwuid(uid)))
return rb_str_new2(pwd->pw_name);
#endif
#ifdef HAVE_GETENV
char* user = getenv("LOGNAME");
if(user){
return rb_str_new2(user);
}
else{
user = getenv("USER");
if(user)
return rb_str_new2(user);
}
#endif
return Qnil;
}
|
.get_user(name) ⇒ Object .get_user(uid) ⇒ Object
Returns a User object for the given name or uid. Raises an Admin::Error if a user cannot be found for that name or user ID.
117 118 119 120 121 122 123 124 125 126 |
# File 'ext/sys/admin.c', line 117
static VALUE admin_get_user(VALUE klass, VALUE v_value){
VALUE v_user;
if(FIXNUM_P(v_value))
v_user = get_user_by_num(v_value);
else
v_user = get_user_by_name(v_value);
return v_user;
}
|
.groups ⇒ Object .groups {|group| ... } ⇒ Object
In block form, yields a Group object for each group on the system. In non-block form, returns an Array of Group objects.
212 213 214 215 216 |
# File 'ext/sys/admin.c', line 212 static VALUE admin_groups(VALUE klass){ return rb_ensure(admin_groups_body, rb_ary_new3(1, klass), admin_groups_cleanup, Qnil ); } |
.users ⇒ Object .users {|user| ... } ⇒ Object
In block form, yields a User object for each user on the system. In non-block form, returns an Array of User objects.
280 281 282 283 284 |
# File 'ext/sys/admin.c', line 280 static VALUE admin_users(VALUE klass){ return rb_ensure(admin_users_body, rb_ary_new3(1, klass), admin_users_cleanup, Qnil ); } |