// my_parcel_restart : parcellite が起動しているか監視し、していなければ起動する
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define APP_NAME "my_parcel_restart"
#define CHECK_INTERVAL (2) // 何秒おきにチェックするか
#define NO_KIDOU_LEN (1) // TEMP_FILEの1行目(pid)がこれ未満の文字長だったら
// processが起動していないとみなす
#define TEMP_FILE "/tmp/"APP_NAME".tmp"
//------------------------------------------------------------------------------
// TEMP_FILEから1行読み込む
// [in] d : 入れ先
// s : 入れ先のサイズ
// [out] 0 : 正常終了
// 1 : file open error
// 2 : 1行も無かった
// 3 : 引数エラー
//------------------------------------------------------------------------------
int get_one_line( char * d, int s)
{
FILE * fp;
int len;
if ( d == NULL || s < 2 ) {
return 3 ;
}
if ( NULL
== ( fp
= fopen ( TEMP_FILE
, "r" ) ) ) { return 1 ;
}
if ( NULL
== fgets ( d
, s
, fp
) ) { return 2 ;
}
if ( len) {
if ( d[ len - 1 ] == '\n ' ) {
len--;
d[ len] = '\0 ' ;
}
}
if ( len < 1 ) {
return 2 ;
}
return 0 ;
}
//------------------------------------------------------------------------------
// 指定した名前のプロセスの起動状況を返す
// 0 : 起動していない
// 1 : 1個起動している
// 2 : 2個以上起動している
//------------------------------------------------------------------------------
int count_kidou( char * proc_name)
{
char buff[ 256 ] = "pidof " ;
strcat ( buff
, " > " TEMP_FILE
) ; if ( get_one_line( buff, sizeof ( buff) ) ) {
return 0 ;
}
if ( NO_KIDOU_LEN
> strlen ( buff
) ) { return 0 ;
}
if ( NULL
!= strstr ( buff
, " " ) ) { return 2 ;
}
return 1 ;
}
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main( )
{
int res;
// title
fprintf ( stderr
, "\n " APP_NAME
" : parcellite が起動しているか監視し、していなければ起動する\n \n " ) ; // 自分起動チェック
if ( count_kidou( APP_NAME) > 1 ) {
fprintf ( stderr
, " すでに自分(" APP_NAME
")が起動していた\n " ) ; return 0 ;
}
// mugen loop
while ( 1 ) {
res = count_kidou( "parcellite" ) ;
switch ( res) {
case 0 : // parcellite が起動していない
// parcellite を起動
break ;
case 1 : // parcellite が1個起動中
break ;
case 2 : // parcellite が複数起動していた
// 現在の parcellite を全て kill して 新しい parcellite を起動
system ( "kill -9 $(pidof parcellite) ; parcellite &" ) ; break ;
default :
break ;
}
sleep( CHECK_INTERVAL) ;
}
// end
return 0 ;
}
Ly8gbXlfcGFyY2VsX3Jlc3RhcnQgOiBwYXJjZWxsaXRlIOOBjOi1t+WLleOBl+OBpuOBhOOCi+OBi+ebo+imluOBl+OAgeOBl+OBpuOBhOOBquOBkeOCjOOBsOi1t+WLleOBmeOCiwojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8c3RyaW5nLmg+CiNpbmNsdWRlIDx1bmlzdGQuaD4KCiNkZWZpbmUgQVBQX05BTUUgICAgICAgICJteV9wYXJjZWxfcmVzdGFydCIKI2RlZmluZSBDSEVDS19JTlRFUlZBTCAgKDIpICAgICAvLyDkvZXnp5LjgYrjgY3jgavjg4Hjgqfjg4Pjgq/jgZnjgovjgYsKI2RlZmluZSBOT19LSURPVV9MRU4gICAgKDEpICAgICAvLyBURU1QX0ZJTEXjga4x6KGM55uuKHBpZCnjgYzjgZPjgozmnKrmuoDjga7mloflrZfplbfjgaDjgaPjgZ/jgokKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLyBwcm9jZXNz44GM6LW35YuV44GX44Gm44GE44Gq44GE44Go44G/44Gq44GZCiNkZWZpbmUgVEVNUF9GSUxFICAgICAgICIvdG1wLyJBUFBfTkFNRSIudG1wIgoKLy8tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KLy8gIFRFTVBfRklMReOBi+OCiTHooYzoqq3jgb/ovrzjgoAKLy8gICAgICBbaW5dICAgIGQgOiDlhaXjgozlhYgKLy8gICAgICAgICAgICAgIHMgOiDlhaXjgozlhYjjga7jgrXjgqTjgroKLy8gICAgICBbb3V0XSAgIDAgOiDmraPluLjntYLkuoYKLy8gICAgICAgICAgICAgIDEgOiBmaWxlIG9wZW4gZXJyb3IKLy8gICAgICAgICAgICAgIDIgOiAx6KGM44KC54Sh44GL44Gj44GfCi8vICAgICAgICAgICAgICAzIDog5byV5pWw44Ko44Op44O8Ci8vLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCmludCBnZXRfb25lX2xpbmUoY2hhciAqZCwgaW50IHMpCnsKICAgIEZJTEUgKmZwOwogICAgaW50IGxlbjsKICAgIGlmIChkID09IE5VTEwgfHwgcyA8IDIpIHsKICAgICAgICByZXR1cm4gMzsKICAgIH0KICAgIGlmIChOVUxMID09IChmcCA9IGZvcGVuKFRFTVBfRklMRSwgInIiKSkpIHsKICAgICAgICByZXR1cm4gMTsKICAgIH0KICAgIGlmIChOVUxMID09IGZnZXRzKGQsIHMsIGZwKSkgewogICAgICAgIGZjbG9zZShmcCk7CiAgICAgICAgcmV0dXJuIDI7CiAgICB9CiAgICBmY2xvc2UoZnApOwogICAgbGVuID0gc3RybGVuKGQpOwogICAgaWYgKGxlbikgewogICAgICAgIGlmIChkW2xlbiAtIDFdID09ICdcbicpIHsKICAgICAgICAgICAgbGVuLS07CiAgICAgICAgICAgIGRbbGVuXSA9ICdcMCc7CiAgICAgICAgfQogICAgfQogICAgaWYgKGxlbiA8IDEpIHsKICAgICAgICByZXR1cm4gMjsKICAgIH0KICAgIHJldHVybiAwOwp9CgovLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQovLyAg5oyH5a6a44GX44Gf5ZCN5YmN44G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RSAiIDogcGFyY2VsbGl0ZSDjgYzotbfli5XjgZfjgabjgYTjgovjgYvnm6PoppbjgZfjgIHjgZfjgabjgYTjgarjgZHjgozjgbDotbfli5XjgZnjgotcblxuIik7CiAgICAvLyDoh6rliIbotbfli5Xjg4Hjgqfjg4Pjgq8KICAgIGlmIChjb3VudF9raWRvdShBUFBfTkFNRSkgPiAxKSB7CiAgICAgICAgZnByaW50ZihzdGRlcnIsICIgIOOBmeOBp+OBq+iHquWIhigiIEFQUF9OQU1FICIp44GM6LW35YuV44GX44Gm44GE44GfXG4iKTsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIC8vIG11Z2VuIGxvb3AKICAgIHdoaWxlICgxKSB7CiAgICAgICAgcmVzID0gY291bnRfa2lkb3UoInBhcmNlbGxpdGUiKTsKICAgICAgICBzd2l0Y2ggKHJlcykgewogICAgICAgIGNhc2UgMDogICAgICAgICAgICAgICAgLy8gcGFyY2VsbGl0ZSDjgYzotbfli5XjgZfjgabjgYTjgarjgYQKICAgICAgICAgICAgLy8gcGFyY2VsbGl0ZSDjgpLotbfli5UKICAgICAgICAgICAgc3lzdGVtKCJwYXJjZWxsaXRlICYiKTsKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSAxOiAgICAgICAgICAgICAgICAvLyBwYXJjZWxsaXRlIOOBjDHlgIvotbfli5XkuK0KICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgY2FzZSAyOiAgICAgICAgICAgICAgICAvLyBwYXJjZWxsaXRlIOOBjOikh+aVsOi1t+WLleOBl+OBpuOBhOOBnwogICAgICAgICAgICAvLyDnj77lnKjjga4gcGFyY2VsbGl0ZSDjgpLlhajjgaYga2lsbCDjgZfjgaYg5paw44GX44GEIHBhcmNlbGxpdGUg44KS6LW35YuVCiAgICAgICAgICAgIHN5c3RlbSgia2lsbCAtOSAkKHBpZG9mIHBhcmNlbGxpdGUpIDsgcGFyY2VsbGl0ZSAmIik7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIH0KICAgICAgICBzbGVlcChDSEVDS19JTlRFUlZBTCk7CiAgICB9CiAgICAvLyBlbmQKICAgIHJldHVybiAwOwp9Cg==