Skip to content

Commit

Permalink
Make it work even on big endian like s390(x)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitstreamout committed Jul 10, 2023
1 parent f2f8b05 commit c8889b7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
42 changes: 24 additions & 18 deletions blogctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ static int getsocket(void)
return fd;
}

static int getcmd(int argc, char *argv[])
static char getcmd(int argc, char *argv[])
{
static const struct {
const char* cmd;
const int req;
const char req;
const int arg;
const char* opt;
} cmds[] = {
Expand All @@ -53,7 +53,7 @@ static int getcmd(int argc, char *argv[])
{ "reactivate", MAGIC_REACTIVATE, 0, NULL }, /* Reactivate logging */
{}
}, *cmd = cmds;
int ret = -1;
char ret = (char)-1;

if (argc <= 1)
goto out;
Expand Down Expand Up @@ -87,7 +87,13 @@ int main(int argc, char *argv[])
int fdsock = -1, ret, len;

cmd[1] = '\0';
while ((cmd[0] = getcmd(argc, argv)) != -1) {
answer[0] = '\x15';

fdsock = getsocket();
if (fdsock < 0)
error("no blogd active");

while ((cmd[0] = getcmd(argc, argv)) != (char)-1) {
switch (cmd[0]) {
case MAGIC_CHROOT:
root = optarg;
Expand All @@ -100,9 +106,7 @@ int main(int argc, char *argv[])
ret = asprintf(&message, "%c\002%c%s%n", cmd[0], (int)(strlen(root) + 1), root, &len);
if (ret < 0)
error("can not allocate message");
fdsock = getsocket();
if (fdsock >= 0)
safeout(fdsock, message, len+1, SSIZE_MAX);
safeout(fdsock, message, len+1, SSIZE_MAX);
free(message);
break;
case MAGIC_PING:
Expand All @@ -112,24 +116,26 @@ int main(int argc, char *argv[])
case MAGIC_CLOSE:
case MAGIC_DEACTIVATE:
case MAGIC_REACTIVATE:
fdsock = getsocket();
if (fdsock >= 0)
safeout(fdsock, cmd, strlen(cmd)+1, SSIZE_MAX);
safeout(fdsock, cmd, strlen(cmd)+1, SSIZE_MAX);
break;
case '?':
default:
return 1;
case '?':
default:
goto fail;
}

answer[0] = '\0';
if (fdsock >= 0) {
if (can_read(fdsock, 1000))
safein(fdsock, &answer[0], sizeof(answer));
close(fdsock);
if (can_read(fdsock, 1000)) {
answer[0] = '\0';
safein(fdsock, &answer[0], sizeof(answer));
}

break; /* One command per call only */
}

argv += optind;
argc -= optind;
fail:
if (fdsock >= 0)
close(fdsock);

return answer[0] == '\x6' ? 0 : 1;
}
32 changes: 28 additions & 4 deletions libconsole/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
# define _PATH_BLOG_FIFO "/dev/blog"
#endif

#if defined(__s390__)
# define RED ""
# define BOLD ">> "
# define NORM ""
#else
# define RED "\e[31m"
# define BOLD "\e[1m"
# define NORM "\e[m"
#endif

int final = 0;
static volatile char *_arg0;

Expand Down Expand Up @@ -101,9 +111,23 @@ static void (*vc_reconnect)(int fd);
void safeout (int fd, const void *ptr, size_t s, ssize_t max)
{
int saveerr = errno;
int issocket = 0;
struct stat st;

if (fstat(fd, &st) < 0)
goto out;
if (S_ISSOCK(st.st_mode))
issocket++;

while (s > 0) {
ssize_t p = write (fd, ptr, (max < 1) ? 1 : ((s < (size_t)max) ? s : (size_t)max));
ssize_t p;
if (issocket) {
int flags = MSG_NOSIGNAL;
if (s > max)
flags |= MSG_MORE;
p = send (fd, ptr, (max < 1) ? 1 : ((s < (size_t)max) ? s : (size_t)max), flags);
} else
p = write (fd, ptr, (max < 1) ? 1 : ((s < (size_t)max) ? s : (size_t)max));
if (p < 0) {
if (errno == EPIPE)
break;
Expand Down Expand Up @@ -167,7 +191,7 @@ ssize_t safein (int fd, void *ptr, size_t s)
if (safein_noexit || signaled)
goto out;
if (fd == 0 && errno == EIO)
warn("\e[31m\e[1msystem console stolen at line %d!\e[m", __LINE__);
warn(RED BOLD "system console stolen at line %d!" NORM, __LINE__);
lerror("Can not read from fd %d", fd);
}

Expand All @@ -191,7 +215,7 @@ ssize_t safein (int fd, void *ptr, size_t s)
if (safein_noexit || signaled)
goto out;
if (fd == 0 && errno == EIO)
warn("\e[31m\e[1msystem console stolen at line %d!\e[m", __LINE__);
warn(RED BOLD "system console stolen at line %d!" NORM, __LINE__);
lerror("Can not read from fd %d", fd);
}
repeated = 0;
Expand Down Expand Up @@ -1184,7 +1208,7 @@ static void ask_for_password(void)
if (c->flags & CON_SERIAL)
len = asprintf(&message, "\n\r%s: ", pwprompt);
else
len = asprintf(&message, "\e[1m\r%s:\e[m ", pwprompt);
len = asprintf(&message, BOLD "\r%s: " NORM, pwprompt);
if (len < 0) {
warn("can not set password prompt");
_exit(1);
Expand Down
6 changes: 3 additions & 3 deletions libconsole/readpw.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ ssize_t readpw(int fd, char *pass, int eightbit)
{
char *ptr = pass;
struct chardata cp;
int c, ret;
int ret;

cp.eol = *ptr = '\0';

while (cp.eol == '\0') {
char ascval;
char ascval, c;

ret = read(fd, &c, 1);
if (ret < 0) {
Expand All @@ -58,7 +58,7 @@ ssize_t readpw(int fd, char *pass, int eightbit)
if (eightbit)
ascval = c;
else if (c != (ascval = (c & 0177))) {
uint32_t bits, mask;
uint8_t bits, mask;
for (bits = 1, mask = 1; mask & 0177; mask <<= 1) {
if (mask & ascval)
bits++;
Expand Down

0 comments on commit c8889b7

Please sign in to comment.