pid.c 831 B

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