sntp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. {
  121. if (strncmp(task->comm, "ntpd", strlen("ntpd")) == 0)
  122. break;
  123. } while ((task = next_task(task)) != current);
  124. if (task == current)
  125. {
  126. sync = 1;
  127. sntp_time_synchronized = 1;
  128. }
  129. }
  130. // sunxi 20201027 added end 判断 ntpd 程序是否退出了,退出了说明对时成功了
  131. msleep(100);
  132. }
  133. }
  134. return 0;
  135. }
  136. /******************************************************************************
  137. 函数名称: sntp_check_run_and_kill
  138. 函数版本: 01.01
  139. 创建作者: xxxxxx
  140. 创建日期: 2015-02-10
  141. 函数说明: 检查进程是否运行,如果运行,kill掉进程。
  142. 参数说明: 无
  143. 返回值: 成功返回0,失败返回负数。
  144. 修改记录:
  145. */
  146. static int sntp_check_run_and_kill(char *name)
  147. {
  148. char buf[16];
  149. char cmd_path[] = "/bin/kill";
  150. char *cmd_argv[] = {cmd_path, "-INT", buf, NULL};
  151. char *cmd_envp[] = {NULL};
  152. // 当前进程遍历起点
  153. struct task_struct *task = current;
  154. int ret = -1;
  155. // 遍历进程链表,找到进程name
  156. do
  157. {
  158. if (strncmp(task->comm, name, strlen(name)) == 0)
  159. {
  160. // printk("*** %s [%d] parent %s\n",
  161. // task->comm, task->pid, task->parent->comm );
  162. sprintf(buf, "%d", task->pid);
  163. ret = call_usermodehelper(cmd_path, cmd_argv, cmd_envp, UMH_WAIT_PROC);
  164. break;
  165. }
  166. } while ((task = next_task(task)) != current);
  167. return ret;
  168. }
  169. /******************************************************************************
  170. 函数名称: sntp_proc
  171. 函数版本: 01.01
  172. 创建作者: xxxxxx
  173. 创建日期: 2015-02-10
  174. 函数说明: SNTP对时处理任务。
  175. 参数说明: 无
  176. 返回值: 成功返回0,失败返回负数。
  177. 修改记录:
  178. */
  179. static int sntp_proc(void *arg)
  180. {
  181. while (1)
  182. {
  183. // 设置服务器IP才可以对时,否则什么都不做
  184. if (g_sntp_para.serverip)
  185. {
  186. // 参数没有初始化,等待.(这是因为任务可能已经运行,但参数没有初始化)
  187. if (!g_sntp_para_init)
  188. {
  189. // 检查程序是否退出
  190. if (main_mod_is_exit())
  191. {
  192. complete_and_exit(&g_exit_sntp_proc, 0);
  193. }
  194. msleep(100);
  195. continue;
  196. }
  197. // 检查SNTP是否运行,如果运行,kill掉进程
  198. sntp_check_run_and_kill(SNTP_PROGNAME);
  199. // 运行SNTP(ntpd)
  200. sntp_run();
  201. }
  202. else
  203. {
  204. // 检查程序是否退出
  205. if (main_mod_is_exit())
  206. {
  207. complete_and_exit(&g_exit_sntp_proc, 0);
  208. }
  209. msleep(100);
  210. }
  211. }
  212. return 0;
  213. }
  214. /******************************************************************************
  215. 函数名称: sntp_para
  216. 函数版本: 01.01
  217. 创建作者: xxxxxx
  218. 创建日期: 2015-02-10
  219. 函数说明: 设置SNTP同步对时参数.
  220. 参数说明:
  221. serverip: 服务器IP
  222. tm: SNTP同步对时间隔
  223. 返回值: 成功返回0,失败返回负数。
  224. 修改记录:
  225. */
  226. int sntp_setpara(unsigned int serverip, int tm)
  227. {
  228. // 置未初始化标志
  229. g_sntp_para_init = 0;
  230. // 置参数修改标志
  231. g_sntp_paraflag = 1;
  232. // 服务器IP
  233. g_sntp_para.serverip = serverip;
  234. if (tm <= 0)
  235. {
  236. tm = 64;
  237. }
  238. // 同步时钟间隔
  239. g_sntp_para.tm = tm;
  240. // 置初始化标志
  241. g_sntp_para_init = 1;
  242. return 0;
  243. }
  244. /******************************************************************************
  245. 函数名称: sntp_exit
  246. 函数版本: 01.01
  247. 创建作者: xxxxxx
  248. 创建日期: 2015-02-10
  249. 函数说明: SNTP对时退出.
  250. 参数说明:
  251. 无
  252. 返回值: 成功返回0,失败返回负数。
  253. 修改记录:
  254. */
  255. int sntp_exit(void)
  256. {
  257. wait_for_completion(&g_exit_sntp_proc);
  258. return 0;
  259. }
  260. /******************************************************************************
  261. 函数名称: sntp_init
  262. 函数版本: 01.01
  263. 创建作者: xxxxxx
  264. 创建日期: 2015-02-10
  265. 函数说明: SNTP对时初始化.
  266. 参数说明:
  267. 无
  268. 返回值: 成功返回0,失败返回负数。
  269. 修改记录:
  270. */
  271. int sntp_init(void)
  272. {
  273. pid_t pid;
  274. struct task_struct *ts;
  275. struct sched_param sp;
  276. init_completion(&g_exit_sntp_proc);
  277. // 创建SNTP处理任务
  278. pid = kernel_thread(sntp_proc, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
  279. ts = find_task_by_vpid(pid);
  280. if (ts == NULL)
  281. {
  282. printk("%s:pid=%d,ts=%p\r\n", __func__, pid, ts);
  283. return -1;
  284. }
  285. // 设置调度策略和优先级
  286. sp.sched_priority = 13;
  287. sched_setscheduler(ts, SCHED_FIFO, &sp);
  288. // 设置进程名字
  289. set_task_comm(ts, "sntp_proc");
  290. return 0;
  291. }
  292. #endif