Fix for CVE-2009-3720 in libjabber's xml parser
[centerim.git] / kksystr / src / kkiproc.cc
blob89a0d0cb883a3b1a616dc6405eedd1b889e5b15f
1 /*
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
21 * USA
25 #include "kkiproc.h"
27 #include <sys/ioctl.h>
28 #include <signal.h>
29 #include <memory>
30 #include <sstream>
32 #ifdef __sun__
33 #include <sys/termio.h>
34 #include <sys/filio.h>
35 #endif
37 #ifdef __CYGWIN__
38 #include <sys/socket.h>
39 #endif
41 #ifdef __linux__
42 /* utmp functionallity is only used for gnu/linux.
43 * Anyone know why ?
45 #include <utmp.h>
46 #endif
48 using namespace std;
50 void detach(char *logfile) {
51 if(logfile) freopen(logfile, "w", stdout);
53 if(!fork()) {
54 setsid();
55 chdir("/");
56 } else {
57 _exit(0);
61 time_t lastkeypress() {
62 time_t t = 0;
63 struct stat s;
65 #ifdef __linux__
67 struct utmp *u;
68 char tname[32];
69 int tlen;
71 if((tlen = readlink("/proc/self/fd/0", tname, 12)) != -1) {
72 tname[tlen] = 0;
74 if(!strncmp(tname, "/dev/tty", 8) && isdigit(tname[8])) {
75 setutent();
77 while((u = getutent())) {
78 switch(u->ut_type) {
79 case USER_PROCESS:
80 case LOGIN_PROCESS:
81 if(strlen(u->ut_line) > 3)
82 if(!strncmp(u->ut_line, "tty", 3))
83 if(isdigit(u->ut_line[3])) {
84 stringstream ss;
85 ss << "/proc/" << u->ut_pid;
87 if(*u->ut_user && !access(ss.str().c_str(), F_OK)) {
88 stringstream ss;
89 ss << "/dev/" << u->ut_line;
90 if(!stat(ss.str().c_str(), &s))
91 if(s.st_atime > t)
92 t = s.st_atime;
95 break;
99 endutent();
100 } else {
101 if(!stat(tname, &s)) {
102 t = s.st_atime;
103 } else {
104 time(&t);
107 } else {
108 time(&t);
111 #else
113 char *p;
115 if((p = ttyname(0)) != NULL) {
116 if(!stat(p, &s) && s.st_atime > t) t = s.st_atime; else time(&t);
117 } else {
118 time(&t);
121 #endif
123 return t;
126 int dataready(int fd, int dowait) {
127 struct timeval tv;
128 fd_set fds;
129 int rc;
131 tv.tv_sec = tv.tv_usec = 0;
133 FD_ZERO(&fds);
134 FD_SET(fd, &fds);
136 if(select(fd+1, &fds, 0, 0, dowait ? 0 : &tv) != -1) {
137 if(rc = FD_ISSET(fd, &fds)) {
138 ioctl(fd, FIONREAD, &rc);
140 } else {
141 rc = 0;
144 return rc;
147 char *getprocentry(char *fname) {
148 FILE *f = fopen(fname, "r");
149 static char *p = 0;
150 int fsize = kfilesize(fname);
152 if(f) {
153 p = (char *) realloc(p, fsize+1);
154 fread(p, fsize, 1, f);
155 fclose(f);
156 } else {
157 p = 0;
160 return p;
163 char *gethostname() {
164 return getprocentry("/proc/kernel/hostname");
167 char *getdomainname() {
168 return getprocentry("/proc/kernel/domainname");
171 #ifdef __sun__
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());
177 putenv(stuff.get());
181 #endif