1 /*******************************************************************************
2 this code is protected by the GNU affero GPLv3
3 author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
4 *******************************************************************************/
7 #include <ulinux/compiler_types.h>
8 #include <ulinux/types.h>
9 #include <ulinux/error.h>
10 #include <ulinux/utils/mem.h>
11 #include <ulinux/utils/ascii/string/string.h>
12 #include <ulinux/utils/ascii/string/conv/decimal/decimal.h>
13 #include <ulinux/sysc.h>
16 #include "ulinux_namespace.h"
19 static u8
hdr_action_is_add(u8
*hdr
)
21 #define HDR_ACTION_ADD "add@"
22 if(memcmp(hdr
,HDR_ACTION_ADD
,sizeof(HDR_ACTION_ADD
)-1)) return 0;
26 static void hdr_skip(u8
**p
)
28 loop
if(*(*p
)++==0) break;
30 #define var_skip hdr_skip
32 static u8
key_is_subsystem(u8
*p
)
34 #define KEY_SUBSYSTEM "SUBSYSTEM="
35 if(memcmp(p
,KEY_SUBSYSTEM
,sizeof(KEY_SUBSYSTEM
)-1)) return 0;
39 static u8
key_is_devname(u8
*p
)
41 #define KEY_DEVNAME "DEVNAME="
42 if(memcmp(p
,KEY_DEVNAME
,sizeof(KEY_DEVNAME
)-1)) return 0;
46 static void move_to_value(u8
**p
)
48 loop
if(*(*p
)++=='=') break;
51 static u8
value_is_block(u8
*p
)
53 #define VALUE_BLOCK "block"
54 if(!*p
||memcmp(p
,VALUE_BLOCK
,sizeof(VALUE_BLOCK
)-1)) return 0;
58 static void value_skip(u8
**p
)
60 loop
if(*(*p
)++==0) break;
63 static void value_str_consume(u8
**p
,u8
*d
)
73 /*the block device is in devtmpfs, use it to probe with libblkid*/
74 static u8
blk_dev_probe(u8
*dev_name
)
76 /*the right way to do it is to query the limits on the /dev file system*/
77 u8 dev_path
[ROOT_DEV_PATH_SZ
]="/dev/";
86 strcat(dev_path
,dev_name
);
88 OUT(PRE
"probing %s...\n",dev_path
);
89 pr
=blkid_new_probe_from_filename((const char *)dev_path
);
91 OUT(PRE
"failed (unable to create a blkid probe), skipping\n");
95 r1
=blkid_do_probe(pr
);
97 OUT(PRE
"failed (unable to perform the blkid probe), skipping\n");
101 r1
=blkid_probe_lookup_value(pr
,"TYPE",&type
,0);
103 OUT(PRE
"failed (missing blkid type tag), skipping\n");
106 OUT(PRE
" type=%s\n",type
);
108 r1
=blkid_probe_lookup_value(pr
,"UUID",&uuid
,0);
110 OUT(PRE
"failed (missing blkid uuid tag), skipping\n");
113 OUT(PRE
" uuid=%s\n",uuid
);
115 if(strcmp(uuid
,root_uuid
)==0){
116 /*found the root filesystem*/
117 OUT(PRE
"found root\n");
118 strcpy(root_dev_path
,dev_path
);
119 strcpy(root_fs_type
,type
);
125 blkid_free_probe(pr
);
129 /*here we are actually looking for block devices*/
130 u8
uevent_process(u8
*p
,i sz
)
133 u8 dev_name
[256];/*usually a devtmpfs which has path components of 256 bytes*/
137 if(!hdr_action_is_add(p
)) return r
;
141 memset(dev_name
,0,sizeof(dev_name
));
146 if(key_is_subsystem(p
)){
148 if(!value_is_block(p
)) goto exit
;
150 }else if(key_is_devname(p
)){
152 value_str_consume(&p
,dev_name
);
156 /*from here, we are dealing with a block device, may be our root*/
157 r
=blk_dev_probe(dev_name
);