[systemd-devel] systemd-activation: it's a mess for beginners

Reindl Harald h.reindl at thelounge.net
Wed Mar 20 19:46:35 UTC 2019


http://0pointer.de/blog/projects/socket-activation.html

---------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include </usr/include/systemd/sd-daemon.h>

int fd;

if(sd_listen_fds(0) != 1)
{
 fprintf(stderr, "No or too many file descriptors received.\n");
 exit(1);
}

fd = SD_LISTEN_FDS_START + 0;

---------------------------------------------

demo-udp.c:16:1: error: expected identifier or '(' before 'if'
 if(sd_listen_fds(0) != 1)
 ^~
demo-udp.c:22:1: warning: data definition has no type or storage class
 fd = SD_LISTEN_FDS_START + 0;
 ^~
demo-udp.c:22:1: warning: type defaults to 'int' in declaration of 'fd'
[-Wimplicit-int]

---------------------------------------------

that thing below "just works" (ipv4 only but well..) as ordinary
service, frankly i just want a "as-simple-as-possible" binary which
receives UDP, get fired up by systemd on-demand, answers whatever to
signal "this port is open and reachbale" to the client, not more and not
less without have to run it all the time

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 1024
int main(int argc, char *argv[])
{
 if(argc < 3 || argc > 3)
 {
  printf("USAGE: demo-udp.bin LISTEN_IP LISTEN_PORT\n");
  exit(EXIT_FAILURE);
 }
 char* LISTEN_IP = argv[1];
 int LISTEN_PORT = atoi(argv[2]);
 if(LISTEN_PORT < 1 || LISTEN_PORT > 65535)
 {
  printf("port out of range 1-65535\n");
  exit(EXIT_FAILURE);
 }
 int sockfd;
 char buffer[MAXLINE];
 char *pong = "PONG";
 struct sockaddr_in servaddr, cliaddr;
 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
 {
  perror("socket creation");
  exit(EXIT_FAILURE);
 }
 memset(&servaddr, 0, sizeof(servaddr));
 memset(&cliaddr, 0, sizeof(cliaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr = inet_addr(LISTEN_IP);
 servaddr.sin_port = htons(LISTEN_PORT);
 if(bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
 {
  perror("bind");
  exit(EXIT_FAILURE);
 }
 int len, n;
 while(1)
 {
  recvfrom(sockfd, (char *)buffer, MAXLINE, MSG_WAITALL, (struct
sockaddr *) &cliaddr, &len);
  sendto(sockfd, (const char *)pong, strlen(pong), MSG_CONFIRM, (const
struct sockaddr *) &cliaddr, len);
 }
}


More information about the systemd-devel mailing list