Skip to content

Commit

Permalink
Move the snprintf_lkp() (and delay_sec()) routines
Browse files Browse the repository at this point in the history
from the convenient.h hdr to the klib.c (klib.h) 'lib' files
  • Loading branch information
kaiwan committed Jan 22, 2024
1 parent f67c30b commit 624c1dc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 56 deletions.
55 changes: 0 additions & 55 deletions convenient.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,32 +279,6 @@ static inline void beep(int what)
}
/*------------------------------------------------------------------------*/

#ifdef __KERNEL__
void delay_sec(long);
/*------------ delay_sec --------------------------------------------------
* Delays execution for @val seconds.
* The fact is, it's unnecessary (just a demo): the kernel already has the
* ssleep() inline function (a simple wrapper over the msleep()).
*
* Parameters:
* @val : number of seconds to sleep for; if -1, sleep forever
* MUST be called from process context.
* (We deliberately do not inline this function; this way, we can see it's
* entry within a kernel stack call trace).
*/
void delay_sec(long val)
{
asm (""); // force the compiler to not inline it!
if (in_task()) {
set_current_state(TASK_INTERRUPTIBLE);
if (val == -1)
schedule_timeout(MAX_SCHEDULE_TIMEOUT);
else
schedule_timeout(val * HZ);
}
}
#endif /* #ifdef __KERNEL__ */

#ifdef __KERNEL__
/*------------------------ SHOW_DELTA() macro -------------------------
* Show the difference between the timestamps passed
Expand Down Expand Up @@ -335,33 +309,4 @@ void delay_sec(long val)
pr_warn("SHOW_DELTA(): *invalid* earlier > later? (check order of params)\n"); \
} while (0)
#endif /* #ifdef __KERNEL__ */

/*
* Simple wrapper over the snprintf() - a bit more security-aware.
* Checks for and warns on overflow
*/
int snprintf_lkp(char *buf, size_t maxsize, const char *fmt, ...)
{
va_list args;
int n;

#ifndef __KERNEL__
#include <stdarg.h>
#endif
va_start(args, fmt);
n = vsnprintf(buf, maxsize, fmt, args);
va_end(args);
if (n >= maxsize) {
#ifdef __KERNEL__
pr_warn("snprintf(): possible overflow! (maxsize=%lu, ret=%d)\n",
maxsize, n);
dump_stack();
#else
fprintf(stderr, "snprintf(): possible overflow! (maxsize=%lu, ret=%d)\n",
maxsize, n);
#endif
}
return n;
}

#endif /* #ifndef __LKP_CONVENIENT_H__ */
51 changes: 51 additions & 0 deletions klib.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@
*/
#include "klib.h"

/*
* Simple wrapper over the snprintf() - a bit more security-aware.
* Checks for and warns on overflow
*/
int snprintf_lkp(char *buf, size_t maxsize, const char *fmt, ...)
{
va_list args;
int n;

#ifndef __KERNEL__
#include <stdarg.h>
#endif
va_start(args, fmt);
n = vsnprintf(buf, maxsize, fmt, args);
va_end(args);
if (n >= maxsize) {
#ifdef __KERNEL__
pr_warn("snprintf(): possible overflow! (maxsize=%lu, ret=%d)\n",
maxsize, n);
dump_stack();
#else
fprintf(stderr, "snprintf(): possible overflow! (maxsize=%lu, ret=%d)\n",
maxsize, n);
#endif
}
return n;
}

/* minsysinfo:
* Similar to our ch5/min_sysinfo code; it's just simpler (avoiding deps) to
* package this code into this small 'library' of sorts rather than to use it
Expand Down Expand Up @@ -192,3 +220,26 @@ void show_sizeof(void)
sizeof(long), sizeof(long long), sizeof(void *),
sizeof(float), sizeof(double), sizeof(long double));
}

/*------------ delay_sec --------------------------------------------------
* Delays execution for @val seconds.
* The fact is, it's unnecessary (just a demo): the kernel already has the
* ssleep() inline function (a simple wrapper over the msleep()).
*
* Parameters:
* @val : number of seconds to sleep for; if -1, sleep forever
* MUST be called from process context.
* (We deliberately do not inline this function; this way, we can see it's
* entry within a kernel stack call trace).
*/
void delay_sec(long val)
{
asm (""); // force the compiler to not inline it!
if (in_task()) {
set_current_state(TASK_INTERRUPTIBLE);
if (val == -1)
schedule_timeout(MAX_SCHEDULE_TIMEOUT);
else
schedule_timeout(val * HZ);
}
}
3 changes: 2 additions & 1 deletion klib.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/io.h> /* virt_to_phys(), phys_to_virt(), ... */
#include "convenient.h" /* snprintf_lkp() wrapper */

int snprintf_lkp(char *buf, size_t maxsize, const char *fmt, ...);
void minsysinfo(void);
u64 powerof(int base, int exponent);
void show_phy_pages(void *kaddr, size_t len, bool contiguity_check);
void show_sizeof(void);
void delay_sec(long);

#endif

0 comments on commit 624c1dc

Please sign in to comment.