Skip to content

Commit

Permalink
Add code formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
fhunleth committed Jul 1, 2021
1 parent d447850 commit 4ccc19f
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 55 deletions.
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ check:
do_check: erlinit fixture
tests/run_tests.sh

format:
astyle \
--style=kr \
--indent=spaces=4 \
--align-pointer=name \
--align-reference=name \
--convert-tabs \
--attach-namespaces \
--max-code-length=100 \
--max-instatement-indent=120 \
--pad-header \
--pad-oper \
src/*.[ch]

clean:
$(RM) erlinit
$(RM) -r tests/work
Expand Down
2 changes: 1 addition & 1 deletion src/cfgloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void merge_config(int argc, char *argv[], int *merged_argc, char **merged_argv)
argc = MAX_ARGC - *merged_argc + 1;
}

memcpy(&merged_argv[*merged_argc], &argv[1], (argc - 1) * sizeof(char**));
memcpy(&merged_argv[*merged_argc], &argv[1], (argc - 1) * sizeof(char **));
*merged_argc += argc - 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int system_cmd(const char *cmd, char *output_buffer, int length)
int index = 0;
int amt;
while (index != length &&
(amt = read(pipefd[0], &output_buffer[index], length - index)) > 0)
(amt = read(pipefd[0], &output_buffer[index], length - index)) > 0)
index += amt;
output_buffer[index] = '\0';
close(pipefd[0]);
Expand Down
34 changes: 21 additions & 13 deletions src/erlinit.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static int is_directory(const char *path)
{
struct stat sb;
return stat(path, &sb) == 0 &&
S_ISDIR(sb.st_mode);
S_ISDIR(sb.st_mode);
}

static int starts_with(const char *str, const char *what)
Expand All @@ -108,7 +108,8 @@ static int erts_filter(const struct dirent *d)
return starts_with(d->d_name, "erts-");
}

static void find_erts_directory(const char *erts_version, const char *release_base_dir, char **erts_dir)
static void find_erts_directory(const char *erts_version, const char *release_base_dir,
char **erts_dir)
{
debug("find_erts_directory");

Expand All @@ -126,7 +127,8 @@ static void find_erts_directory(const char *erts_version, const char *release_ba
if (is_directory(*erts_dir))
return;

warn("start_erl.data specifies erts-%s, but it wasn't found! Looking for any erts version", erts_version);
warn("start_erl.data specifies erts-%s, but it wasn't found! Looking for any erts version",
erts_version);
}

struct dirent **namelist;
Expand Down Expand Up @@ -156,7 +158,7 @@ static int file_exists(const char *path)
static int dotfile_filter(const struct dirent *d)
{
return strcmp(d->d_name, ".") != 0 &&
strcmp(d->d_name, "..") != 0;
strcmp(d->d_name, "..") != 0;
}

static int bootfile_filter(const struct dirent *d)
Expand Down Expand Up @@ -220,7 +222,8 @@ static int find_boot_path_user(const char *release_version_dir, char **boot_path
return 0;
}

static int find_boot_path_by_release_name(const char *release_version_dir, const char *release_name, char **boot_path)
static int find_boot_path_by_release_name(const char *release_version_dir, const char *release_name,
char **boot_path)
{
// If not a named release, skip this option.
if (release_name == NULL || *release_name == '\0')
Expand Down Expand Up @@ -266,12 +269,13 @@ static void find_boot_path_auto(const char *release_version_dir, char **boot_pat
free(namelist);
}

static void find_boot_path(const char *release_version_dir, const char *release_name, char **boot_path)
static void find_boot_path(const char *release_version_dir, const char *release_name,
char **boot_path)
{
debug("find_boot_path");

if (!find_boot_path_user(release_version_dir, boot_path) &&
!find_boot_path_by_release_name(release_version_dir, release_name, boot_path))
!find_boot_path_by_release_name(release_version_dir, release_name, boot_path))
find_boot_path_auto(release_version_dir, boot_path);
}

Expand Down Expand Up @@ -311,7 +315,8 @@ static int find_consolidated_dirs(const char *release_base_dir,
free(consolidated_path);
}
if (num_found > 1)
warn("More than one consolidated directory found. Using '%s'", run_info->consolidated_protocols_path);
warn("More than one consolidated directory found. Using '%s'",
run_info->consolidated_protocols_path);

// Free everything
while (--n >= 0)
Expand All @@ -335,7 +340,8 @@ static int read_start_erl(const char *releases_dir, char **erts_version, char **
#define MAX_VERSION_AND_RELEASE_NAMES 32
char erts_string[MAX_VERSION_AND_RELEASE_NAMES + 1];
char rel_string[MAX_VERSION_AND_RELEASE_NAMES + 1];
if (fscanf(fp, "%" xstr(MAX_VERSION_AND_RELEASE_NAMES) "s %" xstr(MAX_VERSION_AND_RELEASE_NAMES) "s",
if (fscanf(fp, "%" xstr(MAX_VERSION_AND_RELEASE_NAMES) "s %" xstr(MAX_VERSION_AND_RELEASE_NAMES)
"s",
erts_string, rel_string) != 2) {
warn("%s doesn't contain expected contents. Skipping.", start_erl_path);
free(start_erl_path);
Expand Down Expand Up @@ -671,8 +677,9 @@ static void child()
// in the options, but if that doesn't work, go to the root of
// the release.
if (options.working_directory == NULL ||
chdir(options.working_directory) < 0) {
OK_OR_FATAL(chdir(run_info.release_base_dir), "Cannot chdir to release directory (%s)", run_info.release_base_dir);
chdir(options.working_directory) < 0) {
OK_OR_FATAL(chdir(run_info.release_base_dir), "Cannot chdir to release directory (%s)",
run_info.release_base_dir);
}

// Optionally run a "pre-run" program
Expand Down Expand Up @@ -747,7 +754,7 @@ static void child()
if (options.verbose) {
// Dump the environment and commandline
extern char **environ;
char** env = environ;
char **env = environ;
while (*env != 0)
debug("Env: '%s'", *env++);

Expand Down Expand Up @@ -824,7 +831,8 @@ static void wait_for_graceful_shutdown(pid_t pid, struct erlinit_exit_info *exit
} else if (rc < 0) {
if (errno == EAGAIN) {
// Timeout. Brutal kill our child so that the shutdown process can continue.
warn("Graceful shutdown timer expired (%d ms). Killing Erlang VM process shortly. Adjust timeout with --graceful-shutdown-timeout option.", options.graceful_shutdown_timeout_ms);
warn("Graceful shutdown timer expired (%d ms). Killing Erlang VM process shortly. Adjust timeout with --graceful-shutdown-timeout option.",
options.graceful_shutdown_timeout_ms);
break;
} else if (errno != EINTR) {
warn("Unexpected errno %d from sigtimedwait", errno);
Expand Down
20 changes: 13 additions & 7 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ void pivot_root_on_overlayfs()
(void) mkdir("/mnt/.work", 0755);


if (mount("", "/mnt/.merged", "overlay", 0, "lowerdir=/,upperdir=/mnt/.upper,workdir=/mnt/.work") < 0) {
if (mount("", "/mnt/.merged", "overlay", 0,
"lowerdir=/,upperdir=/mnt/.upper,workdir=/mnt/.work") < 0) {
warn("Could not mount overlayfs: %s\n"
"Check that CONFIG_OVERLAY_FS=y is in the kernel config.", strerror(errno));
return;
Expand Down Expand Up @@ -125,11 +126,14 @@ void pivot_root_on_overlayfs()
void setup_pseudo_filesystems()
{
// This only works in the real environment.
OK_OR_WARN(mount("proc", "/proc", "proc", MS_NOEXEC | MS_NOSUID | MS_NODEV, NULL), "Cannot mount /proc");
OK_OR_WARN(mount("sysfs", "/sys", "sysfs", MS_NOEXEC | MS_NOSUID | MS_NODEV, NULL), "Cannot mount /sys");
OK_OR_WARN(mount("proc", "/proc", "proc", MS_NOEXEC | MS_NOSUID | MS_NODEV, NULL),
"Cannot mount /proc");
OK_OR_WARN(mount("sysfs", "/sys", "sysfs", MS_NOEXEC | MS_NOSUID | MS_NODEV, NULL),
"Cannot mount /sys");

// /dev should be automatically created/mounted by Linux
OK_OR_WARN(mount("devtmpfs", "/dev", "devtmpfs", MS_REMOUNT | MS_NOEXEC | MS_NOSUID, "size=1024k"), "Cannot remount /dev");
OK_OR_WARN(mount("devtmpfs", "/dev", "devtmpfs", MS_REMOUNT | MS_NOEXEC | MS_NOSUID, "size=1024k"),
"Cannot remount /dev");

// Create entries in /dev. Turn off the umask since we want the exact
// permissions that we're specifying.
Expand All @@ -138,7 +142,8 @@ void setup_pseudo_filesystems()
OK_OR_WARN(mkdir("/dev/shm", 01777), "Cannot create /dev/shm");
umask(old_umask);

OK_OR_WARN(mount("devpts", "/dev/pts", "devpts", MS_NOEXEC | MS_NOSUID, "gid=5,mode=620"), "Cannot mount /dev/pts");
OK_OR_WARN(mount("devpts", "/dev/pts", "devpts", MS_NOEXEC | MS_NOSUID, "gid=5,mode=620"),
"Cannot mount /dev/pts");
}

void mount_filesystems()
Expand Down Expand Up @@ -175,7 +180,7 @@ void mount_filesystems()
(void) mkdir(target, 0755);

unsigned long imountflags = str_to_mountflags(mountflags);
if (mount(source, target, filesystemtype, imountflags, (void*) data) < 0)
if (mount(source, target, filesystemtype, imountflags, (void *) data) < 0)
warn("Cannot mount %s at %s: %s", source, target, strerror(errno));
} else {
warn("Invalid parameter to -m. Expecting 5 colon-separated fields");
Expand Down Expand Up @@ -204,7 +209,8 @@ void unmount_all()
int passno;
int i = 0;
while (i < MAX_MOUNTS &&
fscanf(fp, "%255s %255s %31s %127s %d %d", mounts[i].source, mounts[i].target, mounts[i].fstype, options, &freq, &passno) >= 3) {
fscanf(fp, "%255s %255s %31s %127s %d %d", mounts[i].source, mounts[i].target, mounts[i].fstype,
options, &freq, &passno) >= 3) {
i++;
}
fclose(fp);
Expand Down
26 changes: 13 additions & 13 deletions src/hostname.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,19 @@ static void make_rfc1123_compatible(char *s)
// Invalid characters are removed.
char *d = s;
while (*s) {
char c = *s;
if ((c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '.') {
// Good
*d = c;
d++;
} else if (c >= 'A' && c <= 'Z') {
// Make lowercase
*d = c - 'A' + 'a';
d++;
}
s++;
char c = *s;
if ((c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '-' || c == '.') {
// Good
*d = c;
d++;
} else if (c >= 'A' && c <= 'Z') {
// Make lowercase
*d = c - 'A' + 'a';
d++;
}
s++;
}
*d = '\0';
}
Expand Down
7 changes: 4 additions & 3 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ struct netlink_ifa_message {
char attrbuf[512];
};

static void add_rta_attr(struct netlink_ifa_message *msg, unsigned short rta_type, char *value, unsigned short value_len)
static void add_rta_attr(struct netlink_ifa_message *msg, unsigned short rta_type, char *value,
unsigned short value_len)
{
struct rtattr *rta = (struct rtattr *)(((char *) msg) + NLMSG_ALIGN(msg->nh.nlmsg_len));
rta->rta_type = rta_type;
Expand All @@ -98,7 +99,7 @@ static void configure_loopback(int ifindex)
struct netlink_ifa_message req[2];
memset(&req[0], 0, sizeof(struct netlink_ifa_message));
req[0].nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
req[0].nh.nlmsg_flags = NLM_F_EXCL|NLM_F_CREATE|NLM_F_REQUEST; // NLM_F_ACK
req[0].nh.nlmsg_flags = NLM_F_EXCL | NLM_F_CREATE | NLM_F_REQUEST; // NLM_F_ACK
req[0].nh.nlmsg_type = RTM_NEWADDR;

req[0].ifa.ifa_family = AF_INET;
Expand All @@ -115,7 +116,7 @@ static void configure_loopback(int ifindex)

memset(&req[1], 0, sizeof(struct netlink_ifa_message));
req[1].nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
req[1].nh.nlmsg_flags = NLM_F_EXCL|NLM_F_CREATE|NLM_F_REQUEST; // NLM_F_ACK
req[1].nh.nlmsg_flags = NLM_F_EXCL | NLM_F_CREATE | NLM_F_REQUEST; // NLM_F_ACK
req[1].nh.nlmsg_type = RTM_NEWADDR;
req[1].ifa.ifa_family = AF_INET6;
req[1].ifa.ifa_prefixlen = 128;
Expand Down
2 changes: 1 addition & 1 deletion src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ enum erlinit_option_value {
// readable to use the long option form.
OPT_BOOT = 'b',
OPT_CTTY = 'c',
OPT_UNIQUEID_EXEC= 'd',
OPT_UNIQUEID_EXEC = 'd',
OPT_ENV = 'e',
OPT_HANG_ON_EXIT = 'h',
OPT_LIMIT = 'l',
Expand Down
2 changes: 1 addition & 1 deletion src/rootdisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static int directory_filter(const struct dirent *d)
}

static void scan_for_partitions(struct block_device_info *parent,
struct block_device_info **infos)
struct block_device_info **infos)
{
char base_dir[ERLINIT_PATH_MAX];
snprintf(base_dir, sizeof(base_dir), "/sys/block/%s", parent->name);
Expand Down
18 changes: 11 additions & 7 deletions src/shutdown_report.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
static const char *reboot_cmd(int cmd)
{
switch (cmd) {
case LINUX_REBOOT_CMD_RESTART: return "restart";
case LINUX_REBOOT_CMD_HALT: return "halt";
case LINUX_REBOOT_CMD_POWER_OFF: return "power off";
default: return "unknown";
case LINUX_REBOOT_CMD_RESTART:
return "restart";
case LINUX_REBOOT_CMD_HALT:
return "halt";
case LINUX_REBOOT_CMD_POWER_OFF:
return "power off";
default:
return "unknown";
}
}

Expand Down Expand Up @@ -95,17 +99,17 @@ static void report_exit_info(FILE *fp, const struct erlinit_exit_info *exit_info
fprintf(fp, "N/A\n");

if (WIFEXITED(exit_info->wait_status))
fprintf(fp, "Erlang exit status: %d\n", WEXITSTATUS(exit_info->wait_status));
fprintf(fp, "Erlang exit status: %d\n", WEXITSTATUS(exit_info->wait_status));
if (WIFSIGNALED(exit_info->wait_status))
fprintf(fp, "Erlang exited due to signal: %d\n", WTERMSIG(exit_info->wait_status));
fprintf(fp, "Erlang exited due to signal: %d\n", WTERMSIG(exit_info->wait_status));
fprintf(fp, "Shutdown action: %s\n", reboot_cmd(exit_info->desired_reboot_cmd));
}

static void report_dmesg(FILE *fp)
{
fprintf(fp, "\n## dmesg\n\n");

int fd = open("/dev/kmsg", O_RDONLY|O_NONBLOCK);
int fd = open("/dev/kmsg", O_RDONLY | O_NONBLOCK);
if (fd < 0) {
fprintf(fp, "Error opening /dev/kmsg: %s\n", strerror(errno));
return;
Expand Down
28 changes: 20 additions & 8 deletions src/term.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,25 @@ static int lookup_tty_options(const char *options, speed_t *speed)
// NOTE: Only setting the speed is supported now. No parity and 8 bits are assumed.
unsigned long baud = strtoul(options, NULL, 10);
switch (baud) {
case 9600: *speed = B9600; break;
case 19200: *speed = B19200; break;
case 38400: *speed = B38400; break;
case 57600: *speed = B57600; break;
case 115200: *speed = B115200; break;
case 9600:
*speed = B9600;
break;
case 19200:
*speed = B19200;
break;
case 38400:
*speed = B38400;
break;
case 57600:
*speed = B57600;
break;
case 115200:
*speed = B115200;
break;
default:
warn("Couldn't parse tty option '%s'. Defaulting to 115200n8", options);
*speed = B115200; break;
*speed = B115200;
break;
}
return 0;
}
Expand Down Expand Up @@ -153,7 +164,7 @@ static void init_terminal(int fd)
termios.c_cflag |= CREAD; // Enable receiver
termios.c_oflag = 0;
termios.c_lflag = 0;
termios.c_iflag &= ~(ICRNL|INLCR); // No CR<->LF conversions
termios.c_iflag &= ~(ICRNL | INLCR); // No CR<->LF conversions

termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0;
Expand All @@ -179,7 +190,8 @@ void set_ctty()
strcat(&ttypath[TTY_PREFIX_LENGTH], options.controlling_terminal);
} else {
// Pick the active console(s)
if (readsysfs(SYSFS_ACTIVE_CONSOLE, &ttypath[TTY_PREFIX_LENGTH], sizeof(ttypath) - TTY_PREFIX_LENGTH) == 0) {
if (readsysfs(SYSFS_ACTIVE_CONSOLE, &ttypath[TTY_PREFIX_LENGTH],
sizeof(ttypath) - TTY_PREFIX_LENGTH) == 0) {
// No active console?
warn("no active consoles found!");
return;
Expand Down

0 comments on commit 4ccc19f

Please sign in to comment.