ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/logging.C
(Generate patch)

Comparing rxvt-unicode/src/logging.C (file contents):
Revision 1.42 by ayin, Mon Nov 5 00:50:06 2007 UTC vs.
Revision 1.46 by root, Mon May 5 16:51:52 2008 UTC

43 43
44#include <cstdio> 44#include <cstdio>
45 45
46#include <sys/types.h> 46#include <sys/types.h>
47#include <sys/stat.h> 47#include <sys/stat.h>
48#include <sys/fcntl.h> 48#include <fcntl.h>
49#include <unistd.h> 49#include <unistd.h>
50#include <time.h> 50#include <time.h>
51#include <errno.h>
51 52
52/* 53/*
53 * BSD style utmp entry 54 * BSD style utmp entry
54 * ut_line, ut_name, ut_host, ut_time 55 * ut_line, ut_name, ut_host, ut_time
55 * SYSV style utmp (and utmpx) entry 56 * SYSV style utmp (and utmpx) entry
104 break; 105 break;
105 } 106 }
106 else if (errno != EAGAIN && errno != EACCES) 107 else if (errno != EAGAIN && errno != EACCES)
107 break; 108 break;
108 if (!gotlock) 109 if (!gotlock)
110 {
109 { /* give it up */ 111 /* give it up */
110 close (fd); 112 close (fd);
111 return; 113 return;
112 } 114 }
113 if (fstat (fd, &sbuf) == 0) 115 if (fstat (fd, &sbuf) == 0)
114 if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp)) 116 if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp))
160 if (stat (fname, &st) != 0) 162 if (stat (fname, &st) != 0)
161 return; 163 return;
162 if (S_ISDIR (st.st_mode)) 164 if (S_ISDIR (st.st_mode))
163 { 165 {
164 sprintf (lastlogfile, "%.*s/%.*s", 166 sprintf (lastlogfile, "%.*s/%.*s",
165 sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname, 167 (int)(sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2), fname,
166 sizeof (pwent->pw_name), 168 (int)sizeof (pwent->pw_name),
167 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown" 169 (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown"
168 : pwent->pw_name); 170 : pwent->pw_name);
169 if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) 171 if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0)
170 { 172 {
171 write (fd, &ll, sizeof (ll)); 173 write (fd, &ll, sizeof (ll));
178 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)), 180 if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)),
179 SEEK_SET) != -1) 181 SEEK_SET) != -1)
180 write (fd, &ll, sizeof (ll)); 182 write (fd, &ll, sizeof (ll));
181 close (fd); 183 close (fd);
182 } 184 }
183# endif /* HAVE_STRUCT_LASTLOG */ 185# endif /* HAVE_STRUCT_LASTLOG */
184} 186}
185#endif /* LASTLOG_SUPPORT */ 187#endif /* LASTLOG_SUPPORT */
186 188
187/* ------------------------------------------------------------------------- */ 189/* ------------------------------------------------------------------------- */
188 190
189/* 191/*
190 * make and write utmp and wtmp entries 192 * make and write utmp and wtmp entries
206#ifdef HAVE_STRUCT_UTMPX 208#ifdef HAVE_STRUCT_UTMPX
207 struct utmpx *utx = &this->utx; 209 struct utmpx *utx = &this->utx;
208#endif 210#endif
209 int i; 211 int i;
210 struct passwd *pwent = getpwuid (getuid ()); 212 struct passwd *pwent = getpwuid (getuid ());
213 const char *name = (pwent && pwent->pw_name) ? pwent->pw_name : "?";
211 214
212 if (!strncmp (pty, "/dev/", 5)) 215 if (!strncmp (pty, "/dev/", 5))
213 pty += 5; /* skip /dev/ prefix */ 216 pty += 5; /* skip /dev/ prefix */
214 217
215#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX) 218#if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX)
216 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) 219 if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3))
217 strncpy (ut_id, pty + 3, sizeof (ut_id)); 220 strncpy (ut_id, pty + 3, sizeof (ut_id));
218 else if (sscanf (pty, "pts/%d", &i) == 1) 221 else if (sscanf (pty, "pts/%d", &i) == 1)
219 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ 222 sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */
220 else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) 223 else
221 { 224 {
222 ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); 225 ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
223 return; 226 return;
224 } 227 }
225#endif 228#endif
229# ifdef HAVE_UTMP_PID 232# ifdef HAVE_UTMP_PID
230 setutent (); 233 setutent ();
231 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); 234 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
232 ut->ut_type = DEAD_PROCESS; 235 ut->ut_type = DEAD_PROCESS;
233 getutid (ut); /* position to entry in utmp file */ 236 getutid (ut); /* position to entry in utmp file */
234 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
235# endif 237# endif
236#endif 238#endif
237 239
238#ifdef HAVE_STRUCT_UTMPX 240#ifdef HAVE_STRUCT_UTMPX
239 memset (utx, 0, sizeof (struct utmpx)); 241 memset (utx, 0, sizeof (struct utmpx));
240 setutxent (); 242 setutxent ();
241 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 243 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
242 utx->ut_type = DEAD_PROCESS; 244 utx->ut_type = DEAD_PROCESS;
243 getutxid (utx); /* position to entry in utmp file */ 245 getutxid (utx); /* position to entry in utmp file */
244 strncpy (this->ut_id, ut_id, sizeof (this->ut_id));
245#endif 246#endif
246 247
247#ifdef HAVE_STRUCT_UTMP 248#ifdef HAVE_STRUCT_UTMP
248 strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); 249 strncpy (ut->ut_line, pty, sizeof (ut->ut_line));
250# ifdef HAVE_UTMP_HOST
251 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
252# endif
249 ut->ut_time = time (NULL); 253 ut->ut_time = time (NULL);
250# ifdef HAVE_UTMP_PID 254# ifdef HAVE_UTMP_PID
251 strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", 255 strncpy (ut->ut_user, name, sizeof (ut->ut_user));
252 sizeof (ut->ut_user));
253 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); 256 strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id));
254 ut->ut_time = time (NULL);
255 ut->ut_pid = cmd_pid; 257 ut->ut_pid = cmd_pid;
256# ifdef HAVE_UTMP_HOST
257 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host));
258# endif
259 ut->ut_type = USER_PROCESS; 258 ut->ut_type = USER_PROCESS;
260 pututline (ut); 259 pututline (ut);
261 endutent (); /* close the file */ 260 endutent (); /* close the file */
262 utmp_pos = 0; 261 utmp_pos = 0;
263# else 262# else
264 strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?",
265 sizeof (ut->ut_name));
266# ifdef HAVE_UTMP_HOST
267 strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); 263 strncpy (ut->ut_name, name, sizeof (ut->ut_name));
268# endif
269# endif 264# endif
270#endif 265#endif
271 266
272#ifdef HAVE_STRUCT_UTMPX 267#ifdef HAVE_STRUCT_UTMPX
273 strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); 268 strncpy (utx->ut_line, pty, sizeof (utx->ut_line));
274 strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", 269 strncpy (utx->ut_user, name, sizeof (utx->ut_user));
275 sizeof (utx->ut_user));
276 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); 270 strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id));
277# if HAVE_UTMPX_SESSION 271# if HAVE_UTMPX_SESSION
278 utx->ut_session = getsid (0); 272 utx->ut_session = getsid (0);
279# endif 273# endif
280 utx->ut_tv.tv_sec = time (NULL); 274 utx->ut_tv.tv_sec = time (NULL);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines