3 * kkiproc inter-process communications related routines
4 * $Id: kkiproc.cc,v 1.11 2003/04/02 09:54:47 konst Exp $
6 * Copyright (C) 1999-2001 by Konstantin Klyagin <k@thekonst.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/ioctl.h>
33 #include <sys/termio.h>
34 #include <sys/filio.h>
38 #include <sys/socket.h>
42 /* utmp functionallity is only used for gnu/linux.
50 void detach(char *logfile
) {
51 if(logfile
) freopen(logfile
, "w", stdout
);
61 time_t lastkeypress() {
71 if((tlen
= readlink("/proc/self/fd/0", tname
, 12)) != -1) {
74 if(!strncmp(tname
, "/dev/tty", 8) && isdigit(tname
[8])) {
77 while((u
= getutent())) {
81 if(strlen(u
->ut_line
) > 3)
82 if(!strncmp(u
->ut_line
, "tty", 3))
83 if(isdigit(u
->ut_line
[3])) {
85 ss
<< "/proc/" << u
->ut_pid
;
87 if(*u
->ut_user
&& !access(ss
.str().c_str(), F_OK
)) {
89 ss
<< "/dev/" << u
->ut_line
;
90 if(!stat(ss
.str().c_str(), &s
))
101 if(!stat(tname
, &s
)) {
115 if((p
= ttyname(0)) != NULL
) {
116 if(!stat(p
, &s
) && s
.st_atime
> t
) t
= s
.st_atime
; else time(&t
);
126 int dataready(int fd
, int dowait
) {
131 tv
.tv_sec
= tv
.tv_usec
= 0;
136 if(select(fd
+1, &fds
, 0, 0, dowait
? 0 : &tv
) != -1) {
137 if(rc
= FD_ISSET(fd
, &fds
)) {
138 ioctl(fd
, FIONREAD
, &rc
);
147 char *getprocentry(char *fname
) {
148 FILE *f
= fopen(fname
, "r");
150 int fsize
= kfilesize(fname
);
153 p
= (char *) realloc(p
, fsize
+1);
154 fread(p
, fsize
, 1, f
);
163 char *gethostname() {
164 return getprocentry("/proc/kernel/hostname");
167 char *getdomainname() {
168 return getprocentry("/proc/kernel/domainname");
173 void setenv(const string
&name
, const string
&value
, int replace
) {
174 if(getenv(name
.c_str()) && replace
) {
175 auto_ptr
<char> stuff(new char[name
.size() + value
.size() + 2]);
176 sprintf(stuff
.get(), "%s=%s", name
.c_str(), value
.c_str());