1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <stdio.h>
18 #include <termios.h>
19 #include <unistd.h>
20 #include "test.h"
21
22 #define BACKGROUND (0)
23 #define FOREGROUND (1)
24 #define TCGETPGRPERROR (-1)
25
tcgetpgrp_handler(void)26 int tcgetpgrp_handler(void)
27 {
28 pid_t pid;
29 errno = 0;
30 pid = tcgetpgrp(STDIN_FILENO);
31 if (pid == -1) {
32 return TCGETPGRPERROR;
33 } else if (pid == getpgrp()) {
34 return FOREGROUND;
35 } else {
36 return BACKGROUND;
37 }
38 }
39
40 /**
41 * @tc.name : tcgetpgrp_0100
42 * @tc.desc : Get terminal foreground process
43 * @tc.level : Level 0
44 */
tcgetpgrp_0100(void)45 void tcgetpgrp_0100(void)
46 {
47 int result, ret;
48 pid_t spid;
49 spid = tcgetsid(STDIN_FILENO);
50 ret = tcgetpgrp_handler();
51 if (ret != 1) {
52 t_error("%s tcgetpgrp is not foreground\n", __func__);
53 }
54 result = tcsetpgrp(STDIN_FILENO, getpgrp());
55 if (result == -1) {
56 t_error("%s tcsetpgrp failed", __func__);
57 }
58
59 ret = tcgetpgrp_handler();
60 result = tcsetpgrp(STDIN_FILENO, spid);
61 }
62
63 /**
64 * @tc.name : tcgetpgrp_0200
65 * @tc.desc : Invalid parameter input
66 * @tc.level : Level 2
67 */
tcgetpgrp_0200(void)68 void tcgetpgrp_0200(void)
69 {
70 int result;
71 result = tcgetpgrp(-1);
72 if (result != -1) {
73 t_error("%s tcgetpgrp should failed, result is %d\n", __func__, result);
74 }
75 }
76
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 tcgetpgrp_0100();
80 tcgetpgrp_0200();
81 return t_status;
82 }