Line data Source code
1 : #include "fd_linux_bond.h"
2 :
3 : #include <errno.h>
4 : #include <stdio.h>
5 : #include <sys/stat.h>
6 :
7 : int
8 0 : fd_bonding_is_master( char const * device ) {
9 0 : char path[ PATH_MAX ];
10 0 : FD_TEST( fd_cstr_printf_check( path, PATH_MAX, NULL, "/sys/class/net/%s/bonding", device ) );
11 0 : struct stat st;
12 0 : int err = stat( path, &st );
13 0 : if( FD_UNLIKELY( err && errno != ENOENT ) )
14 0 : FD_LOG_ERR(( "error checking if device `%s` is bonded, stat(%s) failed (%i-%s)",
15 0 : device, path, errno, fd_io_strerror( errno ) ));
16 0 : return !err;
17 0 : }
18 :
19 : static void
20 : read_slaves( char const * device,
21 0 : char output[ 4096 ] ) {
22 0 : char path[ PATH_MAX ];
23 0 : FD_TEST( fd_cstr_printf_check( path, PATH_MAX, NULL, "/sys/class/net/%s/bonding/slaves", device ) );
24 :
25 0 : FILE * fp = fopen( path, "r" );
26 0 : if( FD_UNLIKELY( !fp ) )
27 0 : FD_LOG_ERR(( "error configuring network device, fopen(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
28 0 : if( FD_UNLIKELY( !fgets( output, 4096, fp ) ) )
29 0 : FD_LOG_ERR(( "error configuring network device, fgets(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
30 0 : if( FD_UNLIKELY( feof( fp ) ) ) FD_LOG_ERR(( "error configuring network device, fgets(%s) failed (EOF)", path ));
31 0 : if( FD_UNLIKELY( ferror( fp ) ) ) FD_LOG_ERR(( "error configuring network device, fgets(%s) failed (error)", path ));
32 0 : if( FD_UNLIKELY( strlen( output ) == 4095 ) ) FD_LOG_ERR(( "line too long in `%s`", path ));
33 0 : if( FD_UNLIKELY( strlen( output ) == 0 ) ) FD_LOG_ERR(( "line empty in `%s`", path ));
34 0 : if( FD_UNLIKELY( fclose( fp ) ) )
35 0 : FD_LOG_ERR(( "error configuring network device, fclose(%s) failed (%i-%s)", path, errno, fd_io_strerror( errno ) ));
36 0 : output[ strlen( output ) - 1 ] = '\0';
37 0 : }
38 :
39 : fd_bonding_slave_iter_t *
40 : fd_bonding_slave_iter_init(
41 : fd_bonding_slave_iter_t * iter,
42 : char const * device
43 0 : ) {
44 0 : read_slaves( device, iter->line );
45 0 : iter->saveptr = NULL;
46 0 : iter->tok = strtok_r( iter->line, " \t", &iter->saveptr );
47 0 : return iter;
48 0 : }
49 :
50 : int
51 0 : fd_bonding_slave_iter_done( fd_bonding_slave_iter_t const * iter ) {
52 0 : return !iter->tok;
53 0 : }
54 :
55 : void
56 : fd_bonding_slave_iter_next(
57 : fd_bonding_slave_iter_t * iter
58 0 : ) {
59 0 : iter->tok = strtok_r( NULL, " \t", &iter->saveptr );
60 0 : }
61 :
62 : char const *
63 : fd_bonding_slave_iter_ele(
64 : fd_bonding_slave_iter_t const * iter
65 0 : ) {
66 0 : return iter->tok;
67 0 : }
68 :
69 : uint
70 0 : fd_bonding_slave_cnt( char const * device ) {
71 0 : fd_bonding_slave_iter_t iter_[1];
72 0 : uint cnt = 0U;
73 0 : for( fd_bonding_slave_iter_t * iter = fd_bonding_slave_iter_init( iter_, device );
74 0 : !fd_bonding_slave_iter_done( iter );
75 0 : fd_bonding_slave_iter_next( iter ) ) {
76 0 : cnt++;
77 0 : }
78 0 : return cnt;
79 0 : }
|