pid.c 838 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <stdio.h>
  2. #include <string.h>
  3. #if !defined _WIN32 || defined MINGW
  4. #include <unistd.h> // getpid(), unlink()
  5. #else
  6. #include <windows.h>
  7. #define getpid GetCurrentProcessId
  8. #endif
  9. #include "../common/plugin.h"
  10. PLUGIN_INFO = {
  11. "ProcessId",
  12. PLUGIN_ALL,
  13. "1.0",
  14. PLUGIN_VERSION,
  15. "Logs the process ID"
  16. };
  17. PLUGIN_EVENTS_TABLE = {
  18. { "pid_create", "Plugin_Init" },
  19. { "pid_delete", "Plugin_Final" },
  20. { NULL, NULL }
  21. };
  22. char pid_file[256];
  23. char *server_name;
  24. void pid_create ()
  25. {
  26. FILE *fp;
  27. int len;
  28. IMPORT_SYMBOL(server_name, 1);
  29. len = strlen(server_name);
  30. strcpy(pid_file, server_name);
  31. if(len > 4 && pid_file[len - 4] == '.') {
  32. pid_file[len - 4] = 0;
  33. }
  34. strcat(pid_file, ".pid");
  35. fp = fopen(pid_file, "w");
  36. if (fp) {
  37. fprintf(fp, "%d", getpid());
  38. fclose(fp);
  39. }
  40. }
  41. void pid_delete ()
  42. {
  43. unlink(pid_file);
  44. }