1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <utmp.h>
#ifdef NOUTFUNCS
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef BSD
#define _NO_UT_ID
#define _NO_UT_TYPE
#define _NO_UT_PID
#define ut_user ut_name
#endif
/*
define these so it still works as documented :)
*/
#ifndef USER_PROCESS
#define EMPTY 0 /* No valid user accounting information. */
#define RUN_LVL 1 /* The system's runlevel. */
#define BOOT_TIME 2 /* Time of system boot. */
#define NEW_TIME 3 /* Time after system clock changed. */
#define OLD_TIME 4 /* Time when system clock changed. */
#define INIT_PROCESS 5 /* Process spawned by the init process. */
#define LOGIN_PROCESS 6 /* Session leader of a logged in user. */
#define USER_PROCESS 7 /* Normal process. */
#define DEAD_PROCESS 8 /* Terminated process. */
#define ACCOUNTING 9
#endif
/*
It is almost certain that if these are not defined the fields they are
for are not present or this is BSD :)
*/
#ifndef UT_LINESIZE
# define UT_LINESIZE 32
#endif
#ifndef UT_NAMESIZE
# define UT_NAMESIZE 32
#endif
#ifndef UT_HOSTSIZE
# define UT_HOSTSIZE
#endif
static int ut_fd = -1;
static char _ut_name[] = _PATH_UTMP;
void utmpname(char *filename)
{
strcpy(_ut_name, filename);
}
void setutent(void)
{
if (ut_fd < 0)
{
if ((ut_fd = open(_ut_name, O_RDONLY)) < 0)
{
croak("Can't open %s",_ut_name);
}
}
lseek(ut_fd, (off_t) 0, SEEK_SET);
}
void endutent(void)
{
if (ut_fd > 0)
{
close(ut_fd);
}
ut_fd = -1;
}
struct utmp *getutent(void)
{
static struct utmp s_utmp;
int readval;
if (ut_fd < 0)
{
setutent();
}
if ((readval = read(ut_fd, &s_utmp, sizeof(s_utmp))) < sizeof(s_utmp))
{
if (readval == 0)
{
return NULL;
}
else if (readval < 0)
{
croak("Error reading %s", _ut_name);
}
else
{
croak("Partial record in %s [%d bytes]", _ut_name, readval );
}
}
return &s_utmp;
}
#endif
static double
constant(char *name, int len, int arg)
{
errno = 0;
if (strEQ(name, "ACCOUNTING"))
{
return ACCOUNTING;
}
else if (strEQ(name, "BOOT_TIME"))
{
return BOOT_TIME;
}
else if (strEQ(name, "DEAD_PROCESS"))
{
return DEAD_PROCESS;
}
else if (strEQ(name, "EMPTY"))
{
return EMPTY;
}
else if (strEQ(name, "INIT_PROCESS"))
{
return INIT_PROCESS;
}
else if (strEQ(name, "LOGIN_PROCESS"))
{
return LOGIN_PROCESS;
}
else if (strEQ(name, "NEW_TIME"))
{
return NEW_TIME;
}
else if (strEQ(name, "OLD_TIME"))
{
return OLD_TIME;
}
else if (strEQ(name, "RUN_LVL"))
{
return RUN_LVL;
}
if (strEQ(name, "USER_PROCESS"))
{
return USER_PROCESS;
}
else
{
errno = EINVAL;
return 0;
}
}
MODULE = Sys::Utmp PACKAGE = Sys::Utmp
PROTOTYPES: DISABLE
double
constant(sv,arg)
PREINIT:
STRLEN len;
INPUT:
SV * sv
char * s = SvPV(sv, len);
int arg
CODE:
RETVAL = constant(s,len,arg);
OUTPUT:
RETVAL
void
getutent(self)
SV *self
PPCODE:
static AV *ut;
static HV *meth_stash;
static IV ut_tv;
static IV _ut_pid;
static IV _ut_type;
static SV *ut_ref;
static char *_ut_id;
static struct utmp *utent;
static char ut_host[UT_HOSTSIZE];
HV *self_hash;
SV *sv_ut_user;
SV *sv_ut_id;
SV *sv_ut_line;
SV *sv_ut_pid;
SV *sv_ut_type;
SV *sv_ut_host;
SV *sv_ut_tv;
if(!SvROK(self))
croak("Must be called as an object method");
self_hash = (HV *)SvRV(self);
utent = getutent();
if ( utent )
{
#ifdef _NO_UT_ID
_ut_id = "";
#else
_ut_id = utent->ut_id;
#endif
#ifdef _NO_UT_TYPE
_ut_type = 7;
#else
_ut_type = utent->ut_type;
#endif
#ifdef _NO_UT_PID
_ut_pid = -1;
#else
_ut_pid = utent->ut_pid;
#endif
#ifdef _HAVE_UT_TV
ut_tv = (IV)utent->ut_tv.tv_sec;
#else
ut_tv = (IV)utent->ut_time;
#endif
#ifdef _HAVE_UT_HOST
strncpy(ut_host, utent->ut_host,UT_HOSTSIZE);
#else
strcpy(ut_host, "",1);
#endif
sv_ut_user = newSVpv(utent->ut_user,0);
sv_ut_id = newSVpv(_ut_id,0);
sv_ut_line = newSVpv(utent->ut_line,0);
sv_ut_pid = newSViv(_ut_pid);
sv_ut_type = newSViv(_ut_type);
sv_ut_host = newSVpv(ut_host,0);
sv_ut_tv = newSViv(ut_tv);
SvTAINTED_on(sv_ut_user);
SvTAINTED_on(sv_ut_host);
if ( GIMME_V == G_ARRAY )
{
sv_ut_user = sv_2mortal(sv_ut_user);
sv_ut_id = sv_2mortal(sv_ut_id);
sv_ut_line = sv_2mortal(sv_ut_line);
sv_ut_pid = sv_2mortal(sv_ut_pid);
sv_ut_type = sv_2mortal(sv_ut_type);
sv_ut_host = sv_2mortal(sv_ut_host);
sv_ut_tv = sv_2mortal(sv_ut_tv);
XPUSHs(sv_ut_user);
XPUSHs(sv_ut_id);
XPUSHs(sv_ut_line);
XPUSHs(sv_ut_pid);
XPUSHs(sv_ut_type);
XPUSHs(sv_ut_host);
XPUSHs(sv_ut_tv);
}
else if ( GIMME_V == G_SCALAR )
{
ut = newAV();
av_push(ut,sv_ut_user);
av_push(ut,sv_ut_id);
av_push(ut,sv_ut_line);
av_push(ut,sv_ut_pid);
av_push(ut,sv_ut_type);
av_push(ut,sv_ut_host);
av_push(ut,sv_ut_tv);
meth_stash = gv_stashpv("Sys::Utmp::Utent",1);
ut_ref = newRV_noinc((SV *)ut);
sv_bless(ut_ref, meth_stash);
XPUSHs(sv_2mortal(ut_ref));
}
else
{
XSRETURN_EMPTY;
}
}
else
{
XSRETURN_EMPTY;
}
void
setutent(self)
SV *self
PPCODE:
HV *self_hash;
if(!SvROK(self))
croak("Must be called as an object method");
self_hash = (HV *)SvRV(self);
setutent();
void
endutent(self)
SV *self
PPCODE:
HV *self_hash;
if(!SvROK(self))
croak("Must be called as an object method");
self_hash = (HV *)SvRV(self);
endutent();
void
utmpname(self, filename)
SV *self
SV *filename
PPCODE:
char *ff;
HV *self_hash;
if(!SvROK(self))
croak("Must be called as an object method");
self_hash = (HV *)SvRV(self);
ff = SvPV(filename,PL_na);
utmpname(ff);
void
DESTROY(self)
SV *self
PPCODE:
HV *self_hash;
if(!SvROK(self))
croak("Must be called as an object method");
self_hash = (HV *)SvRV(self);
endutent();
|