mirror of
https://github.com/sheumann/hush.git
synced 2024-12-28 22:30:05 +00:00
small fixes: using fd-based io instead of FILE*-based,
missed O_TRUNC, etc
This commit is contained in:
parent
5a3395bc01
commit
61126ab30a
@ -40,7 +40,8 @@ static inline void push(pid_t pid)
|
|||||||
|
|
||||||
static int pid_is_exec(pid_t pid, const char *name)
|
static int pid_is_exec(pid_t pid, const char *name)
|
||||||
{
|
{
|
||||||
char buf[32], *execbuf;
|
char buf[sizeof("/proc//exe") + sizeof(int)*3];
|
||||||
|
char *execbuf;
|
||||||
int equal;
|
int equal;
|
||||||
|
|
||||||
sprintf(buf, "/proc/%d/exe", pid);
|
sprintf(buf, "/proc/%d/exe", pid);
|
||||||
@ -56,7 +57,7 @@ static int pid_is_exec(pid_t pid, const char *name)
|
|||||||
static int pid_is_user(int pid, int uid)
|
static int pid_is_user(int pid, int uid)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
char buf[32];
|
char buf[sizeof("/proc/") + sizeof(int)*3];
|
||||||
|
|
||||||
sprintf(buf, "/proc/%d", pid);
|
sprintf(buf, "/proc/%d", pid);
|
||||||
if (stat(buf, &sb) != 0)
|
if (stat(buf, &sb) != 0)
|
||||||
@ -66,7 +67,7 @@ static int pid_is_user(int pid, int uid)
|
|||||||
|
|
||||||
static int pid_is_cmd(pid_t pid, const char *name)
|
static int pid_is_cmd(pid_t pid, const char *name)
|
||||||
{
|
{
|
||||||
char buf[32];
|
char buf[sizeof("/proc//stat") + sizeof(int)*3];
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
@ -115,7 +116,6 @@ static void do_pidfile(void)
|
|||||||
fclose(f);
|
fclose(f);
|
||||||
} else if (errno != ENOENT)
|
} else if (errno != ENOENT)
|
||||||
bb_perror_msg_and_die("open pidfile %s", pidfile);
|
bb_perror_msg_and_die("open pidfile %s", pidfile);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void do_procinit(void)
|
static void do_procinit(void)
|
||||||
@ -146,28 +146,28 @@ static void do_procinit(void)
|
|||||||
|
|
||||||
static int do_stop(void)
|
static int do_stop(void)
|
||||||
{
|
{
|
||||||
RESERVE_CONFIG_BUFFER(what, 1024);
|
char *what;
|
||||||
struct pid_list *p;
|
struct pid_list *p;
|
||||||
int killed = 0;
|
int killed = 0;
|
||||||
|
|
||||||
do_procinit();
|
do_procinit();
|
||||||
|
|
||||||
if (cmdname)
|
if (cmdname)
|
||||||
strcpy(what, cmdname);
|
what = xstrdup(cmdname);
|
||||||
else if (execname)
|
else if (execname)
|
||||||
strcpy(what, execname);
|
what = xstrdup(execname);
|
||||||
else if (pidfile)
|
else if (pidfile)
|
||||||
sprintf(what, "process in pidfile `%.200s'", pidfile);
|
what = xasprintf("process in pidfile '%s'", pidfile);
|
||||||
else if (userspec)
|
else if (userspec)
|
||||||
sprintf(what, "process(es) owned by `%s'", userspec);
|
what = xasprintf("process(es) owned by '%s'", userspec);
|
||||||
else
|
else
|
||||||
bb_error_msg_and_die ("internal error, please report");
|
bb_error_msg_and_die("internal error, please report");
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
printf("no %s found; none killed.\n", what);
|
printf("no %s found; none killed\n", what);
|
||||||
if (ENABLE_FEATURE_CLEAN_UP)
|
if (ENABLE_FEATURE_CLEAN_UP)
|
||||||
RELEASE_CONFIG_BUFFER(what);
|
free(what);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (p = found; p; p = p->next) {
|
for (p = found; p; p = p->next) {
|
||||||
@ -183,10 +183,10 @@ static int do_stop(void)
|
|||||||
for (p = found; p; p = p->next)
|
for (p = found; p; p = p->next)
|
||||||
if(p->pid < 0)
|
if(p->pid < 0)
|
||||||
printf(" %d", -p->pid);
|
printf(" %d", -p->pid);
|
||||||
printf(").\n");
|
puts(")");
|
||||||
}
|
}
|
||||||
if (ENABLE_FEATURE_CLEAN_UP)
|
if (ENABLE_FEATURE_CLEAN_UP)
|
||||||
RELEASE_CONFIG_BUFFER(what);
|
free(what);
|
||||||
return killed;
|
return killed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,9 +29,7 @@ long uptime(void)
|
|||||||
*/
|
*/
|
||||||
static inline void sanitize_fds(void)
|
static inline void sanitize_fds(void)
|
||||||
{
|
{
|
||||||
int fd = open(bb_dev_null, O_RDWR, 0);
|
int fd = xopen(bb_dev_null, O_RDWR);
|
||||||
if (fd < 0)
|
|
||||||
return;
|
|
||||||
while (fd < 3)
|
while (fd < 3)
|
||||||
fd = dup(fd);
|
fd = dup(fd);
|
||||||
close(fd);
|
close(fd);
|
||||||
|
@ -13,9 +13,9 @@
|
|||||||
|
|
||||||
int dumpleases_main(int argc, char *argv[])
|
int dumpleases_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
FILE *fp;
|
int fp;
|
||||||
int i, c, mode = REMAINING;
|
int i, c, mode = REMAINING;
|
||||||
long expires;
|
unsigned long expires;
|
||||||
const char *file = LEASES_FILE;
|
const char *file = LEASES_FILE;
|
||||||
struct dhcpOfferedAddr lease;
|
struct dhcpOfferedAddr lease;
|
||||||
struct in_addr addr;
|
struct in_addr addr;
|
||||||
@ -43,11 +43,11 @@ int dumpleases_main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fp = xfopen(file, "r");
|
fp = xopen(file, O_RDONLY);
|
||||||
|
|
||||||
printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
|
printf("Mac Address IP-Address Expires %s\n", mode == REMAINING ? "in" : "at");
|
||||||
/* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
|
/* "00:00:00:00:00:00 255.255.255.255 Wed Jun 30 21:49:08 1993" */
|
||||||
while (fread(&lease, sizeof(lease), 1, fp)) {
|
while (full_read(fp, &lease, sizeof(lease)) == sizeof(lease)) {
|
||||||
printf(":%02x"+1, lease.chaddr[0]);
|
printf(":%02x"+1, lease.chaddr[0]);
|
||||||
for (i = 1; i < 6; i++) {
|
for (i = 1; i < 6; i++) {
|
||||||
printf(":%02x", lease.chaddr[i]);
|
printf(":%02x", lease.chaddr[i]);
|
||||||
@ -59,23 +59,16 @@ int dumpleases_main(int argc, char *argv[])
|
|||||||
if (!expires)
|
if (!expires)
|
||||||
printf("expired\n");
|
printf("expired\n");
|
||||||
else {
|
else {
|
||||||
if (expires > 60*60*24) {
|
unsigned d, h, m;
|
||||||
printf("%ld days, ", expires / (60*60*24));
|
d = expires / (24*60*60); expires %= (24*60*60);
|
||||||
expires %= 60*60*24;
|
h = expires / (60*60); expires %= (60*60);
|
||||||
}
|
m = expires / 60; expires %= 60;
|
||||||
if (expires > 60*60) {
|
if (d) printf("%u days ", d);
|
||||||
printf("%ld hours, ", expires / (60*60));
|
printf("%02u:%02u:%02u\n", h, m, (unsigned)expires);
|
||||||
expires %= 60*60;
|
|
||||||
}
|
|
||||||
if (expires > 60) {
|
|
||||||
printf("%ld minutes, ", expires / 60);
|
|
||||||
expires %= 60;
|
|
||||||
}
|
|
||||||
printf("%ld seconds\n", expires);
|
|
||||||
}
|
}
|
||||||
} else printf("%s", ctime(&expires));
|
} else fputs(ctime(&expires), stdout);
|
||||||
}
|
}
|
||||||
fclose(fp);
|
/* close(fp); */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -272,27 +272,32 @@ int read_config(const char *file)
|
|||||||
if (keywords[i].def[0])
|
if (keywords[i].def[0])
|
||||||
keywords[i].handler(keywords[i].def, keywords[i].var);
|
keywords[i].handler(keywords[i].def, keywords[i].var);
|
||||||
|
|
||||||
if (!(in = fopen(file, "r"))) {
|
in = fopen(file, "r");
|
||||||
bb_error_msg("unable to open config file: %s", file);
|
if (!in) {
|
||||||
|
bb_error_msg("cannot open config file: %s", file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
|
while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
|
||||||
char debug_orig[READ_CONFIG_BUF_SIZE];
|
char debug_orig[READ_CONFIG_BUF_SIZE];
|
||||||
|
char *p;
|
||||||
|
|
||||||
lm++;
|
lm++;
|
||||||
if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
|
p = strchr(buffer, '\n');
|
||||||
|
if (p) *p = '\0';
|
||||||
if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
|
if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
|
||||||
if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
|
p = strchr(buffer, '#');
|
||||||
|
if (p) *p = '\0';
|
||||||
|
|
||||||
if (!(token = strtok(buffer, " \t"))) continue;
|
if (!(token = strtok(buffer, " \t"))) continue;
|
||||||
if (!(line = strtok(NULL, ""))) continue;
|
if (!(line = strtok(NULL, ""))) continue;
|
||||||
|
|
||||||
/* eat leading whitespace */
|
/* eat leading whitespace */
|
||||||
line = line + strspn(line, " \t=");
|
line = skip_whitespace(line);
|
||||||
/* eat trailing whitespace */
|
/* eat trailing whitespace */
|
||||||
for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
|
i = strlen(line) - 1;
|
||||||
line[i] = '\0';
|
while (i >= 0 && isspace(line[i]))
|
||||||
|
line[i--] = '\0';
|
||||||
|
|
||||||
for (i = 0; keywords[i].keyword[0]; i++)
|
for (i = 0; keywords[i].keyword[0]; i++)
|
||||||
if (!strcasecmp(token, keywords[i].keyword))
|
if (!strcasecmp(token, keywords[i].keyword))
|
||||||
@ -311,14 +316,14 @@ int read_config(const char *file)
|
|||||||
|
|
||||||
void write_leases(void)
|
void write_leases(void)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
int fp;
|
||||||
unsigned int i;
|
unsigned i;
|
||||||
char buf[255];
|
|
||||||
time_t curr = time(0);
|
time_t curr = time(0);
|
||||||
unsigned long tmp_time;
|
unsigned long tmp_time;
|
||||||
|
|
||||||
if (!(fp = fopen(server_config.lease_file, "w"))) {
|
fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
||||||
bb_error_msg("unable to open %s for writing", server_config.lease_file);
|
if (fp < 0) {
|
||||||
|
bb_error_msg("cannot open %s for writing", server_config.lease_file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -334,33 +339,38 @@ void write_leases(void)
|
|||||||
else leases[i].expires -= curr;
|
else leases[i].expires -= curr;
|
||||||
} /* else stick with the time we got */
|
} /* else stick with the time we got */
|
||||||
leases[i].expires = htonl(leases[i].expires);
|
leases[i].expires = htonl(leases[i].expires);
|
||||||
fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
|
// FIXME: error check??
|
||||||
|
full_write(fp, &leases[i], sizeof(leases[i]));
|
||||||
|
|
||||||
/* Then restore it when done. */
|
/* then restore it when done */
|
||||||
leases[i].expires = tmp_time;
|
leases[i].expires = tmp_time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
close(fp);
|
||||||
|
|
||||||
if (server_config.notify_file) {
|
if (server_config.notify_file) {
|
||||||
sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
|
char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
|
||||||
system(buf);
|
system(cmd);
|
||||||
|
free(cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void read_leases(const char *file)
|
void read_leases(const char *file)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
int fp;
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
struct dhcpOfferedAddr lease;
|
struct dhcpOfferedAddr lease;
|
||||||
|
|
||||||
if (!(fp = fopen(file, "r"))) {
|
fp = open(file, O_RDONLY);
|
||||||
bb_error_msg("unable to open %s for reading", file);
|
if (fp < 0) {
|
||||||
|
bb_error_msg("cannot open %s for reading", file);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
|
while (i < server_config.max_leases
|
||||||
|
&& full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
|
||||||
|
) {
|
||||||
/* ADDME: is it a static lease */
|
/* ADDME: is it a static lease */
|
||||||
if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
|
if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
|
||||||
lease.expires = ntohl(lease.expires);
|
lease.expires = ntohl(lease.expires);
|
||||||
@ -373,5 +383,5 @@ void read_leases(const char *file)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
DEBUG("Read %d leases", i);
|
DEBUG("Read %d leases", i);
|
||||||
fclose(fp);
|
close(fp);
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
|
||||||
static char *saved_pidfile;
|
static const char *saved_pidfile;
|
||||||
|
|
||||||
static void pidfile_delete(void)
|
static void pidfile_delete(void)
|
||||||
{
|
{
|
||||||
@ -36,14 +36,14 @@ int pidfile_acquire(const char *pidfile)
|
|||||||
int pid_fd;
|
int pid_fd;
|
||||||
if (!pidfile) return -1;
|
if (!pidfile) return -1;
|
||||||
|
|
||||||
pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
|
pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644);
|
||||||
if (pid_fd < 0) {
|
if (pid_fd < 0) {
|
||||||
bb_perror_msg("unable to open pidfile %s", pidfile);
|
bb_perror_msg("cannot open pidfile %s", pidfile);
|
||||||
} else {
|
} else {
|
||||||
lockf(pid_fd, F_LOCK, 0);
|
lockf(pid_fd, F_LOCK, 0);
|
||||||
if (!saved_pidfile)
|
if (!saved_pidfile)
|
||||||
atexit(pidfile_delete);
|
atexit(pidfile_delete);
|
||||||
saved_pidfile = (char *) pidfile;
|
saved_pidfile = pidfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pid_fd;
|
return pid_fd;
|
||||||
|
Loading…
Reference in New Issue
Block a user