/uevent.c (e6e7af84e5bd621843bfee6c9907ad85f789dccb) (3592 bytes) (mode 100644) (type blob)

/*******************************************************************************
this code is protected by the GNU affero GPLv3
author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
*******************************************************************************/
#include <blkid.h>

#include <ulinux/compiler_types.h>
#include <ulinux/types.h>
#include <ulinux/error.h>
#include <ulinux/utils/mem.h>
#include <ulinux/utils/ascii/string/string.h>
#include <ulinux/utils/ascii/string/conv/decimal/decimal.h>
#include <ulinux/sysc.h>

#include "out.h"
#include "ulinux_namespace.h"
#include "globals.h"

static u8 hdr_action_is_add(u8 *hdr)
{
  #define HDR_ACTION_ADD "add@"
  if(memcmp(hdr,HDR_ACTION_ADD,sizeof(HDR_ACTION_ADD)-1)) return 0;
  return 1;
}

static void hdr_skip(u8 **p)
{
  loop if(*(*p)++==0) break;
}
#define var_skip hdr_skip

static u8 key_is_subsystem(u8 *p)
{
  #define KEY_SUBSYSTEM "SUBSYSTEM="
  if(memcmp(p,KEY_SUBSYSTEM,sizeof(KEY_SUBSYSTEM)-1)) return 0;
  return 1;
}

static u8 key_is_devname(u8 *p)
{
  #define KEY_DEVNAME "DEVNAME="
  if(memcmp(p,KEY_DEVNAME,sizeof(KEY_DEVNAME)-1)) return 0;
  return 1;
}

static void move_to_value(u8 **p)
{
  loop if(*(*p)++=='=') break;
}

static u8 value_is_block(u8 *p)
{
  #define VALUE_BLOCK "block"
  if(!*p||memcmp(p,VALUE_BLOCK,sizeof(VALUE_BLOCK)-1)) return 0;
  return 1;
}

static void value_skip(u8 **p)
{
  loop if(*(*p)++==0) break;
}

static void value_str_consume(u8 **p,u8 *d)
{
  loop{
    *d++=**p;
    if(*(*p)++==0) break;
  }
}

#define IS_ROOT     1
#define IS_NOT_ROOT 0
/*the block device is in devtmpfs, use it to probe with libblkid*/
static u8 blk_dev_probe(u8 *dev_name)
{
  /*the right way to do it is to query the limits on the /dev file system*/
  u8 dev_path[ROOT_DEV_PATH_SZ]="/dev/";
  blkid_probe pr;
  u8 r0;
  i r1;
  const char *type;
  const char *uuid;

  r0=IS_NOT_ROOT;

  strcat(dev_path,dev_name);

  OUT(PRE "probing %s...\n",dev_path);
  pr=blkid_new_probe_from_filename((const char *)dev_path);
  if(!pr){
    OUT(PRE "failed (unable to create a blkid probe), skipping\n");
    return IS_NOT_ROOT;
  }

  r1=blkid_do_probe(pr);
  if(r1!=0){
    OUT(PRE "failed (unable to perform the blkid probe), skipping\n");
    goto free_probe;
  }

  r1=blkid_probe_lookup_value(pr,"TYPE",&type,0);
  if(r1!=0){
    OUT(PRE "failed (missing blkid type tag), skipping\n");
    goto free_probe;
  }
  OUT(PRE "  type=%s\n",type);

  r1=blkid_probe_lookup_value(pr,"UUID",&uuid,0);
  if(r1!=0){
    OUT(PRE "failed (missing blkid uuid tag), skipping\n");
    goto free_probe;
  }
  OUT(PRE "  uuid=%s\n",uuid);

  if(strcmp(uuid,root_uuid)==0){
    /*found the root filesystem*/
    OUT(PRE "found root\n");
    strcpy(root_dev_path,dev_path);
    strcpy(root_fs_type,type);
    r0=IS_ROOT;
  }
  
  OUT(PRE "done\n");
free_probe: 
  blkid_free_probe(pr);
  return r0;
}

/*here we are actually looking for block devices*/
u8 uevent_process(u8 *p,i sz)
{
  u8 *p_end;
  u8 dev_name[256];/*usually a devtmpfs which has path components of 256 bytes*/
  u8 r;

  r=ROOT_NOT_FOUND;
  if(!hdr_action_is_add(p)) return r;
  p_end=p+sz;
  hdr_skip(&p);

  memset(dev_name,0,sizeof(dev_name));

  loop{
    if(p>=p_end) break;

    if(key_is_subsystem(p)){
      move_to_value(&p);
      if(!value_is_block(p)) goto exit;
      else value_skip(&p);
    }else if(key_is_devname(p)){
      move_to_value(&p);
      value_str_consume(&p,dev_name);
    }else var_skip(&p);
  }

  /*from here, we are dealing with a block device, may be our root*/
  r=blk_dev_probe(dev_name);

exit:
  return r;
}


Mode Type Size Ref File
100644 blob 444 49d5064b5a60c1d457aaf871c71275eb962d83f2 3.16
100644 blob 558 3b1926aa2a9e5e4a8fa5c8a3d178a0ed02a01346 DEPENDENCIES
100644 blob 35147 94a9ed024d3859793618152ea559a168bbcbb5e2 LICENSE.md
100644 blob 768 32cae14d717baa6c88aab11a82663e0a2aefe652 README
100644 blob 644 598bf35b742147b3cc2bc1da0fd6d41aa282e77e TODO
100644 blob 432 2516d063ca695699c1ec3d755ef782bad84f203f cpio.in
100644 blob 587 1890391057120a83a6714310987be3ca82de46cc globals.h
100644 blob 5197 6caef253d4902e1c038135a156d42a847c89dddf init.c
100755 blob 12172 9f4bb09cd1e95ca807848a2445e8580f0a8b0aa6 make
100644 blob 5877 e849ba428752ea61144fe879792c8c68a65e3160 modules.c
100644 blob 503 f478bdeefa4ae3259dedfe0b4f78036492295d27 modules.h
100644 blob 674 7f272e25a25b4da12ad81f13b6eeff1bba4187af out.h
100644 blob 3180 6d142befdbc559aa3b6089443a2988b054bc71de ramfs.c
100644 blob 341 de9218a39777b8dd8feb5ee76cdf41af215ef473 ramfs.h
040000 tree - 87b8a0b9717f8367a1f8427da787968136f8aac9 script
100644 blob 3592 e6e7af84e5bd621843bfee6c9907ad85f789dccb uevent.c
100644 blob 348 cf07be71685703316f9522de37d466034d08e635 uevent.h
100644 blob 3970 cfe47059c2c26697fd02d2f18b0afe68dc7627fd uevents.c
100644 blob 413 896725abf8b5477eb7013e55429aa89a2bf97ed3 uevents.h
040000 tree - 62d63dbff565849898f1b5138d50a03774874906 ulinux
100644 blob 4060 feb12fd4fc27f1442198ea878911269a2b2e1215 ulinux_namespace.h
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/sylware/cinitramfs

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/sylware/cinitramfs

Clone this repository using git:
git clone git://git.rocketgit.com/user/sylware/cinitramfs

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main