Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

  1. #!/usr/bin/perl
  2. eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  3. if $running_under_some_shell;
  4. require 'sys/ipc.ph';
  5. require 'sys/shm.ph';
  6. $| = 1;
  7. $mode = shift;
  8. die "usage: ipcshm {r|s}\n" unless $mode =~ /^[rs]$/;
  9. $send = ($mode eq "s");
  10. $SIZE = 32;
  11. $id = shmget(0x1234, $SIZE, ($send ? 0 : &IPC_CREAT) | 0644);
  12. die "Can't get shared memory: $!\n" unless defined($id);
  13. print "shared memory id: $id\n";
  14. if ($send) {
  15. while (<STDIN>) {
  16. chop;
  17. unless (shmwrite($id, pack("La*", length($_), $_), 0, $SIZE)) {
  18. die "Can't write to shared memory: $!\n";
  19. }
  20. }
  21. }
  22. else {
  23. $SIG{'INT'} = $SIG{'QUIT'} = "leave";
  24. for (;;) {
  25. $_ = <STDIN>;
  26. unless (shmread($id, $_, 0, $SIZE)) {
  27. die "Can't read shared memory: $!\n";
  28. }
  29. $len = unpack("L", $_);
  30. $message = substr($_, length(pack("L",0)), $len);
  31. printf "[%d] %s\n", $len, $message;
  32. }
  33. }
  34. &leave;
  35. sub leave {
  36. if (!$send) {
  37. $x = shmctl($id, &IPC_RMID, 0);
  38. if (!defined($x) || $x < 0) {
  39. die "Can't remove shared memory: $!\n";
  40. }
  41. }
  42. exit;
  43. }