~exec64/imv

ref: 4c0123bb3b449a2ea45b3e831db725d853bb03a1 imv/src/imv_msg.c -rw-r--r-- 873 bytes
4c0123bb — Harry Jeffery Release v4.0.1 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "ipc.h"

int main(int argc, char **argv)
{
  if (argc < 3) {
    fprintf(stderr, "Usage: %s <pid> <command>\n", argv[0]);
    return 0;
  }

  int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  assert(sockfd);

  struct sockaddr_un desc = {
    .sun_family = AF_UNIX
  };
  imv_ipc_path(desc.sun_path, sizeof desc.sun_path, atoi(argv[1]));

  if (connect(sockfd, (struct sockaddr *)&desc, sizeof desc) < 0) {
    perror("Failed to connect");
    return 1;
  }

  char buf[4096] = {0};
  for (int i = 2; i < argc; ++i) {
    strncat(buf, argv[i], sizeof buf - 1);
    if (i + 1 < argc) {
      strncat(buf, " ", sizeof buf - 1);
    }
  }
  strncat(buf, "\n", sizeof buf - 1);

  write(sockfd, buf, strlen(buf));
  close(sockfd);
  return 0;
}