sntp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /******************************************************************************
  2. 版权所有:
  3. 文件名称: SNTP.c
  4. 文件版本: 01.01
  5. 创建作者: xxxxxx
  6. 创建日期: 2015-02-10
  7. 功能说明: SNTP对时处理
  8. 其它说明:
  9. 修改记录:
  10. */
  11. #include "head.h"
  12. #ifdef SNTP_FUNC
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/fs.h>
  18. #include <linux/sched.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kthread.h>
  21. #include <linux/delay.h>
  22. #include <linux/wait.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/io.h>
  25. #include <asm/ioctls.h>
  26. //#include <asm/semaphore.h>
  27. #include <linux/if.h>
  28. #include <linux/tcp.h>
  29. #include <termios.h>
  30. #include <linux/syscalls.h>
  31. #include "sntp.h"
  32. #define SNTP_IS_NTPD
  33. #ifdef SNTP_IS_NTPD
  34. //应用程序名称
  35. #define SNTP_PROGNAME "ntpd"
  36. #else
  37. #define SNTP_PROGNAME "sntp"
  38. #endif
  39. //sntp同步参数数据结构
  40. struct sntp_para
  41. {
  42. unsigned int serverip; //服务器IP
  43. int tm; //同步钟间隔
  44. };
  45. //sntp同步参数
  46. static struct sntp_para g_sntp_para = {0, 0};
  47. //completion变量,用于等待任务退出
  48. static struct completion g_exit_sntp_proc;
  49. //修改参数标志
  50. static int g_sntp_paraflag = 0;
  51. //参数初始化
  52. static int g_sntp_para_init = 0;
  53. int main_mod_is_exit(void);
  54. /******************************************************************************
  55. 函数名称: sntp_run
  56. 函数版本: 01.01
  57. 创建作者: xxxxxx
  58. 创建日期: 2015-02-10
  59. 函数说明: 运行ntpd,需要指定服务器地址。
  60. 参数说明: 无
  61. 返回值: 成功返回0,失败返回负数。
  62. 修改记录:
  63. */
  64. // sunxi 20201027 added start sntp时间同步标志
  65. static unsigned char sntp_time_synchronized = 0;
  66. unsigned char sntp_get_time_sync_flag(void)
  67. {
  68. unsigned char sync = sntp_time_synchronized;
  69. if(sntp_time_synchronized)
  70. sntp_time_synchronized = 0;
  71. return sync;
  72. }
  73. // sunxi 20201027 added end sntp时间同步标志
  74. static int sntp_run(void)
  75. {
  76. int i = 0, j = 0;
  77. char buf[16];
  78. int ret = 0;
  79. int tm = g_sntp_para.tm;
  80. unsigned int serverip = g_sntp_para.serverip;
  81. struct task_struct *task; // sunxi 20201027 added
  82. unsigned char sync = 0;
  83. #ifdef SNTP_IS_NTPD
  84. char cmd_path[] = "/usr/sbin/ntpd";
  85. char* cmd_argv[] = {cmd_path, "-p", buf, "-q", NULL};
  86. char* cmd_envp[] = {NULL};
  87. #else
  88. char cmd_path[] = "/app/sntp";
  89. char* cmd_argv[] = {cmd_path, "-s", buf, NULL};
  90. char* cmd_envp[] = {NULL};
  91. #endif
  92. sprintf(buf, "%d.%d.%d.%d", (serverip>>24) & 0xFF, (serverip>>16) & 0xFF, (serverip>>8) & 0xFF, (serverip>>0) & 0xFF);
  93. sntp_time_synchronized = 0; // sunxi 20201027 added
  94. // sunxi 20201027 ret = call_usermodehelper(cmd_path, cmd_argv, cmd_envp, UMH_NO_WAIT);
  95. ret = call_usermodehelper(cmd_path, cmd_argv, cmd_envp, UMH_WAIT_PROC);
  96. //已经同步时钟,置参数未修改标志
  97. g_sntp_paraflag = 0;
  98. for(i=0; i<tm; i++)
  99. {
  100. for(j=0; j<10; j++)
  101. {
  102. //检查程序是否退出
  103. if(main_mod_is_exit())
  104. {
  105. complete_and_exit(&g_exit_sntp_proc, 0);
  106. }
  107. //检查是否修改了参数
  108. if(g_sntp_paraflag)
  109. {
  110. sntp_time_synchronized = 0;// sunxi 20201027 added 修改了参数后,对时成功标识重新清零。
  111. return 0;
  112. }
  113. // sunxi 20201027 added start 判断 ntpd 程序是否退出了,退出了说明对时成功了
  114. if(!sync)
  115. {
  116. //当前进程遍历起点
  117. task = current;
  118. //遍历进程链表,找到进程name
  119. do {
  120. if(strncmp(task->comm, "ntpd", strlen("ntpd"))==0)
  121. break;
  122. } while( (task = next_task(task)) != current );
  123. if(task == current)
  124. {
  125. sync = 1;
  126. sntp_time_synchronized = 1;
  127. }
  128. }
  129. // sunxi 20201027 added end 判断 ntpd 程序是否退出了,退出了说明对时成功了
  130. msleep(100);
  131. }
  132. }
  133. return 0;
  134. }
  135. /******************************************************************************
  136. 函数名称: sntp_check_run_and_kill
  137. 函数版本: 01.01
  138. 创建作者: xxxxxx
  139. 创建日期: 2015-02-10
  140. 函数说明: 检查进程是否运行,如果运行,kill掉进程。
  141. 参数说明: 无
  142. 返回值: 成功返回0,失败返回负数。
  143. 修改记录:
  144. */
  145. static int sntp_check_run_and_kill(char *name)
  146. {
  147. char buf[16];
  148. char cmd_path[] = "/bin/kill";
  149. char* cmd_argv[] = {cmd_path, "-INT", buf, NULL};
  150. char* cmd_envp[] = {NULL};
  151. //当前进程遍历起点
  152. struct task_struct *task = current;
  153. int ret = -1;
  154. //遍历进程链表,找到进程name
  155. do {
  156. if(strncmp(task->comm, name, strlen(name))==0)
  157. {
  158. //printk("*** %s [%d] parent %s\n",
  159. //task->comm, task->pid, task->parent->comm );
  160. sprintf(buf, "%d", task->pid);
  161. ret = call_usermodehelper(cmd_path, cmd_argv, cmd_envp, UMH_WAIT_PROC);
  162. break;
  163. }
  164. } while( (task = next_task(task)) != current );
  165. return ret;
  166. }
  167. /******************************************************************************
  168. 函数名称: sntp_proc
  169. 函数版本: 01.01
  170. 创建作者: xxxxxx
  171. 创建日期: 2015-02-10
  172. 函数说明: SNTP对时处理任务。
  173. 参数说明: 无
  174. 返回值: 成功返回0,失败返回负数。
  175. 修改记录:
  176. */
  177. static int sntp_proc(void *arg)
  178. {
  179. while(1)
  180. {
  181. //设置服务器IP才可以对时,否则什么都不做
  182. if(g_sntp_para.serverip)
  183. {
  184. //参数没有初始化,等待.(这是因为任务可能已经运行,但参数没有初始化)
  185. if(!g_sntp_para_init)
  186. {
  187. //检查程序是否退出
  188. if(main_mod_is_exit())
  189. {
  190. complete_and_exit(&g_exit_sntp_proc, 0);
  191. }
  192. msleep(100);
  193. continue;
  194. }
  195. //检查SNTP是否运行,如果运行,kill掉进程
  196. sntp_check_run_and_kill(SNTP_PROGNAME);
  197. //运行SNTP(ntpd)
  198. sntp_run();
  199. }
  200. else
  201. {
  202. //检查程序是否退出
  203. if(main_mod_is_exit())
  204. {
  205. complete_and_exit(&g_exit_sntp_proc, 0);
  206. }
  207. msleep(100);
  208. }
  209. }
  210. return 0;
  211. }
  212. /******************************************************************************
  213. 函数名称: sntp_para
  214. 函数版本: 01.01
  215. 创建作者: xxxxxx
  216. 创建日期: 2015-02-10
  217. 函数说明: 设置SNTP同步对时参数.
  218. 参数说明:
  219. serverip: 服务器IP
  220. tm: SNTP同步对时间隔
  221. 返回值: 成功返回0,失败返回负数。
  222. 修改记录:
  223. */
  224. int sntp_setpara(unsigned int serverip, int tm)
  225. {
  226. //置未初始化标志
  227. g_sntp_para_init = 0;
  228. //置参数修改标志
  229. g_sntp_paraflag = 1;
  230. //服务器IP
  231. g_sntp_para.serverip = serverip;
  232. if(tm <= 0)
  233. {
  234. tm = 64;
  235. }
  236. //同步时钟间隔
  237. g_sntp_para.tm = tm;
  238. //置初始化标志
  239. g_sntp_para_init = 1;
  240. return 0;
  241. }
  242. /******************************************************************************
  243. 函数名称: sntp_exit
  244. 函数版本: 01.01
  245. 创建作者: xxxxxx
  246. 创建日期: 2015-02-10
  247. 函数说明: SNTP对时退出.
  248. 参数说明:
  249. 无
  250. 返回值: 成功返回0,失败返回负数。
  251. 修改记录:
  252. */
  253. int sntp_exit(void)
  254. {
  255. wait_for_completion(&g_exit_sntp_proc);
  256. return 0;
  257. }
  258. /******************************************************************************
  259. 函数名称: sntp_init
  260. 函数版本: 01.01
  261. 创建作者: xxxxxx
  262. 创建日期: 2015-02-10
  263. 函数说明: SNTP对时初始化.
  264. 参数说明:
  265. 无
  266. 返回值: 成功返回0,失败返回负数。
  267. 修改记录:
  268. */
  269. int sntp_init(void)
  270. {
  271. pid_t pid;
  272. struct task_struct * ts;
  273. struct sched_param sp;
  274. init_completion(&g_exit_sntp_proc);
  275. //创建SNTP处理任务
  276. pid = kernel_thread(sntp_proc, NULL,CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  277. ts = find_task_by_vpid(pid);
  278. if(ts == NULL)
  279. {
  280. printk("%s:pid=%d,ts=%p\r\n",__func__,pid,ts);
  281. return -1;
  282. }
  283. // 设置调度策略和优先级
  284. sp.sched_priority = 13;
  285. sched_setscheduler(ts,SCHED_FIFO,&sp);
  286. // 设置进程名字
  287. set_task_comm(ts, "sntp_proc");
  288. return 0;
  289. }
  290. #endif