#include       /* printf, NULL */
#include       /* O_CREAT, O_RDWR, O_TRUNC */
#include      /* memset */

int main(int argc, char *argv[])
{
        int fd;
        char string[] = "test string\n";
        struct flock lock;

        fd = open("test.txt", O_CREAT | O_RDWR | O_TRUNC, 0664);
        if (fd < 0) return 1;

        memset(&lock, 0, sizeof(lock));
        // 排他鎖,即不允許其他進程再對其加任何類型的鎖,但讀鎖(共享鎖)允許
        lock.l_type   = F_WRLCK;
        lock.l_whence = SEEK_SET;
        lock.l_start  = 0;  // 從文件開頭鎖定
        lock.l_len    = 0;  // 文件全部內容鎖定

        if (fcntl(fd, F_SETLK, &lock) == 0) {
                /* 若為F_SETLKW,這時如果鎖已經被其他進程佔用,則此進程會阻塞直到其他進程釋放鎖 */
                printf("lock success\n");
                printf("press any key to unlock\n");
                getchar();
                lock.l_type = F_UNLCK;
                if (fcntl(fd, F_SETLK, &lock) == 0) {
                        printf("unlock success\n");
                }
                else {
                        printf("unlock fail\n");
                        return 1;
                }
        }
        else {
                if (write(fd, string, strlen(string)) < 0)
                        printf("write fail\n");
                else
                        printf("write success\n");
                printf("lock fail\n");
                return 1;
        }

        // 進程退出會對所有文件解鎖
        return 0;
}

memo

  • fcntl 很單純就是一個利用文件來做同步,並不會鎖住文件的讀寫。
  • fcntl(fd, F_SETLK, &lock) 不會 block process,直接回傳 -1。
  • fcntl(fd, F_SETLKW, &lock) 直接 block process,直接鎖被 unlock,並回傳 0。
  • 上面的程式碼在執行第二個時會直接回傳 -1,並寫入 test string。

Reference


台南小新 發表在 痞客邦 留言(0) 人氣()

udhcpc script enviromental variables

#!/bin/sh
echo 1=$1
echo interface=$interface
echo ip=$ip
echo subnet=$subnet
echo router=$router
echo dns=$dns
echo lease=$lease
echo dhcptype=$dhcptype
echo serverid=$serverid
echo hostname=$hostname
echo domain=$domain
echo mtu=$mtu
echo broadcast=$broadcast
echo message=$message
echo timezone=$timezone
echo timesvr=$timesvr
echo ntpsrv=$ntpsrv
echo namesvr=$namesvr
  • $1 - What action the script should perform
  • interface - The interface this was obtained on
  • ip - The obtained IP
  • subnet - The assigend subnet mask
  • broadcast - The broadcast address for this network
  • router - A list of routers
  • dns - A list of DNS server
  • 執行後會馬上呼叫一次,執行 deconfig 指令。取得 ip 後會呼叫 bound 指令。
# udhcpc -i eth3 -s /tmp/udhcpc.sh -p /var/run/udhcpc.pid &
# udhcpc (v1.12.1) started
1=deconfig
interface=eth3
ip=
subnet=
router=
dns=
lease=
dhcptype=
serverid=
hostname=
domain=
mtu=
broadcast=
message=
timezone=
timesvr=
ntpsrv=
namesvr=
>>>>>>>>>>>
Sending select for 192.168.249.102...
Lease of 192.168.249.102 obtained, lease time 1800
>>>>>>>>>>>
1=bound
interface=eth3
ip=192.168.249.102
subnet=255.255.255.0
router=192.168.249.254
dns=192.168.249.254 8.8.4.4
lease=1800
dhcptype=5
serverid=192.168.249.252
hostname=
domain=
mtu=
broadcast=
message=
timezone=
timesvr=
ntpsrv=
namesvr=

udhcpc script example

#!/bin/sh
# 檢查是否有來自 udhcpc 的指令
[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
# 設定 ifconfig 參數 
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
 
case "$1" in
  deconfig)
    /sbin/ifconfig $interface 0.0.0.0
    ;;
 
  renew|bound)
    # 設定 IP
    /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
    # 設定 routing table
    if [ -n "$router" ] ; then
      echo "deleting routers"
      while route del default gw 0.0.0.0 dev $interface ; do
        :
      done
      for i in $router ; do
        route add default gw $i dev $interface
      done
    fi
    # 設定 dns server
    echo -n > "/etc/resolv.conf"
    [ -n "$domain" ] && echo search $domain >> "/etc/resolv.conf"
    for i in $dns ; do
      echo adding dns $i
      echo nameserver $i >> "/etc/resolv.conf"
    done
    ;;
esac

exit 0

Reference


台南小新 發表在 痞客邦 留言(0) 人氣()

程式碼

#include 
#include 

int split(char **arr, int arr_size, char *str, const char *del) {
        int total=0;
        char *s = strtok(str, del);

        while(s != NULL) {
                *arr++ = s;
                if (++total >= arr_size) break;
                s = strtok(NULL, del);
        }
        return total;
}

int main()
{
        char str[] = "10,20,30";
        char *arr[3];
        const char *del = ",";
        int i = 0;
        int total=split(arr, 2, str, del);

        while(i

結果

台南小新 發表在 痞客邦 留言(0) 人氣()

Ubuntu >= 15
nmcli device show <interfacename> | grep IP4.DNS

台南小新 發表在 痞客邦 留言(0) 人氣()

  • vtysh -d bgpd
    只針對 bgpd 溝通
  • write terminal

台南小新 發表在 痞客邦 留言(0) 人氣()