/*
* Scan /dev and find the device name.
*/
-static int findtty(char *res, int rlen, dev_t dev)
+static int findtty(char *res, const char *startdir, size_t rlen, dev_t dev)
{
DIR *dir;
struct dirent *ent;
- struct stat st;
int r = -1;
- char *olddir = getcwd(NULL, 0);
- if (chdir(startdir) < 0 || (dir = opendir(".")) == NULL) {
+ if ((dir = opendir(startdir)) == NULL) {
int msglen = strlen(startdir) + 11;
char *msg = malloc(msglen);
snprintf(msg, msglen, "bootlogd: %s", startdir);
perror(msg);
free(msg);
- chdir(olddir);
return -1;
}
while ((ent = readdir(dir)) != NULL) {
- if (lstat(ent->d_name, &st) != 0)
+ struct stat st;
+ int pathlen = strlen(startdir) + strlen(ent->d_name) + 2;
+ char *path = malloc(pathlen);
+ snprintf(path, pathlen, "%s/%s", startdir, ent->d_name);
+
+ if (lstat(path, &st) != 0) {
+ free(path);
continue;
+ }
if (S_ISDIR(st.st_mode)
&& 0 != strcmp(".", ent->d_name)
&& 0 != strcmp("..", ent->d_name)) {
- char *path = malloc(rlen);
- snprintf(path, rlen, "%s/%s", startdir, ent->d_name);
r = findtty(res, path, rlen, dev);
- free(path);
if (0 == r) { /* device found, return */
+ free(path);
closedir(dir);
- chdir(olddir);
return 0;
}
+ free(path);
continue;
}
+ free(path);
+ path = NULL;
if (!S_ISCHR(st.st_mode))
continue;
if (st.st_rdev == dev) {
- if (strlen(ent->d_name) + strlen(startdir) + 1 >= rlen) {
+ if ( (strlen(ent->d_name) + strlen(startdir) + 1) >= rlen) {
fprintf(stderr, "bootlogd: console device name too long\n");
closedir(dir);
- chdir(olddir);
return -1;
} else {
snprintf(res, rlen, "%s/%s", startdir, ent->d_name);
closedir(dir);
- chdir(olddir);
return 0;
}
}
}
closedir(dir);
- chdir(olddir);
return r;
}
* See if a console taken from the kernel command line maps
* to a character device we know about, and if we can open it.
*/
-int isconsole(char *s, char *res, int rlen)
+int isconsole(char *s, char *res, size_t rlen)
{
struct consdev *c;
int l, sl, i, fd;
* Find out the _real_ console. Assume that stdin is connected to
* the console device (/dev/console).
*/
-int consolename(char *res, int rlen)
+int consolename(char *res, size_t rlen)
{
#ifdef TIOCGDEV
unsigned int kdev;
#endif
- struct stat st, st2;
+ struct stat st;
+ int n;
+#ifdef __linux__
char buf[256];
char *p;
+ struct stat st2;
int didmount = 0;
- int n, r;
+ int r;
int fd;
+#endif
fstat(0, &st);
if (major(st.st_rdev) != 5 || minor(st.st_rdev) != 1) {
}
#ifdef TIOCGDEV
+# ifndef ENOIOCTLCMD
+# define ENOIOCTLCMD 515
+# endif
if (ioctl(0, TIOCGDEV, &kdev) == 0) {
int r = findtty(res, "/dev", rlen, (dev_t)kdev);
if (0 != r)
perror("bootlogd: /proc/cmdline");
} else {
buf[0] = 0;
- if ((n = read(fd, buf, sizeof(buf) - 1)) >= 0)
+ if ((n = read(fd, buf, sizeof(buf) - 1)) >= 0) {
r = 0;
- else
+ buf[sizeof(buf)-1] = 0; /* enforce null termination */
+ } else
perror("bootlogd: /proc/cmdline");
close(fd);
}
break;
case '\n':
didnl = 1;
- dosync = syncalot;
+ dosync = 1;
break;
case '\t':
line.pos += (line.pos / 8 + 1) * 8;
if (dosync) {
fflush(fp);
- fdatasync(fileno(fp));
+ if (syncalot) {
+ fdatasync(fileno(fp));
+ }
}
outptr += olen;
int realfd;
int n, m, i;
int todo;
+#ifndef __linux__ /* BSD-style ioctl needs an argument. */
+ int on = 1;
+#endif
fp = NULL;
logfile = LOGFILE;
return 1;
}
+#ifdef __linux__
(void)ioctl(0, TIOCCONS, NULL);
-#if 1
/* Work around bug in 2.1/2.2 kernels. Fixed in 2.2.13 and 2.3.18 */
if ((n = open("/dev/tty0", O_RDWR)) >= 0) {
(void)ioctl(n, TIOCCONS, NULL);
close(n);
}
#endif
- if (ioctl(pts, TIOCCONS, NULL) < 0) {
+#ifdef __linux__
+ if (ioctl(pts, TIOCCONS, NULL) < 0)
+#else /* BSD usage of ioctl TIOCCONS. */
+ if (ioctl(pts, TIOCCONS, &on) < 0)
+#endif
+ {
fprintf(stderr, "bootlogd: ioctl(%s, TIOCCONS): %s\n",
buf, strerror(errno));
return 1;