sj3serv: Migrate to KNF and ISO C all but setup.c

Signed-off-by: Masanori Ogino <mogino@acm.org>
This commit is contained in:
Masanori Ogino 2023-11-29 11:46:20 +09:00
commit 55d33df2a1
6 changed files with 1156 additions and 1147 deletions

View file

@ -28,34 +28,24 @@
* from Sony Corporation.
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "sj_sysvdef.h"
#include "sj_kcnv.h"
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <errno.h>
#include <netdb.h>
#include <setjmp.h>
#include <errno.h>
#ifdef SVR4
#include <sys/filio.h>
#include <sys/signal.h>
#include <poll.h>
#endif
#ifdef TLI
#include <fcntl.h>
#include <tiuser.h>
#include <stropts.h>
#include <sys/types.h>
#include <netdir.h>
#include <netconfig.h>
#endif
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sj_kcnv.h"
#ifdef SVR4
#define signal sigset
@ -104,7 +94,6 @@ static int putpos = 0;
static int buflen = 0;
static int getpos = 0;
void
socket_init(void)
{
@ -123,8 +112,6 @@ socket_init(void)
maxfds = 0;
}
static void
set_fd(int fd)
{
@ -134,12 +121,11 @@ set_fd(int fd)
maxfds++;
#else
FD_SET(fd, &fds_org);
if (fd >= maxfds) maxfds = fd + 1;
if (fd >= maxfds)
maxfds = fd + 1;
#endif
}
static void
clr_fd(int fd)
{
@ -164,7 +150,8 @@ clr_fd(int fd)
FD_CLR(fd, &fds_org);
if (maxfds == fd + 1) {
while (--fd >= 0) {
if (FD_ISSET(fd, &fds_org)) break;
if (FD_ISSET(fd, &fds_org))
break;
}
maxfds = fd + 1;
}
@ -173,7 +160,6 @@ clr_fd(int fd)
#if (!defined(TLI) || (defined(TLI) && defined(SOCK_UNIX)))
static void
open_af_unix(void)
{
@ -191,12 +177,14 @@ open_af_unix(void)
exit(1);
}
if (bind(fd_unix, (struct sockaddr *)&sunix, strlen(sunix.sun_path) + 2) == ERROR) {
if (errno == EADDRINUSE)
if (bind(fd_unix, (struct sockaddr *)&sunix,
strlen(sunix.sun_path) + 2) == ERROR) {
if (errno == EADDRINUSE) {
fprintf(stderr, "Port '%s' is already in use\n",
socket_name);
else
} else {
fprintf(stderr, "Can't bind AF_UNIX socket\n");
}
shutdown(fd_unix, 2);
close(fd_unix);
unlink(socket_name);
@ -219,7 +207,6 @@ struct t_call *inet_callptr;
struct netconfig *nconf;
#endif
static void
open_af_inet(void)
{
@ -240,7 +227,6 @@ open_af_inet(void)
#endif
int true = 1;
#ifdef TLI
if ((handlep = setnetpath()) == NULL) {
fprintf(stderr, "Setnetpath fail\n");
@ -249,7 +235,8 @@ open_af_inet(void)
while ((nconf = getnetpath(handlep)) != NULL) {
if ((nconf->nc_semantics == NC_TPI_COTS_ORD) &&
(strncmp(nconf->nc_proto, proto_name, strlen(proto_name)) == 0))
(strncmp(nconf->nc_proto, proto_name, strlen(proto_name)) ==
0))
break;
}
if (nconf == NULL) {
@ -265,16 +252,19 @@ open_af_inet(void)
exit(1);
}
}
if ((fd_inet = t_open(nconf->nc_device, O_RDWR, (struct t_info *) NULL)) < 0) {
if ((fd_inet = t_open(nconf->nc_device, O_RDWR,
(struct t_info *)NULL)) < 0) {
fprintf(stderr, "Can't t_open\n");
exit(1);
}
if ((opt = (struct t_optmgmt *) t_alloc(fd_inet, T_OPTMGMT, T_ALL)) == NULL) {
if ((opt = (struct t_optmgmt *)t_alloc(fd_inet, T_OPTMGMT, T_ALL)) ==
NULL) {
fprintf(stderr, "Can't t_alloc error for T_OPTMGT\n");
exit(1);
}
if ((hdr = (struct opthdr *) malloc(sizeof(*hdr) + sizeof(true))) == NULL) {
if ((hdr = (struct opthdr *)malloc(sizeof(*hdr) + sizeof(true))) ==
NULL) {
fprintf(stderr, "server: not malloc\n");
exit(1);
}
@ -314,13 +304,15 @@ open_af_inet(void)
t_close(fd_inet);
exit(1);
}
if (memcmp(req->addr.buf, addrs->n_addrs->buf, addrs->n_addrs->len) != 0) {
if (memcmp(req->addr.buf, addrs->n_addrs->buf, addrs->n_addrs->len) !=
0) {
fprintf(stderr, "Port '%s(%s)' is already in use\n",
port_name, port_number);
t_close(fd_inet);
exit(1);
}
if ((inet_callptr = (struct t_call *) t_alloc(fd_inet, T_CALL, T_ALL)) == NULL) {
if ((inet_callptr = (struct t_call *)t_alloc(fd_inet, T_CALL, T_ALL)) ==
NULL) {
fprintf(stderr, "t_alloc error for T_CALL\n");
t_close(fd_inet);
exit(1);
@ -328,7 +320,6 @@ open_af_inet(void)
inet_callptr->addr = *(addrs->n_addrs);
inet_callptr->udata.maxlen = 0;
inet_callptr->udata.len = 0;
inet_callptr->udata.buf = (char *)NULL;
@ -352,17 +343,18 @@ open_af_inet(void)
if (bind(fd_inet, (struct sockaddr *)&sin, sizeof(sin)) == ERROR) {
if (errno == EADDRINUSE) {
if (sp)
if (sp) {
fprintf(stderr,
"Port '%s(%d)' is already in use\n",
port_name, port);
else
} else {
fprintf(stderr,
"Port %d is already in use\n",
port);
}
else
} else {
fprintf(stderr, "Can't bind AF_INET socket\n");
}
shutdown(fd_inet, 2);
close(fd_inet);
exit(1);
@ -389,7 +381,6 @@ open_socket(void)
#if (!defined(TLI) || (defined(TLI) && defined(SOCK_UNIX)))
static int
connect_af_unix(void)
{
@ -407,38 +398,40 @@ connect_af_unix(void)
int
accept_func(int fd, struct t_call *callptr, char *name, int closeflg)
{
int newfd;
int look;
int look, newfd;
extern int t_errno;
if ((newfd = t_open(name, O_RDWR, (struct t_info *)0)) < 0) {
if (!closeflg) warning_out("Can't t_open");
if (!closeflg)
warning_out("Can't t_open");
return ERROR;
}
if (t_bind(newfd, (struct t_bind *)0, (struct t_bind *)0) < 0) {
if (!closeflg) warning_out("t_bind error");
if (!closeflg)
warning_out("t_bind error");
return ERROR;
}
t_errno = 0;
if (t_accept(fd, newfd, callptr) < 0) {
t_close(newfd);
if (!closeflg) warning_out("t_accept error");
if (!closeflg)
warning_out("t_accept error");
return ERROR;
}
if (ioctl(newfd, I_POP, (char *)0) < 0) {
if (!closeflg) warning_out("I_POP timod failed");
if (!closeflg)
warning_out("I_POP timod failed");
return ERROR;
}
if (ioctl(newfd, I_PUSH, "tirdwr") < 0) {
if (!closeflg) warning_out("I_PUSH tirdwr failed");
if (!closeflg)
warning_out("I_PUSH tirdwr failed");
return ERROR;
}
return (newfd);
}
#endif
static int
connect_af_inet(void)
{
@ -449,12 +442,11 @@ connect_af_inet(void)
int fd;
#ifdef TLI
if (t_listen(fd_inet, inet_callptr) < 0) {
if (t_listen(fd_inet, inet_callptr) < 0)
warning_out("t_listen error");
}
if ((fd = accept_func(fd_inet, inet_callptr, nconf->nc_device, FALSE)) == ERROR)
if ((fd = accept_func(fd_inet, inet_callptr, nconf->nc_device,
FALSE)) == ERROR)
#else
if ((fd = accept(fd_inet, (struct sockaddr *)&sin, &i)) == ERROR)
#endif
@ -464,7 +456,6 @@ connect_af_inet(void)
#if (!defined(TLI) || (defined(TLI) && defined(SOCK_UNIX)))
static void
close_af_unix(void)
{
@ -475,7 +466,8 @@ close_af_unix(void)
ioctl(fd_unix, FIONBIO, &true);
for (; ;) {
i = sizeof(sunix);
if (accept(fd_unix, (struct sockaddr *)&sunix, &i) < 0) break;
if (accept(fd_unix, (struct sockaddr *)&sunix, &i) < 0)
break;
}
shutdown(fd_unix, 2);
close(fd_unix);
@ -484,8 +476,6 @@ close_af_unix(void)
}
#endif
static void
close_af_inet(void)
{
@ -498,11 +488,13 @@ close_af_inet(void)
ioctl(fd_inet, FIONBIO, &true);
for (; ;) {
#ifdef TLI
if (accept_func(fd_inet, inet_callptr, nconf->nc_device, TRUE) < 0)
if (accept_func(fd_inet, inet_callptr, nconf->nc_device, TRUE) <
0)
break;
#else
i = sizeof(sin);
if (accept(fd_inet, (struct sockaddr *)&sin, &i) < 0) break;
if (accept(fd_inet, (struct sockaddr *)&sin, &i) < 0)
break;
#endif
}
#ifdef TLI
@ -525,8 +517,6 @@ close_socket(void)
close_af_inet();
}
static void
#ifdef SVR4
connect_client(struct pollfd *readfds)
@ -562,7 +552,8 @@ connect_client(fd_set *readfds)
}
#endif
}
if (i == maxfds) return;
if (i == maxfds)
return;
#else /* SVR4 */
if (FD_ISSET(fd_inet, readfds))
fd = connect_af_inet();
@ -580,20 +571,15 @@ connect_client(fd_set *readfds)
else
return;
#endif /* SVR4 */
if (fd == ERROR) return;
if (fd == ERROR)
return;
if (client_num >= MaxClientNum || client_num >= max_client) {
warning_out("No more client");
}
else {
} else {
debug_out(1, "client create(%d)\r\n", fd);
logging_out("connect to client on %d", fd);
if ((client_tmp = (Client *)malloc(sizeof(Client))) != NULL) {
memset(client_tmp, 0, sizeof(Client));
client_tmp->fd = fd;
@ -630,18 +616,18 @@ connect_client(fd_set *readfds)
}
static Client *
disconnect_client(Client *cp)
{
WorkArea *wp;
StdyFile *sp;
int fd;
Client *cl_tmp, *cl_before;
Client *cl_before, *cl_tmp;
if ((sp = cp->stdy) != NULL) closestdy(sp);
if ((wp = cp->work) != NULL) free_workarea(wp);
if ((sp = cp->stdy) != NULL)
closestdy(sp);
if ((wp = cp->work) != NULL)
free_workarea(wp);
debug_out(1, "client dead(%d)\r\n", cp->fd);
logging_out("disconnect from client on %d", cp->fd);
@ -690,10 +676,6 @@ disconnect_client(Client *cp)
return cp;
}
#ifndef SIGPIPE
#include <signal.h>
#endif
void
communicate(void)
{
@ -705,23 +687,23 @@ communicate(void)
int i;
while (poll(poll_fds, maxfds, -1) == ERROR) {
if (errno != EINTR) error_out("poll");
if (errno != EINTR)
error_out("poll");
errno = 0;
}
#else
fd_set readfds;
readfds = fds_org;
while (select(maxfds, &readfds, NULL, NULL, NULL) == ERROR) {
if (errno != EINTR) error_out("select");
if (errno != EINTR)
error_out("select");
errno = 0;
}
#endif
for (cur_client = client; cur_client;) {
if (setjmp(client_dead)) {
cur_client = disconnect_client(cur_client);
continue;
@ -753,33 +735,29 @@ communicate(void)
}
}
void
put_flush(void)
{
int len;
int i;
int i, len;
char *p;
for (len = putpos, p = putbuf; len > 0;) {
if ((i = write(client_fd, p, len)) == ERROR) {
if ((i = write(client_fd, p, len)) == ERROR)
longjmp(client_dead, -1);
}
len -= i;
p += i;
}
putpos = 0;
}
void
put_byte(int c)
{
putbuf[putpos++] = c;
if (putpos >= sizeof(putbuf)) put_flush();
if (putpos >= sizeof(putbuf))
put_flush();
}
void
put_work(int c)
{
@ -787,7 +765,6 @@ put_work(int c)
put_byte(c & 0xff);
}
void
put_int(int c)
{
@ -797,7 +774,6 @@ put_int(int c)
put_byte(c);
}
u_char *
put_string(u_char *p)
{
@ -805,31 +781,33 @@ put_string(u_char* p)
do {
put_byte(*p);
} while (*p++);
}
else
} else {
put_byte(0);
}
return p;
}
u_char *
put_ndata(u_char *p, int n)
{
while (n-- > 0) put_byte(p ? *p++ : 0);
while (n-- > 0)
put_byte(p ? *p++ : 0);
return p;
}
void
get_buf(void)
{
int i;
for (;;) {
if ((i = read(client_fd, getbuf, sizeof(getbuf))) > 0) break;
if ((i = read(client_fd, getbuf, sizeof(getbuf))) > 0)
break;
if (i == ERROR) {
if (errno == EINTR) continue;
if (errno == EWOULDBLOCK) continue;
if (errno == EINTR)
continue;
if (errno == EWOULDBLOCK)
continue;
}
longjmp(client_dead, -1);
}
@ -837,39 +815,34 @@ get_buf(void)
getpos = 0;
}
int
get_byte(void)
{
if (getpos >= buflen) get_buf();
return (getbuf[getpos++] & 0xff);
if (getpos >= buflen)
get_buf();
return getbuf[getpos++] & 0xff;
}
int
get_word(void)
{
int i;
i = get_byte();
return ((i << 8) | get_byte());
return (i << 8) | get_byte();
}
int
get_int(void)
{
int i0;
int i1;
int i2;
int i0, i1, i2;
i0 = get_byte();
i1 = get_byte();
i2 = get_byte();
return((i0 << (8*3)) | (i1 << (8*2)) | (i2 << (8*1)) | get_byte());
return (i0 << (8*3)) | (i1 << (8*2)) | (i2 << (8*1)) | get_byte();
}
int
get_nstring(u_char *p, int n)
{
@ -877,16 +850,17 @@ get_nstring(u_char* p, int n)
do {
c = get_byte();
if (n-- > 0) *p++ = c;
if (n-- > 0)
*p++ = c;
} while (c);
return (n < 0) ? ERROR : 0;
}
u_char *
get_ndata(u_char *p, int n)
{
while (n-- > 0) *p++ = get_byte();
while (n-- > 0)
*p++ = get_byte();
return p;
}

View file

@ -28,16 +28,13 @@
* from Sony Corporation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sj_typedef.h"
#include "server.h"
#include "sj_typedef.h"
extern char program_name[];
extern int debug_level;
@ -48,7 +45,6 @@ static FILE *errfp = NULL;
static FILE *logfp = NULL;
static FILE *dbgfp = NULL;
int
open_error(void)
{
@ -59,17 +55,16 @@ open_error(void)
if (!strcmp(error_file, tty_name)) {
errfp = stderr;
flg = -1;
}
else if (!(errfp = fopen(error_file, "a")))
} else if (!(errfp = fopen(error_file, "a"))) {
fprintf(stderr, "Can't open \"%s\"\r\n", error_file);
}
else
} else {
errfp = NULL;
}
return flg;
}
void
error_out(char *s, int p1, int p2, int p3, int p4, int p5)
{
@ -83,7 +78,6 @@ error_out(char* s, int p1, int p2, int p3, int p4, int p5)
exit(1);
}
void
warning_out(char *s, int p1, int p2, int p3, int p4, int p5)
{
@ -96,7 +90,6 @@ warning_out(char* s, int p1, int p2, int p3, int p4, int p5)
}
}
int
open_log(void)
{
@ -107,9 +100,9 @@ open_log(void)
if (!strcmp(log_file, tty_name)) {
logfp = stderr;
flg = -1;
}
else
} else {
logfp = fopen(log_file, "a");
}
if (logfp) {
time_t t;
@ -117,17 +110,16 @@ open_log(void)
fprintf(logfp, "%s: log started at %s\r",
program_name, ctime(&t));
fflush(logfp);
}
else
} else {
fprintf(stderr, "Can't open \"%s\"\r\n", log_file);
}
else
} else {
logfp = NULL;
}
return flg;
}
void
logging_out(char *s, int p1, int p2, int p3, int p4, int p5)
{
@ -140,7 +132,6 @@ logging_out(char* s, int p1, int p2, int p3, int p4, int p5)
}
}
int
open_debug(void)
{
@ -151,17 +142,16 @@ open_debug(void)
if (!strcmp(debug_file, tty_name)) {
dbgfp = stderr;
flg = -1;
}
else if (!(dbgfp = fopen(debug_file, "a")))
} else if (!(dbgfp = fopen(debug_file, "a"))) {
fprintf(stderr, "Can't open \"%s\"\r\n", debug_file);
}
else
} else {
dbgfp = NULL;
}
return flg;
}
void
debug_out(int lvl, char *s, int p1, int p2, int p3, int p4, int p5)
{

File diff suppressed because it is too large Load diff

View file

@ -29,11 +29,7 @@
*/
#include "sj_sysvdef.h"
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/types.h>
@ -55,23 +51,19 @@
extern int fork_flag;
extern char *lock_file;
#ifdef LOCK_FILE
int
make_lockfile(void)
{
int fd;
fd = open(lock_file, O_CREAT | O_EXCL, 0600);
if (fd < 0) return ERROR;
if (fd < 0)
return ERROR;
close(fd);
return 0;
}
int
erase_lockfile(void)
{
@ -80,18 +72,13 @@ erase_lockfile (void)
#endif
static int
signal_handler (sig, code, scp)
int sig;
int code;
struct sigcontext *scp;
signal_handler(int sig, int code, struct sigcontext *scp)
{
warning_out("signal %d, code %d catched.", sig, code);
}
static int terminate_handler(sig, code, scp)
int sig;
int code;
struct sigcontext *scp;
static int
terminate_handler(int sig, int code, struct sigcontext *scp)
{
close_socket();
sj_closeall();
@ -112,8 +99,6 @@ server_terminate (void)
exit(0);
}
static void
exec_fork(void)
{
@ -123,8 +108,7 @@ exec_fork (void)
if (tmp < 0) {
fprintf(stderr, "Can't fork child process\r\n");
fflush(stderr);
}
else {
} else {
signal(SIGCHLD, _exit);
signal(SIGHUP, SIG_IGN);
signal(SIGINT, SIG_IGN);
@ -138,7 +122,6 @@ exec_fork (void)
signal(SIGHUP, (void (*)())signal_handler);
signal(SIGINT, (void (*)())terminate_handler);
signal(SIGQUIT, (void (*)())signal_handler);
signal(SIGTERM, (void (*)())terminate_handler);
@ -146,8 +129,6 @@ exec_fork (void)
signal(SIGTSTP, SIG_IGN);
}
static void
leave_tty(void)
{
@ -159,7 +140,8 @@ leave_tty (void)
kill(getppid(), SIGTERM);
fclose(stdin);
fclose(stdout);
if (!tmp) fclose(stderr);
if (!tmp)
fclose(stderr);
if (fork_flag) {
if ((tmp = open("/dev/tty", O_RDWR)) >= 0) {
@ -169,14 +151,13 @@ leave_tty (void)
}
}
int
main(int argc, char **argv)
{
#ifndef __NetBSD__
if (setuid(geteuid())) {
fprintf(stderr, "error at setuid.\r\n"); exit(1);
fprintf(stderr, "error at setuid.\r\n");
exit(1);
}
#endif
@ -186,7 +167,8 @@ main (int argc, char **argv)
#ifdef LOCK_FILE
if (make_lockfile() == ERROR) {
fprintf(stderr, "Already running...\r\n"); exit(1);
fprintf(stderr, "Already running...\r\n");
exit(1);
}
#endif

View file

@ -28,10 +28,4 @@
* from Sony Corporation.
*/
char *time_stamp = "Mon Mar 23 16:42:59 JST 1998";

View file

@ -28,9 +28,4 @@
* from Sony Corporation.
*/
char *version_number = "2.08C";