Skip to content

Commit

Permalink
Minor codingstyle fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Nagy committed Jun 13, 2017
1 parent 164874e commit 99b14a0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Author: Peter Nagy
#
# File: Makefile
# Project: Simple linux daemon
# Popis: Daemon listening on specific port
# Date: 12.6.2017
# Author: Peter Nagy
#
# File: Makefile
# Project: Simple linux daemon
# Popis: Daemon listening on specific port
# Date: 12.6.2017

FILE = daemon
FILE_SOURCES = daemon.c
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# simple_linux_daemon
# Simple linux daemon example

This is an example of simple linux daemon implementation. Daemon is listening on TCP port 5001. It behaves as multithread server.

## Available commands:

* cpu - System CPU usage
* mem - System memory usage

## TODO:

* CPU usage measure as separate thread measuring CPU usage in time intervals
* More sophisticated signal handling
* Server running in loop - not just one command
11 changes: 6 additions & 5 deletions daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <arpa/inet.h>
#include <syslog.h>

/* Macro for unused variables */
#define UNUSED(x) (void)(x)

/* Error codes */
Expand Down Expand Up @@ -50,10 +51,10 @@ struct sys_cpu_info {
long long guest_nice;
};

/* Prev version of measured CPU times */
long long prev_cpu_idle_time = 0;
long long prev_cpu_non_idle_time = 0;


/* Memory info - parsed from /proc/meminfo */
struct sys_mem_info {
unsigned long mem_total;
Expand All @@ -63,7 +64,6 @@ struct sys_mem_info {
unsigned long mem_cached;
};


/**
* @brief Gets the total CPU usage percentage computed from /proc/stat file
* @return The total CPU usage
Expand All @@ -72,6 +72,8 @@ char *get_cpu_usage()
{
char *result;
struct sys_cpu_info cpuinfo;

/* Open /proc/stat file */
FILE *cpu_f = fopen("/proc/stat", "r");
if (cpu_f == NULL) {
syslog(LOG_ERR, "Could not open /proc/stat file");
Expand All @@ -88,6 +90,7 @@ char *get_cpu_usage()
}
fclose(cpu_f);

/* Parse first line of /proc/stat file */
sscanf(buffer,
"cpu %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld",
&cpuinfo.user, &cpuinfo.nice, &cpuinfo.system, &cpuinfo.idle, &cpuinfo.iowait,
Expand Down Expand Up @@ -141,7 +144,6 @@ int get_mem_usage_num(char *line)
return result;
}


/**
* @brief Gets the memory usage
* @return The memory usage or NULL in case of error
Expand All @@ -155,6 +157,7 @@ char *get_memory_usage()
int mem_used;
char *result;

/* Open /proc/meminfo file */
if ((mem_f = fopen("/proc/meminfo", "r")) == NULL) {
syslog(LOG_ERR, "Could not open /proc/meminfo file");
return NULL;
Expand Down Expand Up @@ -195,7 +198,6 @@ char *get_memory_usage()
return result;
}


/**
* @brief Parse and execute received command
* @param buffer Buffer containing received buffer
Expand Down Expand Up @@ -295,7 +297,6 @@ static void daemonize()
openlog("simple_linux_daemon", LOG_PID, LOG_DAEMON);
}


/**
* @brief Handles a TCP connection properties
* @param[in] port_num The port number
Expand Down

0 comments on commit 99b14a0

Please sign in to comment.