2016-08-01

C: How to Detect LD_PRELOAD

As I'm writing a lib to be preloaded under something heavy, I tried to detect if LD_PRELOAD is set or not. For fun.

The easiest way to check is...get the environment variable! It is so straightforward, that I feel the urge to write it down :-)

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
 char *env_val = getenv("LD_PRELOAD");
 if (env_val[0] != 0) // May be set but value is empty.
  printf("LD_PRELOAD active: \"%s\"\n", env_val);
 return 0;
}

Note, this only checks the existence of the variable, not that if it is actually loaded...

2016-07-27

C: Sorted Walk Through Directory Contents

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

void walk_dir(const char *path)
{
 struct dirent **namelist;
 int i, n;

 n = scandir(path, &namelist, NULL/*filter*/, alphasort);
 if (n < 0)
 {
  perror("scandir");
 } else {
  for (i = 0; i < n; i++) {
   printf("Entry: %s\n", namelist[i]->d_name);
   free(namelist[i]);
  }
  free(namelist);
 }
}

int main(void)
{
 walk_dir("/tmp");
 return EXIT_SUCCESS;
}

C: Simple Walk Through Directory Contents

You can easily walk through the entries in a directory. Note, that it's unordered.

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

void walk_dir(const char *path)
{
 struct dirent *entry;
 DIR *dir = opendir(path);

 while ((entry = readdir(dir)) != NULL) {
  printf("Entry: %s\n", entry->d_name);
 }
}

int main(void)
{
 walk_dir("/tmp");
 return EXIT_SUCCESS;
}

2016-07-26

C: Prettyprinted Value aka Controlled Rounding

void fancy(long l, long nearest)
{
 char suffix[2] = "";
 long mod, val = l;
 float f;
 
 if(val < 1500) /* Too small to pretty print. */
 {
  printf("%li B", val);
 } else {
  mod = val % nearest;
  /* Where are we in the range? */
  if(mod >= (nearest - mod))
  { // hi - we are at or above the 50% of the [0-nearest[ range.
   // add the difference to reach upper nearest.
   val += (nearest - mod);
  } else { // lo
   // substract the difference to reach lower nearest.
   val -= mod;
  }
  
  if(val < 1100000L)
  {
   f = (float)val / 1000;
   suffix[0] = 'K';
   
  } else if(val < 1100000000L)
  {
   f = (float)val / 1000000;
   suffix[0] = 'M';
  } else {
   f = (float)val / 1000000000;
   suffix[0] = 'G';
  }
  
  printf("%.2f %sB", f, suffix); // Here you can play with the format.
 }
}

Usage:

 long size = 3141592535L;
 fancy(size, 1000);

2016-07-25

Some Hungarian laws #3

http://net.jogtar.hu/jr/gen/hjegy_doc.cgi?docid=A1200001.TV
2012. évi I. törvény
a munka törvénykönyvéről

7. § (1) Tilos a joggal való visszaélés. E törvény alkalmazásában joggal való visszaélés különösen, ha az mások jogos érdekeinek csorbítására, érdekérvényesítési lehetőségeinek korlátozására, zaklatására, véleménynyilvánításának elfojtására irányul vagy ehhez vezet.

Érdekesség, hogy az említett "mások jogos érdekei"-ben a "mások" vonatkozik a munkaerőpiac többi tagjára (pl. jelen és leendő munkavállalók, tehát en bloc a Piac), illetve a fizetés eltitkolása nem jogszerű érdeke a munkáltatónak, hisz az piackorlátozó hatású.

Azaz például (leendő) munkáltató nem korlátozhatja munkaszerződés-ajánlat nyilvánosságra hozását, ajánlott bér összegének továbbközlését; munkáltatóról (vagy bármi másról) vélemény formálását, mely nem tetszik a munkáltatónak, pl. felvételi folyamat kritizálása, vagy sima leírása (lásd: Glassdoor, fórumok), hiszen az nem a munkája során kerül tudomására.

A probléma az, hogy a jogalkalmazói gyakorlatban viszont a PTK üzleti titok definícióját alkalmazzák, ami szerint a munkabér kifejezetten üzleti titok.

Ám mivel a munkabérrel, kompenzációval kapcsolatban az alkalmazó és az alkalmazott is titokgazda, az alkalmazottak a saját bérezésükkel kapcsolatban nem jogosulatlan személyek, így az üzleti titoksértés nem állhat fent (nincs illetéktelenség).

A Munka Törvénykönyve szerint a munkaszerződés munkaviszonyra vonatkozó szabálytól csak a munkavállaló javára térhet el, így elvileg egy ilyen irányú tiltás alapvetően jogellenes.

Mi hát a megoldás eme szürke zónában Magyarországon? Kinek van jobb ügyvédje, ha a munkáltató pert indít...

2016-05-11

Note to self #4

Next time you screw up your default file extensions as I did (executing a .bat file resulted in an IE's View Downloads window -_- ...), look here for a fix: http://www.winhelponline.com/blog/file-asso-fixes-for-windows-7/

2016-05-09

c: How not to code #1

(gdb) bt
#0  0x00007f8ac115bebe in __lll_lock_wait_private () from /lib64/libc.so.6
#1  0x00007f8ac10f22be in _L_lock_9876 () from /lib64/libc.so.6
#2  0x00007f8ac10f05c1 in free () from /lib64/libc.so.6
#3  0x0000000000402649 in handle_sig (signo=<optimized out>, info=<optimized out>, context=<optimized out>) at lol.c:158
#4  <signal handler called>
#5  0x00007f8ac10edf03 in _int_malloc () from /lib64/libc.so.6
#6  0x00007f8ac10f06b7 in malloc () from /lib64/libc.so.6
#7  0x00000000004015ba in do_the_boogie (fd=3, gp=<optimized out>) at lol.c:715
#8  0x00000000004023cc in main (argc=5, argv=<optimized out>) at lol.c:810

LOL, it is deadlocked. The signal handler (frame #4) was invoked inside a malloc (frame #5), and the signal handler calls free in frame #3. Of course, the heap lock is held in frame #5 and both are in the same memory arena (see: break space), hence we are screwed.

2016-04-22

Note to self #3

Task:
  • Measure network throughput of <your favorite dipshit 3rd party app>
  • Report traffic over granularity periods
  • C99, _GNU_SOURCE
Actions taken:
  • Using libpcap in main thread to capture packets (also filtering, etc)
  • Piping info into another thread (simple pthread usage) that does the math
Problem encountered:
  1. When emitting all the info I read from the pipe to stdout, the average packet count is 2700..3000 packet/s
  2. When only the per GP statistics, the packet capture drops to 3-11 packet/s
Extensive head scratching intensifies.

[few days had passed]

Turned out, if I print all the info, of course it captures more tcp traffic, as I'm over an SSH connection! I'm generating that traffic!!!111one

/me idiot

Don't do Java, kids. It's bad for your brains.

2016-04-06

XML: Can I use it as a database?

A question popped up, that can (and should) we store slightly structured data in XML? For example: results of measurements that do not have any structure except timestamps and named columns.

Well, my opinion matches with this book's ideology:
XML is not a database. It was never meant to be a database. It is never going to be a database. Relational databases are proven technology with more than 20 years of implementation experience. They are solid, stable, useful products. They are not going away. XML is a very useful technology for moving data between different databases or between databases and other programs. However, it is not itself a database. Don't use it like one.

The important point here that you must grasp is, that XML is an encoding. It is a data exchange format, not a data storage format.